From 903eeb7db7480e6057de1e6a0ae592e5d4bf2a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20THEROND=20=28Fl1nt=29?= Date: Wed, 22 Feb 2023 16:12:18 +0100 Subject: [PATCH] Add module to filter available volume services. Add a way to filter which volume service is running on a host or list which hosts run available volume services. Closes-Bug: #2010490 Change-Id: Icb17f6019a61d9346472d83ddcd2ad29c340ea05 --- ci/roles/volume_service/defaults/main.yml | 9 ++ ci/roles/volume_service/tasks/main.yml | 23 +++++ ci/run-collection.yml | 1 + meta/runtime.yml | 1 + plugins/modules/volume_service_info.py | 103 ++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 ci/roles/volume_service/defaults/main.yml create mode 100644 ci/roles/volume_service/tasks/main.yml create mode 100644 plugins/modules/volume_service_info.py diff --git a/ci/roles/volume_service/defaults/main.yml b/ci/roles/volume_service/defaults/main.yml new file mode 100644 index 00000000..c9f2a969 --- /dev/null +++ b/ci/roles/volume_service/defaults/main.yml @@ -0,0 +1,9 @@ +expected_fields: + - availability_zone + - binary + - disabled_reason + - host + - name + - state + - status + - updated_at diff --git a/ci/roles/volume_service/tasks/main.yml b/ci/roles/volume_service/tasks/main.yml new file mode 100644 index 00000000..33fd6045 --- /dev/null +++ b/ci/roles/volume_service/tasks/main.yml @@ -0,0 +1,23 @@ +--- +- name: Fetch volume services + openstack.cloud.volume_service_info: + cloud: "{{ cloud }}" + register: volume_services + +- name: Assert return values of volume_service_info module + assert: + that: + - volume_services.volume_services | length > 0 + # allow new fields to be introduced but prevent fields from being removed + - expected_fields|difference(volume_services.volume_services[0].keys())|length == 0 + +- name: Fetch volume services with filters + openstack.cloud.volume_service_info: + cloud: "{{ cloud }}" + binary: "cinder-volume" + register: volume_services + +- name: Assert return values of volume_service_info module + assert: + that: + - volume_services.volume_services | length > 0 diff --git a/ci/run-collection.yml b/ci/run-collection.yml index 1d82b178..6bafa6f2 100644 --- a/ci/run-collection.yml +++ b/ci/run-collection.yml @@ -57,5 +57,6 @@ - { role: volume, tags: volume } - { role: volume_type, tags: volume_type } - { role: volume_backup, tags: volume_backup } + - { role: volume_service, tags: volume_service } - { role: volume_snapshot, tags: volume_snapshot } - { role: volume_type_access, tags: volume_type_access } diff --git a/meta/runtime.yml b/meta/runtime.yml index 2b04142e..81aaa08a 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -85,6 +85,7 @@ action_groups: - volume_backup - volume_backup_info - volume_info + - volume_service_info - volume_snapshot - volume_snapshot_info - volume_type_access diff --git a/plugins/modules/volume_service_info.py b/plugins/modules/volume_service_info.py new file mode 100644 index 00000000..75b99ef4 --- /dev/null +++ b/plugins/modules/volume_service_info.py @@ -0,0 +1,103 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2023 Bitswalk, inc. +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +DOCUMENTATION = r''' +--- +module: volume_service_info +short_description: Fetch OpenStack Volume (Cinder) services +author: OpenStack Ansible SIG +description: + - Fetch OpenStack Volume (Cinder) services. +options: + binary: + description: + - Filter the service list result by binary name of the service. + type: str + host: + description: + - Filter the service list result by the host name. + type: str +extends_documentation_fragment: + - openstack.cloud.openstack +''' + +EXAMPLES = r''' +- name: Fetch all OpenStack Volume (Cinder) services + openstack.cloud.volume_service_info: + cloud: awesomecloud + +- name: Fetch a subset of OpenStack Volume (Cinder) services + openstack.cloud.volume_service_info: + cloud: awesomecloud + binary: "cinder-volume" + host: "localhost" +''' + +RETURN = r''' +volume_services: + description: List of dictionaries describing Volume (Cinder) services. + returned: always + type: list + elements: dict + contains: + availability_zone: + description: The availability zone name. + type: str + binary: + description: The binary name of the service. + type: str + disabled_reason: + description: The reason why the service is disabled + type: str + host: + description: The name of the host. + type: str + name: + description: Service name + type: str + state: + description: The state of the service. One of up or down. + type: str + status: + description: The status of the service. One of enabled or disabled. + type: str + update_at: + description: The date and time when the resource was updated + type: str +''' + +from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule + + +class VolumeServiceInfoModule(OpenStackModule): + + argument_spec = dict( + binary=dict(), + host=dict(), + ) + + module_kwargs = dict( + supports_check_mode=True + ) + + def run(self): + kwargs = {k: self.params[k] + for k in ['binary', 'host'] + if self.params[k] is not None} + volume_services = self.conn.block_storage.services(**kwargs) + + self.exit_json(changed=False, + volume_services=[s.to_dict(computed=False) + for s in volume_services]) + + +def main(): + module = VolumeServiceInfoModule() + module() + + +if __name__ == '__main__': + main()