From 894b86a467b2d034efea6088254cbd5838c0bc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Wed, 7 Jun 2017 00:35:24 +0200 Subject: [PATCH] cloudstack: cs_zone_facts: add tests and fix pep8 (#25407) --- .../modules/cloud/cloudstack/cs_zone_facts.py | 54 ++++++++++--------- .../integration/targets/cs_zone_facts/aliases | 2 + .../targets/cs_zone_facts/meta/main.yml | 3 ++ .../targets/cs_zone_facts/tasks/main.yml | 32 +++++++++++ test/sanity/pep8/legacy-files.txt | 1 - 5 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 test/integration/targets/cs_zone_facts/aliases create mode 100644 test/integration/targets/cs_zone_facts/meta/main.yml create mode 100644 test/integration/targets/cs_zone_facts/tasks/main.yml diff --git a/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py b/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py index f1c4d300c91..094bfd7811a 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py @@ -36,6 +36,7 @@ options: description: - Name of the zone. required: true + aliases: [ zone ] extends_documentation_fragment: cloudstack ''' @@ -142,44 +143,43 @@ cloudstack_zone.tags: sample: [ { "key": "foo", "value": "bar" } ] ''' -import base64 +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.cloudstack import ( + AnsibleCloudStack, + CloudStackException, + cs_argument_spec, +) -# import cloudstack common -from ansible.module_utils.cloudstack import * class AnsibleCloudStackZoneFacts(AnsibleCloudStack): def __init__(self, module): super(AnsibleCloudStackZoneFacts, self).__init__(module) self.returns = { - 'dns1': 'dns1', - 'dns2': 'dns2', - 'internaldns1': 'internal_dns1', - 'internaldns2': 'internal_dns2', - 'ipv6dns1': 'dns1_ipv6', - 'ipv6dns2': 'dns2_ipv6', - 'domain': 'network_domain', - 'networktype': 'network_type', - 'securitygroupsenabled': 'securitygroups_enabled', - 'localstorageenabled': 'local_storage_enabled', - 'guestcidraddress': 'guest_cidr_address', - 'dhcpprovider': 'dhcp_provider', - 'allocationstate': 'allocation_state', - 'zonetoken': 'zone_token', + 'dns1': 'dns1', + 'dns2': 'dns2', + 'internaldns1': 'internal_dns1', + 'internaldns2': 'internal_dns2', + 'ipv6dns1': 'dns1_ipv6', + 'ipv6dns2': 'dns2_ipv6', + 'domain': 'network_domain', + 'networktype': 'network_type', + 'securitygroupsenabled': 'securitygroups_enabled', + 'localstorageenabled': 'local_storage_enabled', + 'guestcidraddress': 'guest_cidr_address', + 'dhcpprovider': 'dhcp_provider', + 'allocationstate': 'allocation_state', + 'zonetoken': 'zone_token', } self.facts = { 'cloudstack_zone': None, } - def get_zone(self): if not self.zone: - # TODO: add param key signature in get_zone() - self.module.params['zone'] = self.module.params.get('name') super(AnsibleCloudStackZoneFacts, self).get_zone() return self.zone - def run(self): zone = self.get_zone() self.facts['cloudstack_zone'] = self.get_result(zone) @@ -189,7 +189,7 @@ class AnsibleCloudStackZoneFacts(AnsibleCloudStack): def main(): argument_spec = cs_argument_spec() argument_spec.update(dict( - name = dict(required=True), + zone=dict(required=True, aliases=['name']), )) module = AnsibleModule( @@ -197,10 +197,14 @@ def main(): supports_check_mode=False, ) - cs_zone_facts = AnsibleCloudStackZoneFacts(module=module).run() - cs_facts_result = dict(changed=False, ansible_facts=cs_zone_facts) + try: + cs_zone_facts = AnsibleCloudStackZoneFacts(module=module).run() + cs_facts_result = dict(changed=False, ansible_facts=cs_zone_facts) + except CloudStackException as e: + module.fail_json(msg='CloudStackException: %s' % str(e)) + module.exit_json(**cs_facts_result) -from ansible.module_utils.basic import * + if __name__ == '__main__': main() diff --git a/test/integration/targets/cs_zone_facts/aliases b/test/integration/targets/cs_zone_facts/aliases new file mode 100644 index 00000000000..ba249b99d73 --- /dev/null +++ b/test/integration/targets/cs_zone_facts/aliases @@ -0,0 +1,2 @@ +cloud/cs +posix/ci/cloud/cs diff --git a/test/integration/targets/cs_zone_facts/meta/main.yml b/test/integration/targets/cs_zone_facts/meta/main.yml new file mode 100644 index 00000000000..e9a5b9eeaef --- /dev/null +++ b/test/integration/targets/cs_zone_facts/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - cs_common diff --git a/test/integration/targets/cs_zone_facts/tasks/main.yml b/test/integration/targets/cs_zone_facts/tasks/main.yml new file mode 100644 index 00000000000..09aea5f850f --- /dev/null +++ b/test/integration/targets/cs_zone_facts/tasks/main.yml @@ -0,0 +1,32 @@ +--- +- name: setup zone is present + cs_zone: + name: "{{ cs_resource_prefix }}-zone" + dns1: 8.8.8.8 + dns2: 8.8.4.4 + network_type: basic + register: zone +- name: verify setup zone is present + assert: + that: + - zone|success + +- name: get facts from zone + cs_zone_facts: + name: "{{ cs_resource_prefix }}-zone" + register: zone +- name: verify setup zone is present + assert: + that: + - zone|success + - not zone|changed + - ansible_facts.cloudstack_zone.dns1 == "8.8.8.8" + - ansible_facts.cloudstack_zone.dns2 == "8.8.4.4" + - ansible_facts.cloudstack_zone.internal_dns1 == "8.8.8.8" + - ansible_facts.cloudstack_zone.internal_dns2 == "8.8.4.4" + - ansible_facts.cloudstack_zone.local_storage_enabled == false + - ansible_facts.cloudstack_zone.network_type == "Basic" + - ansible_facts.cloudstack_zone.zone_token != "" + - ansible_facts.cloudstack_zone.securitygroups_enabled == true + - ansible_facts.cloudstack_zone.dhcp_provider == "VirtualRouter" + - ansible_facts.cloudstack_zone.local_storage_enabled == false diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index dee41d950b3..132e294de50 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -102,7 +102,6 @@ lib/ansible/modules/cloud/cloudstack/cs_user.py lib/ansible/modules/cloud/cloudstack/cs_vmsnapshot.py lib/ansible/modules/cloud/cloudstack/cs_volume.py lib/ansible/modules/cloud/cloudstack/cs_zone.py -lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py lib/ansible/modules/cloud/docker/_docker.py lib/ansible/modules/cloud/docker/docker_container.py lib/ansible/modules/cloud/docker/docker_image.py