Support project parameter for os_security_group module (#34472)

Many OpenStack modules provide the ability to register a resource in a
project other than the one being used to authenticate with, by adding a
project parameter to the module. Examples include os_network, os_subnet,
and os_router. This change adds a project parameter to the
os_security_group module.

Fixes: #34467
Fixes: #30292
This commit is contained in:
Mark Goddard 2018-08-22 18:52:25 +01:00 committed by ansibot
parent c4303804bf
commit 8522e6420e
2 changed files with 69 additions and 5 deletions

View file

@ -36,6 +36,11 @@ options:
- Should the resource be present or absent. - Should the resource be present or absent.
choices: [present, absent] choices: [present, absent]
default: present default: present
project:
description:
- Unique name or ID of the project.
required: false
version_added: "2.7"
availability_zone: availability_zone:
description: description:
- Ignored. Present for backwards compatibility - Ignored. Present for backwards compatibility
@ -55,6 +60,13 @@ EXAMPLES = '''
state: present state: present
name: foo name: foo
description: updated description for the foo security group description: updated description for the foo security group
# Create a security group for a given project
- os_security_group:
cloud: mordred
state: present
name: foo
project: myproj
''' '''
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -87,6 +99,7 @@ def main():
name=dict(required=True), name=dict(required=True),
description=dict(default=''), description=dict(default=''),
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
project=dict(default=None),
) )
module_kwargs = openstack_module_kwargs() module_kwargs = openstack_module_kwargs()
@ -97,10 +110,24 @@ def main():
name = module.params['name'] name = module.params['name']
state = module.params['state'] state = module.params['state']
description = module.params['description'] description = module.params['description']
project = module.params['project']
sdk, cloud = openstack_cloud_from_module(module) sdk, cloud = openstack_cloud_from_module(module)
try: try:
secgroup = cloud.get_security_group(name) if project is not None:
proj = cloud.get_project(project)
if proj is None:
module.fail_json(msg='Project %s could not be found' % project)
project_id = proj['id']
else:
project_id = cloud.current_project_id
if project_id:
filters = {'tenant_id': project_id}
else:
filters = None
secgroup = cloud.get_security_group(name, filters=filters)
if module.check_mode: if module.check_mode:
module.exit_json(changed=_system_state_change(module, secgroup)) module.exit_json(changed=_system_state_change(module, secgroup))
@ -108,7 +135,11 @@ def main():
changed = False changed = False
if state == 'present': if state == 'present':
if not secgroup: if not secgroup:
secgroup = cloud.create_security_group(name, description) kwargs = {}
if project_id:
kwargs['project_id'] = project_id
secgroup = cloud.create_security_group(name, description,
**kwargs)
changed = True changed = True
else: else:
if _needs_update(module, secgroup): if _needs_update(module, secgroup):

View file

@ -61,6 +61,11 @@ options:
- Should the resource be present or absent. - Should the resource be present or absent.
choices: [present, absent] choices: [present, absent]
default: present default: present
project:
description:
- Unique name or ID of the project.
required: false
version_added: "2.7"
availability_zone: availability_zone:
description: description:
- Ignored. Present for backwards compatibility - Ignored. Present for backwards compatibility
@ -114,6 +119,14 @@ EXAMPLES = '''
security_group: loadbalancer_sg security_group: loadbalancer_sg
protocol: 112 protocol: 112
remote_group: loadbalancer-node_sg remote_group: loadbalancer-node_sg
# Create a security group rule for a given project
- os_security_group_rule:
cloud: mordred
security_group: foo
protocol: icmp
remote_ip_prefix: 0.0.0.0/0
project: myproj
''' '''
RETURN = ''' RETURN = '''
@ -271,6 +284,7 @@ def main():
choices=['egress', 'ingress']), choices=['egress', 'ingress']),
state=dict(default='present', state=dict(default='present',
choices=['absent', 'present']), choices=['absent', 'present']),
project=dict(default=None),
) )
module_kwargs = openstack_module_kwargs( module_kwargs = openstack_module_kwargs(
@ -286,14 +300,29 @@ def main():
state = module.params['state'] state = module.params['state']
security_group = module.params['security_group'] security_group = module.params['security_group']
remote_group = module.params['remote_group'] remote_group = module.params['remote_group']
project = module.params['project']
changed = False changed = False
sdk, cloud = openstack_cloud_from_module(module) sdk, cloud = openstack_cloud_from_module(module)
try: try:
secgroup = cloud.get_security_group(security_group) if project is not None:
proj = cloud.get_project(project)
if proj is None:
module.fail_json(msg='Project %s could not be found' % project)
project_id = proj['id']
else:
project_id = cloud.current_project_id
if project_id:
filters = {'tenant_id': project_id}
else:
filters = None
secgroup = cloud.get_security_group(security_group, filters=filters)
if remote_group: if remote_group:
remotegroup = cloud.get_security_group(remote_group) remotegroup = cloud.get_security_group(remote_group,
filters=filters)
else: else:
remotegroup = {'id': None} remotegroup = {'id': None}
@ -307,6 +336,9 @@ def main():
rule = _find_matching_rule(module, secgroup, remotegroup) rule = _find_matching_rule(module, secgroup, remotegroup)
if not rule: if not rule:
kwargs = {}
if project_id:
kwargs['project_id'] = project_id
rule = cloud.create_security_group_rule( rule = cloud.create_security_group_rule(
secgroup['id'], secgroup['id'],
port_range_min=module.params['port_range_min'], port_range_min=module.params['port_range_min'],
@ -315,7 +347,8 @@ def main():
remote_ip_prefix=module.params['remote_ip_prefix'], remote_ip_prefix=module.params['remote_ip_prefix'],
remote_group_id=remotegroup['id'], remote_group_id=remotegroup['id'],
direction=module.params['direction'], direction=module.params['direction'],
ethertype=module.params['ethertype'] ethertype=module.params['ethertype'],
**kwargs
) )
changed = True changed = True
module.exit_json(changed=changed, rule=rule, id=rule['id']) module.exit_json(changed=changed, rule=rule, id=rule['id'])