2015-03-31 22:37:07 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
|
|
|
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
|
|
|
|
#
|
|
|
|
# This module is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This software is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import shade
|
2015-06-09 21:24:38 +02:00
|
|
|
HAS_SHADE = True
|
2015-03-31 22:37:07 +02:00
|
|
|
except ImportError:
|
2015-06-09 21:24:38 +02:00
|
|
|
HAS_SHADE = False
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_security_group
|
|
|
|
short_description: Add/Delete security groups from an OpenStack cloud.
|
|
|
|
extends_documentation_fragment: openstack
|
2015-06-17 00:56:24 +02:00
|
|
|
author: "Monty Taylor (@emonty)"
|
2015-03-31 22:37:07 +02:00
|
|
|
version_added: "2.0"
|
|
|
|
description:
|
|
|
|
- Add or Remove security groups from an OpenStack cloud.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2015-06-09 22:18:38 +02:00
|
|
|
- Name that has to be given to the security group. This module
|
|
|
|
requires that security group names be unique.
|
2015-03-31 22:37:07 +02:00
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Long description of the purpose of the security group
|
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
default: present
|
2015-06-19 19:45:12 +02:00
|
|
|
|
|
|
|
requirements: ["shade"]
|
2015-03-31 22:37:07 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a security group
|
2015-06-09 21:24:38 +02:00
|
|
|
- os_security_group:
|
|
|
|
cloud=mordred
|
2015-06-09 22:18:38 +02:00
|
|
|
state=present
|
2015-06-09 21:24:38 +02:00
|
|
|
name=foo
|
|
|
|
description=security group for foo servers
|
2015-06-09 22:18:38 +02:00
|
|
|
|
|
|
|
# Update the existing 'foo' security group description
|
|
|
|
- os_security_group:
|
|
|
|
cloud=mordred
|
|
|
|
state=present
|
|
|
|
name=foo
|
|
|
|
description=updated description for the foo security group
|
2015-03-31 22:37:07 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2015-06-09 21:24:38 +02:00
|
|
|
def _needs_update(module, secgroup):
|
|
|
|
"""Check for differences in the updatable values.
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-09 21:24:38 +02:00
|
|
|
NOTE: We don't currently allow name updates.
|
|
|
|
"""
|
|
|
|
if secgroup['description'] != module.params['description']:
|
|
|
|
return True
|
|
|
|
return False
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
|
2015-06-09 21:24:38 +02:00
|
|
|
def _system_state_change(module, secgroup):
|
|
|
|
state = module.params['state']
|
|
|
|
if state == 'present':
|
|
|
|
if not secgroup:
|
|
|
|
return True
|
|
|
|
return _needs_update(module, secgroup)
|
|
|
|
if state == 'absent' and secgroup:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2015-03-31 22:37:07 +02:00
|
|
|
argument_spec = openstack_full_argument_spec(
|
2015-06-09 21:24:38 +02:00
|
|
|
name=dict(required=True),
|
|
|
|
description=dict(default=None),
|
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
2015-03-31 22:37:07 +02:00
|
|
|
)
|
2015-06-09 21:24:38 +02:00
|
|
|
|
2015-03-31 22:37:07 +02:00
|
|
|
module_kwargs = openstack_module_kwargs()
|
2015-06-09 21:24:38 +02:00
|
|
|
module = AnsibleModule(argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
**module_kwargs)
|
|
|
|
|
|
|
|
if not HAS_SHADE:
|
|
|
|
module.fail_json(msg='shade is required for this module')
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
|
|
|
description = module.params['description']
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
cloud = shade.openstack_cloud(**module.params)
|
2015-06-09 21:24:38 +02:00
|
|
|
secgroup = cloud.get_security_group(name)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-09 21:24:38 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=_system_state_change(module, secgroup))
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if not secgroup:
|
|
|
|
secgroup = cloud.create_security_group(name, description)
|
2015-06-19 19:45:12 +02:00
|
|
|
module.exit_json(changed=True, id=secgroup['id'])
|
2015-06-09 21:24:38 +02:00
|
|
|
else:
|
|
|
|
if _needs_update(module, secgroup):
|
|
|
|
secgroup = cloud.update_security_group(
|
|
|
|
secgroup['id'], description=description)
|
2015-06-19 19:45:12 +02:00
|
|
|
module.exit_json(changed=True, id=secgroup['id'])
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False, id=secgroup['id'])
|
2015-06-09 21:24:38 +02:00
|
|
|
|
|
|
|
if state == 'absent':
|
2015-06-19 19:45:12 +02:00
|
|
|
if not secgroup:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
else:
|
2015-06-09 21:24:38 +02:00
|
|
|
cloud.delete_security_group(secgroup['id'])
|
2015-06-19 19:45:12 +02:00
|
|
|
module.exit_json(changed=True)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
except shade.OpenStackCloudException as e:
|
|
|
|
module.fail_json(msg=e.message)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.openstack import *
|
|
|
|
main()
|