From 851cfc0c1837581b9437ea991d1fac7ff5eb90f5 Mon Sep 17 00:00:00 2001 From: "Patryk D. Cichy" Date: Sat, 16 Mar 2019 07:58:55 +0100 Subject: [PATCH] Add new Cloudstack module cs_image_store (#53617) --- .../cloud/cloudstack/cs_image_store.py | 248 ++++++++++++++++++ .../targets/cs_image_store/aliases | 2 + .../targets/cs_image_store/meta/main.yml | 3 + .../targets/cs_image_store/tasks/main.yml | 151 +++++++++++ 4 files changed, 404 insertions(+) create mode 100644 lib/ansible/modules/cloud/cloudstack/cs_image_store.py create mode 100644 test/integration/targets/cs_image_store/aliases create mode 100644 test/integration/targets/cs_image_store/meta/main.yml create mode 100644 test/integration/targets/cs_image_store/tasks/main.yml diff --git a/lib/ansible/modules/cloud/cloudstack/cs_image_store.py b/lib/ansible/modules/cloud/cloudstack/cs_image_store.py new file mode 100644 index 00000000000..aa04be71d19 --- /dev/null +++ b/lib/ansible/modules/cloud/cloudstack/cs_image_store.py @@ -0,0 +1,248 @@ +#!/usr/bin/python + +# Copyright: (c) 2019, Patryk Cichy @PatTheSilent +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = ''' +--- +module: cs_image_store + +short_description: Manages CloudStack Image Stores. + +version_added: "2.8" + +description: + - Deploy, remove, recreate CloudStack Image Stores. + +options: + url: + description: + - The URL for the Image Store. + - Required when I(state=present). + type: str + name: + description: + - The ID of the Image Store. Required when deleting a Image Store. + required: true + type: str + zone: + description: + - The Zone name for the Image Store. + required: true + type: str + state: + description: + - Stage of the Image Store + choices: [present, absent] + default: present + type: str + provider: + description: + - The image store provider name. Required when creating a new Image Store + type: str + force_recreate: + description: + - Set to C(yes) if you're changing an existing Image Store. + - This will force the recreation of the Image Store. + - Recreation might fail if there are snapshots present on the Image Store. Delete them before running the recreation. + type: bool + default: no + +extends_documentation_fragment: cloudstack + +author: + - Patryk Cichy (@PatTheSilent) +''' + +EXAMPLES = ''' +- name: Add a Image Store (NFS) + cs_image_store: + zone: zone-01 + name: nfs-01 + provider: NFS + url: nfs://192.168.21.16/exports/secondary + delegate_to: localhost + +# Change the NFS share URL and force a Image Store recreation +- name: Change the NFS url + cs_image_store: + zone: zone-01 + name: nfs-01 + provider: NFS + force_recreate: yes + url: nfs://192.168.21.10/shares/secondary + delegate_to: localhost + +- name: delete the image store + cs_image_store: + name: nfs-01 + zone: zone-01 + state: absent + delegate_to: localhost + +''' + +RETURN = ''' +id: + description: the ID of the image store + type: str + returned: success + sample: feb11a84-a093-45eb-b84d-7f680313c40b +name: + description: the name of the image store + type: str + returned: success + sample: nfs-01 +protocol: + description: the protocol of the image store + type: str + returned: success + sample: nfs +provider_name: + description: the provider name of the image store + type: str + returned: success + sample: NFS +scope: + description: the scope of the image store + type: str + returned: success + sample: ZONE +url: + description: the url of the image store + type: str + sample: nfs://192.168.21.16/exports/secondary + returned: success +zone: + description: the Zone name of the image store + type: str + returned: success + sample: zone-01 +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together + + +class AnsibleCloudstackImageStore(AnsibleCloudStack): + + def __init__(self, module): + super(AnsibleCloudstackImageStore, self).__init__(module) + self.returns = { + 'protocol': 'protocol', + 'providername': 'provider_name', + 'scope': 'scope', + 'url': 'url' + } + self.image_store = None + + def get_storage_providers(self, storage_type="image"): + args = { + 'type': storage_type + } + storage_provides = self.query_api('listStorageProviders', **args) + return [provider.get('name') for provider in storage_provides.get('dataStoreProvider')] + + def get_image_store(self): + if self.image_store: + return self.image_store + image_store = self.module.params.get('name') + args = { + 'name': self.module.params.get('name'), + 'zoneid': self.get_zone(key='id') + } + + image_stores = self.query_api('listImageStores', **args) + if image_stores: + for img_s in image_stores.get('imagestore'): + if image_store.lower() in [img_s['name'].lower(), img_s['id']]: + self.image_store = img_s + break + + return self.image_store + + def present_image_store(self): + provider_list = self.get_storage_providers() + image_store = self.get_image_store() + + if self.module.params.get('provider') not in provider_list: + self.module.fail_json( + msg='Provider %s is not in the provider list (%s). Please specify a correct provider' % ( + self.module.params.get('provider'), provider_list)) + args = { + 'name': self.module.params.get('name'), + 'url': self.module.params.get('url'), + 'zoneid': self.get_zone(key='id'), + 'provider': self.module.params.get('provider') + } + if not image_store: + self.result['changed'] = True + if not self.module.check_mode: + res = self.query_api('addImageStore', **args) + self.image_store = res.get('imagestore') + else: + # Cloudstack API expects 'provider' but returns 'providername' + args['providername'] = args.pop('provider') + if self.has_changed(args, image_store): + if self.module.params.get('force_recreate'): + self.absent_image_store() + self.image_store = None + self.image_store = self.present_image_store() + else: + self.module.warn("Changes to the Image Store won't be applied" + "Use force_recreate=yes to allow the store to be recreated") + + return self.image_store + + def absent_image_store(self): + image_store = self.get_image_store() + if image_store: + self.result['changed'] = True + if not self.module.check_mode: + args = { + 'id': image_store.get('id') + } + self.query_api('deleteImageStore', **args) + return image_store + + +def main(): + argument_spec = cs_argument_spec() + argument_spec.update(dict( + url=dict(), + name=dict(required=True), + zone=dict(required=True), + provider=dict(), + force_recreate=dict(type='bool', default=False), + state=dict(choices=['present', 'absent'], default='present'), + )) + + module = AnsibleModule( + argument_spec=argument_spec, + required_together=cs_required_together(), + required_if=[ + ('state', 'present', ['url', 'provider']), + ], + supports_check_mode=True + ) + + acis_do = AnsibleCloudstackImageStore(module) + + state = module.params.get('state') + if state == "absent": + image_store = acis_do.absent_image_store() + else: + image_store = acis_do.present_image_store() + + result = acis_do.get_result(image_store) + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/cs_image_store/aliases b/test/integration/targets/cs_image_store/aliases new file mode 100644 index 00000000000..c89c86d7d28 --- /dev/null +++ b/test/integration/targets/cs_image_store/aliases @@ -0,0 +1,2 @@ +cloud/cs +shippable/cs/group1 diff --git a/test/integration/targets/cs_image_store/meta/main.yml b/test/integration/targets/cs_image_store/meta/main.yml new file mode 100644 index 00000000000..e9a5b9eeaef --- /dev/null +++ b/test/integration/targets/cs_image_store/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - cs_common diff --git a/test/integration/targets/cs_image_store/tasks/main.yml b/test/integration/targets/cs_image_store/tasks/main.yml new file mode 100644 index 00000000000..d8a916f2ebb --- /dev/null +++ b/test/integration/targets/cs_image_store/tasks/main.yml @@ -0,0 +1,151 @@ +--- +- name: setup image store is absent + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + state: absent + register: sp +- name: verify setup image store is absent + assert: + that: + - sp is successful + +- name: test fail if missing params + cs_image_store: + register: ss + ignore_errors: true +- name: verify test fail if missing params + assert: + that: + - ss is failed + - "'name' in ss.msg" + - "'zone' in ss.msg" + - "'missing required arguments: ' in ss.msg" + +- name: setup image store with wrong parameters + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + state: present + ignore_errors: true + register: ss +- name: verify setup image store with wrong parameters + assert: + that: + - ss is failed + - "ss.msg == 'state is present but all of the following are missing: url, provider'" + +- name: setup image store in check mode + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + url: "nfs://nfs-mount.domain/share/images/" + provider: "NFS" + state: present + check_mode: true + register: ss +- name: verify setup image store in check mode + assert: + that: + - ss is successful + - ss is changed + +- name: setup image store + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + url: "nfs://nfs-mount.domain/share/images/" + provider: "NFS" + state: present + register: ss +- name: verify setup image store + assert: + that: + - ss is successful + - ss is changed + - "ss.url == 'nfs://nfs-mount.domain/share/images/'" + - "ss.provider_name == 'NFS'" + - "ss.zone == cs_common_zone_adv" + - "ss.protocol == 'nfs'" + +- name: setup image store idempotence + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + url: "nfs://nfs-mount.domain/share/images/" + provider: "NFS" + state: present + register: ss +- name: verify setup image store idempotence + assert: + that: + - ss is successful + - ss is not changed + - "ss.url == 'nfs://nfs-mount.domain/share/images/'" + - "ss.provider_name == 'NFS'" + - "ss.zone == cs_common_zone_adv" + - "ss.protocol == 'nfs'" + - "ss.name == 'storage_pool_adv'" + +- name: image store not recreated + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + url: "nfs://nfs-mount.domain/share2/images/" + provider: "NFS" + state: present + register: ss +- name: verify image store not recreated + assert: + that: + - ss is successful + - ss is not changed + - "ss.url == 'nfs://nfs-mount.domain/share/images/'" + - "ss.name == 'storage_pool_adv'" + - "ss.zone == cs_common_zone_adv" + +- name: recreate image store + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + url: "nfs://nfs-mount.domain/share2/images/" + provider: "NFS" + force_recreate: yes + state: present + register: ss +- name: verify setup image store idempotence + assert: + that: + - ss is successful + - ss is changed + - "ss.url == 'nfs://nfs-mount.domain/share2/images/'" + - "ss.name == 'storage_pool_adv'" + - "ss.zone == cs_common_zone_adv" + +- name: delete the image store + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + state: absent + register: ss +- name: verify results for delete the image store + assert: + that: + - ss is successful + - ss is changed + - "ss.name == 'storage_pool_adv'" + - "ss.zone == cs_common_zone_adv" + +- name: delete the image store idempotence + cs_image_store: + name: "storage_pool_adv" + zone: "{{ cs_common_zone_adv }}" + state: absent + register: ss +- name: verify delete the image store idempotence + assert: + that: + - ss is successful + - ss is not changed + - ss.name is undefined + - "ss.zone == cs_common_zone_adv" \ No newline at end of file