Skip to content

Commit

Permalink
cloudstack: fix pep8 cs_domain
Browse files Browse the repository at this point in the history
  • Loading branch information
resmo committed May 28, 2017
1 parent 1778e23 commit 439f0be
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
65 changes: 31 additions & 34 deletions lib/ansible/modules/cloud/cloudstack/cs_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,26 @@
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):

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')
Expand All @@ -141,8 +145,9 @@ def _get_domain_internal(self, path=None):
elif not path.startswith('root/'):
path = "root/" + path

args = {}
args['listall'] = True
args = {
'listall': True
}

domains = self.cs.listDomains(**args)
if domains:
Expand All @@ -151,19 +156,16 @@ def _get_domain_internal(self, path=None):
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 /*
Expand All @@ -175,7 +177,6 @@ def get_parent_domain(self, key=None):
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:
Expand All @@ -184,28 +185,26 @@ def present_domain(self):
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:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
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:
Expand All @@ -215,16 +214,16 @@ def update_domain(self, domain):
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:
Expand All @@ -236,15 +235,14 @@ def absent_domain(self):
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(
Expand All @@ -269,7 +267,6 @@ def main():

module.exit_json(**result)

# import module snippets
from ansible.module_utils.basic import *

if __name__ == '__main__':
main()
11 changes: 11 additions & 0 deletions test/integration/targets/cs_domain/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion test/sanity/pep8/legacy-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 439f0be

Please sign in to comment.