1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

cs_securitygroup: remove CloudStackException dep (#31597)

- fixes pep8
- fixes docs
This commit is contained in:
René Moser 2017-10-11 20:58:08 +02:00 committed by GitHub
parent 0c416b5891
commit af6e89fbc8
2 changed files with 41 additions and 67 deletions

View file

@ -29,41 +29,32 @@ options:
description: description:
description: description:
- Description of the security group. - Description of the security group.
required: false
default: null
state: state:
description: description:
- State of the security group. - State of the security group.
required: false default: present
default: 'present' choices: [ present, absent ]
choices: [ 'present', 'absent' ]
domain: domain:
description: description:
- Domain the security group is related to. - Domain the security group is related to.
required: false
default: null
account: account:
description: description:
- Account the security group is related to. - Account the security group is related to.
required: false
default: null
project: project:
description: description:
- Name of the project the security group to be created in. - Name of the project the security group to be created in.
required: false
default: null
extends_documentation_fragment: cloudstack extends_documentation_fragment: cloudstack
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Create a security group - name: create a security group
- local_action: local_action:
module: cs_securitygroup module: cs_securitygroup
name: default name: default
description: default security group description: default security group
# Remove a security group - name: remove a security group
- local_action: local_action:
module: cs_securitygroup module: cs_securitygroup
name: default name: default
state: absent state: absent
@ -107,88 +98,77 @@ account:
type: string type: string
sample: example account sample: example account
''' '''
try:
from cs import CloudStackException
except ImportError:
pass # Handled in AnsibleCloudStack.__init__
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together
class AnsibleCloudStackSecurityGroup(AnsibleCloudStack): class AnsibleCloudStackSecurityGroup(AnsibleCloudStack):
def __init__(self, module): def __init__(self, module):
super(AnsibleCloudStackSecurityGroup, self).__init__(module) super(AnsibleCloudStackSecurityGroup, self).__init__(module)
self.security_group = None self.security_group = None
def get_security_group(self): def get_security_group(self):
if not self.security_group: if not self.security_group:
args = {} args = {
args['projectid'] = self.get_project(key='id') 'projectid': self.get_project(key='id'),
args['account'] = self.get_account(key='name') 'account': self.get_account(key='name'),
args['domainid'] = self.get_domain(key='id') 'domainid': self.get_domain(key='id'),
args['securitygroupname'] = self.module.params.get('name') 'securitygroupname': self.module.params.get('name'),
}
sgs = self.cs.listSecurityGroups(**args) sgs = self.query_api('listSecurityGroups', **args)
if sgs: if sgs:
self.security_group = sgs['securitygroup'][0] self.security_group = sgs['securitygroup'][0]
return self.security_group return self.security_group
def create_security_group(self): def create_security_group(self):
security_group = self.get_security_group() security_group = self.get_security_group()
if not security_group: if not security_group:
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['name'] = self.module.params.get('name') 'name': self.module.params.get('name'),
args['projectid'] = self.get_project(key='id') 'projectid': self.get_project(key='id'),
args['account'] = self.get_account(key='name') 'account': self.get_account(key='name'),
args['domainid'] = self.get_domain(key='id') 'domainid': self.get_domain(key='id'),
args['description'] = self.module.params.get('description') 'description': self.module.params.get('description'),
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.createSecurityGroup(**args) res = self.query_api('createSecurityGroup', **args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
security_group = res['securitygroup'] security_group = res['securitygroup']
return security_group return security_group
def remove_security_group(self): def remove_security_group(self):
security_group = self.get_security_group() security_group = self.get_security_group()
if security_group: if security_group:
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['name'] = self.module.params.get('name') 'name': self.module.params.get('name'),
args['projectid'] = self.get_project(key='id') 'projectid': self.get_project(key='id'),
args['account'] = self.get_account(key='name') 'account': self.get_account(key='name'),
args['domainid'] = self.get_domain(key='id') 'domainid': self.get_domain(key='id'),
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.deleteSecurityGroup(**args) self.query_api('deleteSecurityGroup', **args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
return security_group return security_group
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
name = dict(required=True), name=dict(required=True),
description = dict(default=None), description=dict(),
state = dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
project = dict(default=None), project=dict(),
account = dict(default=None), account=dict(),
domain = dict(default=None), domain=dict(),
)) ))
module = AnsibleModule( module = AnsibleModule(
@ -197,20 +177,15 @@ def main():
supports_check_mode=True supports_check_mode=True
) )
try: acs_sg = AnsibleCloudStackSecurityGroup(module)
acs_sg = AnsibleCloudStackSecurityGroup(module)
state = module.params.get('state') state = module.params.get('state')
if state in ['absent']: if state in ['absent']:
sg = acs_sg.remove_security_group() sg = acs_sg.remove_security_group()
else: else:
sg = acs_sg.create_security_group() sg = acs_sg.create_security_group()
result = acs_sg.get_result(sg)
except CloudStackException as e:
module.fail_json(msg='CloudStackException: %s' % str(e))
result = acs_sg.get_result(sg)
module.exit_json(**result) module.exit_json(**result)

View file

@ -68,7 +68,6 @@ 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
lib/ansible/modules/cloud/cloudstack/_cs_nic.py lib/ansible/modules/cloud/cloudstack/_cs_nic.py
lib/ansible/modules/cloud/cloudstack/cs_portforward.py lib/ansible/modules/cloud/cloudstack/cs_portforward.py
lib/ansible/modules/cloud/cloudstack/cs_securitygroup.py
lib/ansible/modules/cloud/cloudstack/cs_securitygroup_rule.py lib/ansible/modules/cloud/cloudstack/cs_securitygroup_rule.py
lib/ansible/modules/cloud/docker/_docker.py lib/ansible/modules/cloud/docker/_docker.py
lib/ansible/modules/cloud/docker/docker_container.py lib/ansible/modules/cloud/docker/docker_container.py