cloudstack: fix pep8 cs_domain

This commit is contained in:
Rene Moser 2017-05-28 01:31:00 +02:00 committed by René Moser
parent 1778e23fad
commit 439f0beca5
3 changed files with 42 additions and 35 deletions

View file

@ -111,8 +111,13 @@ network_domain:
sample: example.local sample: example.local
''' '''
# import cloudstack common from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import * from ansible.module_utils.cloudstack import (
AnsibleCloudStack,
CloudStackException,
cs_argument_spec,
cs_required_together
)
class AnsibleCloudStackDomain(AnsibleCloudStack): class AnsibleCloudStackDomain(AnsibleCloudStack):
@ -126,7 +131,6 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
} }
self.domain = None self.domain = None
def _get_domain_internal(self, path=None): def _get_domain_internal(self, path=None):
if not path: if not path:
path = self.module.params.get('path') path = self.module.params.get('path')
@ -141,8 +145,9 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
elif not path.startswith('root/'): elif not path.startswith('root/'):
path = "root/" + path path = "root/" + path
args = {} args = {
args['listall'] = True 'listall': True
}
domains = self.cs.listDomains(**args) domains = self.cs.listDomains(**args)
if domains: if domains:
@ -151,19 +156,16 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
return d return d
return None return None
def get_name(self): def get_name(self):
# last part of the path is the name # last part of the path is the name
name = self.module.params.get('path').split('/')[-1:] name = self.module.params.get('path').split('/')[-1:]
return name return name
def get_domain(self, key=None): def get_domain(self, key=None):
if not self.domain: if not self.domain:
self.domain = self._get_domain_internal() self.domain = self._get_domain_internal()
return self._get_by_key(key, self.domain) return self._get_by_key(key, self.domain)
def get_parent_domain(self, key=None): def get_parent_domain(self, key=None):
path = self.module.params.get('path') path = self.module.params.get('path')
# cut off last /* # cut off last /*
@ -175,7 +177,6 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
self.module.fail_json(msg="Parent domain path %s does not exist" % path) self.module.fail_json(msg="Parent domain path %s does not exist" % path)
return self._get_by_key(key, parent_domain) return self._get_by_key(key, parent_domain)
def present_domain(self): def present_domain(self):
domain = self.get_domain() domain = self.get_domain()
if not domain: if not domain:
@ -184,15 +185,14 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
domain = self.update_domain(domain) domain = self.update_domain(domain)
return domain return domain
def create_domain(self, domain): def create_domain(self, domain):
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['name'] = self.get_name() 'name': self.get_name(),
args['parentdomainid'] = self.get_parent_domain(key='id') 'parentdomainid': self.get_parent_domain(key='id'),
args['networkdomain'] = self.module.params.get('network_domain') 'networkdomain': self.module.params.get('network_domain')
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.createDomain(**args) res = self.cs.createDomain(**args)
if 'errortext' in res: if 'errortext' in res:
@ -200,12 +200,11 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
domain = res['domain'] domain = res['domain']
return domain return domain
def update_domain(self, domain): def update_domain(self, domain):
args = {} args = {
args['id'] = domain['id'] 'id': domain['id'],
args['networkdomain'] = self.module.params.get('network_domain') 'networkdomain': self.module.params.get('network_domain')
}
if self.has_changed(args, domain): if self.has_changed(args, domain):
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
@ -215,16 +214,16 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
domain = res['domain'] domain = res['domain']
return domain return domain
def absent_domain(self): def absent_domain(self):
domain = self.get_domain() domain = self.get_domain()
if domain: if domain:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
args = {} args = {
args['id'] = domain['id'] 'id': domain['id'],
args['cleanup'] = self.module.params.get('clean_up') 'cleanup': self.module.params.get('clean_up')
}
res = self.cs.deleteDomain(**args) res = self.cs.deleteDomain(**args)
if 'errortext' in res: if 'errortext' in res:
@ -236,15 +235,14 @@ class AnsibleCloudStackDomain(AnsibleCloudStack):
return domain return domain
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
path = dict(required=True), path=dict(required=True),
state = dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
network_domain = dict(default=None), network_domain=dict(),
clean_up = dict(type='bool', default=False), clean_up=dict(type='bool', default=False),
poll_async = dict(type='bool', default=True), poll_async=dict(type='bool', default=True),
)) ))
module = AnsibleModule( module = AnsibleModule(
@ -269,7 +267,6 @@ def main():
module.exit_json(**result) module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -56,6 +56,17 @@
- dom.path == "ROOT/{{ cs_resource_prefix }}_domain" - dom.path == "ROOT/{{ cs_resource_prefix }}_domain"
- dom.name == "{{ 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 - name: test create a subdomain in check mode
cs_domain: path=ROOT/{{ cs_resource_prefix }}_domain/{{ cs_resource_prefix }}_subdomain cs_domain: path=ROOT/{{ cs_resource_prefix }}_domain/{{ cs_resource_prefix }}_subdomain
register: dom register: dom

View file

@ -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/centurylink/clc_loadbalancer.py
lib/ansible/modules/cloud/cloudscale/cloudscale_server.py lib/ansible/modules/cloud/cloudscale/cloudscale_server.py
lib/ansible/modules/cloud/cloudstack/cs_configuration.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_facts.py
lib/ansible/modules/cloud/cloudstack/cs_instance.py lib/ansible/modules/cloud/cloudstack/cs_instance.py
lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py