2015-03-31 19:31:42 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2015, René Moser <mail@renemoser.net>
|
2017-10-08 19:13:58 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-03-31 19:31:42 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['stableinterface'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2015-03-31 19:31:42 +00:00
|
|
|
DOCUMENTATION = '''
|
2016-12-08 05:34:16 +00:00
|
|
|
---
|
2015-03-31 19:31:42 +00:00
|
|
|
module: cs_securitygroup
|
|
|
|
short_description: Manages security groups on Apache CloudStack based clouds.
|
2016-12-08 05:34:16 +00:00
|
|
|
description:
|
|
|
|
- Create and remove security groups.
|
2015-03-31 19:31:42 +00:00
|
|
|
version_added: '2.0'
|
2015-06-16 18:32:39 +00:00
|
|
|
author: "René Moser (@resmo)"
|
2015-03-31 19:31:42 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the security group.
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Description of the security group.
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the security group.
|
2017-10-11 18:58:08 +00:00
|
|
|
default: present
|
|
|
|
choices: [ present, absent ]
|
2015-12-20 21:24:58 +00:00
|
|
|
domain:
|
|
|
|
description:
|
|
|
|
- Domain the security group is related to.
|
|
|
|
account:
|
|
|
|
description:
|
|
|
|
- Account the security group is related to.
|
2015-03-31 19:31:42 +00:00
|
|
|
project:
|
|
|
|
description:
|
|
|
|
- Name of the project the security group to be created in.
|
2015-05-05 13:53:55 +00:00
|
|
|
extends_documentation_fragment: cloudstack
|
2015-03-31 19:31:42 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2017-10-11 18:58:08 +00:00
|
|
|
- name: create a security group
|
|
|
|
local_action:
|
2015-03-31 19:31:42 +00:00
|
|
|
module: cs_securitygroup
|
|
|
|
name: default
|
|
|
|
description: default security group
|
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
- name: remove a security group
|
|
|
|
local_action:
|
2015-03-31 19:31:42 +00:00
|
|
|
module: cs_securitygroup
|
|
|
|
name: default
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
---
|
2015-08-17 06:30:11 +00:00
|
|
|
id:
|
|
|
|
description: UUID of the security group.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: a6f7a5fc-43f8-11e5-a151-feff819cdc9f
|
2015-03-31 19:31:42 +00:00
|
|
|
name:
|
|
|
|
description: Name of security group.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: app
|
|
|
|
description:
|
|
|
|
description: Description of security group.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: application security group
|
2015-12-20 21:24:58 +00:00
|
|
|
tags:
|
|
|
|
description: List of resource tags associated with the security group.
|
|
|
|
returned: success
|
|
|
|
type: dict
|
|
|
|
sample: '[ { "key": "foo", "value": "bar" } ]'
|
|
|
|
project:
|
|
|
|
description: Name of project the security group is related to.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: Production
|
|
|
|
domain:
|
|
|
|
description: Domain the security group is related to.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: example domain
|
|
|
|
account:
|
|
|
|
description: Account the security group is related to.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: example account
|
2015-03-31 19:31:42 +00:00
|
|
|
'''
|
2017-10-08 19:13:58 +00:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together
|
2015-03-31 19:31:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AnsibleCloudStackSecurityGroup(AnsibleCloudStack):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
2015-08-17 06:30:11 +00:00
|
|
|
super(AnsibleCloudStackSecurityGroup, self).__init__(module)
|
2015-03-31 19:31:42 +00:00
|
|
|
self.security_group = None
|
|
|
|
|
|
|
|
def get_security_group(self):
|
|
|
|
if not self.security_group:
|
2015-12-20 21:24:58 +00:00
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
args = {
|
|
|
|
'projectid': self.get_project(key='id'),
|
|
|
|
'account': self.get_account(key='name'),
|
|
|
|
'domainid': self.get_domain(key='id'),
|
|
|
|
'securitygroupname': self.module.params.get('name'),
|
|
|
|
}
|
|
|
|
sgs = self.query_api('listSecurityGroups', **args)
|
2015-03-31 19:31:42 +00:00
|
|
|
if sgs:
|
2015-12-20 21:24:58 +00:00
|
|
|
self.security_group = sgs['securitygroup'][0]
|
2015-03-31 19:31:42 +00:00
|
|
|
return self.security_group
|
|
|
|
|
|
|
|
def create_security_group(self):
|
|
|
|
security_group = self.get_security_group()
|
|
|
|
if not security_group:
|
|
|
|
self.result['changed'] = True
|
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
args = {
|
|
|
|
'name': self.module.params.get('name'),
|
|
|
|
'projectid': self.get_project(key='id'),
|
|
|
|
'account': self.get_account(key='name'),
|
|
|
|
'domainid': self.get_domain(key='id'),
|
|
|
|
'description': self.module.params.get('description'),
|
|
|
|
}
|
2015-03-31 19:31:42 +00:00
|
|
|
|
|
|
|
if not self.module.check_mode:
|
2017-10-11 18:58:08 +00:00
|
|
|
res = self.query_api('createSecurityGroup', **args)
|
2015-03-31 19:31:42 +00:00
|
|
|
security_group = res['securitygroup']
|
|
|
|
|
|
|
|
return security_group
|
|
|
|
|
|
|
|
def remove_security_group(self):
|
|
|
|
security_group = self.get_security_group()
|
|
|
|
if security_group:
|
|
|
|
self.result['changed'] = True
|
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
args = {
|
|
|
|
'name': self.module.params.get('name'),
|
|
|
|
'projectid': self.get_project(key='id'),
|
|
|
|
'account': self.get_account(key='name'),
|
|
|
|
'domainid': self.get_domain(key='id'),
|
|
|
|
}
|
2015-03-31 19:31:42 +00:00
|
|
|
|
|
|
|
if not self.module.check_mode:
|
2017-10-11 18:58:08 +00:00
|
|
|
self.query_api('deleteSecurityGroup', **args)
|
2015-03-31 19:31:42 +00:00
|
|
|
|
|
|
|
return security_group
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2015-09-22 14:28:14 +00:00
|
|
|
argument_spec = cs_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2017-10-11 18:58:08 +00:00
|
|
|
name=dict(required=True),
|
|
|
|
description=dict(),
|
|
|
|
state=dict(choices=['present', 'absent'], default='present'),
|
|
|
|
project=dict(),
|
|
|
|
account=dict(),
|
|
|
|
domain=dict(),
|
2015-09-22 14:28:14 +00:00
|
|
|
))
|
|
|
|
|
2015-03-31 19:31:42 +00:00
|
|
|
module = AnsibleModule(
|
2015-09-22 14:28:14 +00:00
|
|
|
argument_spec=argument_spec,
|
|
|
|
required_together=cs_required_together(),
|
2015-03-31 19:31:42 +00:00
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
acs_sg = AnsibleCloudStackSecurityGroup(module)
|
2015-03-31 19:31:42 +00:00
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
state = module.params.get('state')
|
|
|
|
if state in ['absent']:
|
|
|
|
sg = acs_sg.remove_security_group()
|
|
|
|
else:
|
|
|
|
sg = acs_sg.create_security_group()
|
2015-03-31 19:31:42 +00:00
|
|
|
|
2017-10-11 18:58:08 +00:00
|
|
|
result = acs_sg.get_result(sg)
|
2015-03-31 19:31:42 +00:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
2017-10-08 19:13:58 +00:00
|
|
|
|
2015-06-28 10:52:05 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|