From 439f0beca5b58aab590cb4f2f64ec48cd6456245 Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Sun, 28 May 2017 01:31:00 +0200 Subject: [PATCH] cloudstack: fix pep8 cs_domain --- .../modules/cloud/cloudstack/cs_domain.py | 65 +++++++++---------- .../targets/cs_domain/tasks/main.yml | 11 ++++ test/sanity/pep8/legacy-files.txt | 1 - 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_domain.py b/lib/ansible/modules/cloud/cloudstack/cs_domain.py index d5e0881e5b9..8fe6e42273c 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_domain.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_domain.py @@ -111,8 +111,13 @@ network_domain: sample: example.local ''' -# import cloudstack common -from ansible.module_utils.cloudstack import * +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.cloudstack import ( + AnsibleCloudStack, + CloudStackException, + cs_argument_spec, + cs_required_together +) class AnsibleCloudStackDomain(AnsibleCloudStack): @@ -120,13 +125,12 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): def __init__(self, module): super(AnsibleCloudStackDomain, self).__init__(module) self.returns = { - 'path': 'path', - 'networkdomain': 'network_domain', + 'path': 'path', + 'networkdomain': 'network_domain', 'parentdomainname': 'parent_domain', } self.domain = None - def _get_domain_internal(self, path=None): if not path: path = self.module.params.get('path') @@ -141,8 +145,9 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): elif not path.startswith('root/'): path = "root/" + path - args = {} - args['listall'] = True + args = { + 'listall': True + } domains = self.cs.listDomains(**args) if domains: @@ -151,19 +156,16 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): return d return None - def get_name(self): # last part of the path is the name name = self.module.params.get('path').split('/')[-1:] return name - def get_domain(self, key=None): if not self.domain: self.domain = self._get_domain_internal() return self._get_by_key(key, self.domain) - def get_parent_domain(self, key=None): path = self.module.params.get('path') # cut off last /* @@ -175,7 +177,6 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): self.module.fail_json(msg="Parent domain path %s does not exist" % path) return self._get_by_key(key, parent_domain) - def present_domain(self): domain = self.get_domain() if not domain: @@ -184,15 +185,14 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): domain = self.update_domain(domain) return domain - def create_domain(self, domain): self.result['changed'] = True - args = {} - args['name'] = self.get_name() - args['parentdomainid'] = self.get_parent_domain(key='id') - args['networkdomain'] = self.module.params.get('network_domain') - + args = { + 'name': self.get_name(), + 'parentdomainid': self.get_parent_domain(key='id'), + 'networkdomain': self.module.params.get('network_domain') + } if not self.module.check_mode: res = self.cs.createDomain(**args) if 'errortext' in res: @@ -200,12 +200,11 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): domain = res['domain'] return domain - def update_domain(self, domain): - args = {} - args['id'] = domain['id'] - args['networkdomain'] = self.module.params.get('network_domain') - + args = { + 'id': domain['id'], + 'networkdomain': self.module.params.get('network_domain') + } if self.has_changed(args, domain): self.result['changed'] = True if not self.module.check_mode: @@ -215,16 +214,16 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): domain = res['domain'] return domain - def absent_domain(self): domain = self.get_domain() if domain: self.result['changed'] = True if not self.module.check_mode: - args = {} - args['id'] = domain['id'] - args['cleanup'] = self.module.params.get('clean_up') + args = { + 'id': domain['id'], + 'cleanup': self.module.params.get('clean_up') + } res = self.cs.deleteDomain(**args) if 'errortext' in res: @@ -236,15 +235,14 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): return domain - def main(): argument_spec = cs_argument_spec() argument_spec.update(dict( - path = dict(required=True), - state = dict(choices=['present', 'absent'], default='present'), - network_domain = dict(default=None), - clean_up = dict(type='bool', default=False), - poll_async = dict(type='bool', default=True), + path=dict(required=True), + state=dict(choices=['present', 'absent'], default='present'), + network_domain=dict(), + clean_up=dict(type='bool', default=False), + poll_async=dict(type='bool', default=True), )) module = AnsibleModule( @@ -269,7 +267,6 @@ def main(): module.exit_json(**result) -# import module snippets -from ansible.module_utils.basic import * + if __name__ == '__main__': main() diff --git a/test/integration/targets/cs_domain/tasks/main.yml b/test/integration/targets/cs_domain/tasks/main.yml index 5e10f1b018b..0a523663b49 100644 --- a/test/integration/targets/cs_domain/tasks/main.yml +++ b/test/integration/targets/cs_domain/tasks/main.yml @@ -56,6 +56,17 @@ - dom.path == "ROOT/{{ cs_resource_prefix }}_domain" - dom.name == "{{ cs_resource_prefix }}_domain" +- name: test fail to create a subdomain for inexistent domain + cs_domain: + path: ROOT/inexistent/{{ cs_resource_prefix }}_subdomain + register: dom + ignore_errors: true +- name: test fail to create a subdomain for inexistent domain + assert: + that: + - dom|failed + - dom.msg == "Parent domain path ROOT/inexistent does not exist" + - name: test create a subdomain in check mode cs_domain: path=ROOT/{{ cs_resource_prefix }}_domain/{{ cs_resource_prefix }}_subdomain register: dom diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 3e3c5f05102..3024a562462 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -165,7 +165,6 @@ lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_facts.py lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py lib/ansible/modules/cloud/cloudscale/cloudscale_server.py lib/ansible/modules/cloud/cloudstack/cs_configuration.py -lib/ansible/modules/cloud/cloudstack/cs_domain.py lib/ansible/modules/cloud/cloudstack/cs_facts.py lib/ansible/modules/cloud/cloudstack/cs_instance.py lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py