From 3e932045985636005b959e266712b7e95b9393bd Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Mon, 14 Mar 2016 10:49:30 +0000 Subject: [PATCH] Add project parameter to os_network A cloud/domain admin should be able to create a network on any project it is granted to. This changes adds the possibility to pass either a project ID or project name. --- cloud/openstack/os_network.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cloud/openstack/os_network.py b/cloud/openstack/os_network.py index 59fc0e57e81..da0be212a01 100644 --- a/cloud/openstack/os_network.py +++ b/cloud/openstack/os_network.py @@ -82,6 +82,11 @@ options: required: false default: None version_added: "2.1" + project: + description: + - Project name or ID containing the network (name admin-only) + required: false + default: None requirements: ["shade"] ''' @@ -166,6 +171,7 @@ def main(): choices=['flat', 'vlan', 'vxlan', 'gre']), provider_segmentation_id=dict(required=False), state=dict(default='present', choices=['absent', 'present']), + project=dict(default=None) ) module_kwargs = openstack_module_kwargs() @@ -174,6 +180,11 @@ def main(): if not HAS_SHADE: module.fail_json(msg='shade is required for this module') + if (module.params['project'] and + StrictVersion(shade.__version__) < StrictVersion('1.6.0')): + module.fail_json(msg="To utilize project, the installed version of" + "the shade library MUST be >=1.6.0") + state = module.params['state'] name = module.params['name'] shared = module.params['shared'] @@ -182,10 +193,20 @@ def main(): provider_physical_network = module.params['provider_physical_network'] provider_network_type = module.params['provider_network_type'] provider_segmentation_id = module.params['provider_segmentation_id'] + project = module.params.pop('project') try: cloud = shade.openstack_cloud(**module.params) - net = cloud.get_network(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 + net = cloud.get_network(name, filters=filters) if state == 'present': if not net: @@ -201,7 +222,7 @@ def main(): module.fail_json(msg="Shade >= 1.5.0 required to use provider options") net = cloud.create_network(name, shared, admin_state_up, - external, provider) + external, provider, project_id) changed = True else: changed = False