From 682d97b7655c50c9898e677e1b2611d7e28f1c62 Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Mon, 14 Mar 2016 12:29:13 +0000 Subject: [PATCH] Create subnet by domain A cloud/domain admin should be able to create a subnet on any project it is granted on. This change adds the 'project' parameter that accepts either a name (admin-only) or id. --- .../modules/cloud/openstack/os_subnet.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/openstack/os_subnet.py b/lib/ansible/modules/cloud/openstack/os_subnet.py index 4d06425d2c9..c75e8059f0b 100644 --- a/lib/ansible/modules/cloud/openstack/os_subnet.py +++ b/lib/ansible/modules/cloud/openstack/os_subnet.py @@ -105,6 +105,11 @@ options: choices: ['dhcpv6-stateful', 'dhcpv6-stateless', 'slaac'] required: false default: None + project: + description: + - Project name or ID containing the subnet (name admin-only) + required: false + default: None requirements: - "python >= 2.6" - "shade" @@ -233,6 +238,7 @@ def main(): ipv6_ra_mode=dict(default=None, choice=ipv6_mode_choices), ipv6_address_mode=dict(default=None, choice=ipv6_mode_choices), state=dict(default='present', choices=['absent', 'present']), + project=dict(default=None) ) module_kwargs = openstack_module_kwargs() @@ -256,6 +262,7 @@ def main(): host_routes = module.params['host_routes'] ipv6_ra_mode = module.params['ipv6_ra_mode'] ipv6_a_mode = module.params['ipv6_address_mode'] + project = module.params.pop('project') # Check for required parameters when state == 'present' if state == 'present': @@ -272,7 +279,17 @@ def main(): try: cloud = shade.openstack_cloud(**module.params) - subnet = cloud.get_subnet(subnet_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'] + filters = {'tenant_id': project_id} + else: + project_id = None + filters = None + + subnet = cloud.get_subnet(subnet_name, filters=filters) if module.check_mode: module.exit_json(changed=_system_state_change(module, subnet, @@ -289,7 +306,8 @@ def main(): allocation_pools=pool, host_routes=host_routes, ipv6_ra_mode=ipv6_ra_mode, - ipv6_address_mode=ipv6_a_mode) + ipv6_address_mode=ipv6_a_mode, + tenant_id=project_id) changed = True else: if _needs_update(subnet, module, cloud):