From 9af40c167879096a8f0f209bde4e6c5cc9295b86 Mon Sep 17 00:00:00 2001 From: Alessio Ababilov Date: Mon, 16 Jul 2012 18:27:58 +0300 Subject: Implement network association in OS API Networks are associated with projects automatically during launch of an instance. The network is chosen rather randomly. This commit adds support for association of given network and project. DocImpact - when this lands, need to update openstack-manuals repo: doc/src/docbkx/openstack-api-site/src/wadls/compute-api/src/ext/os-networks.wadl Implements: blueprint os-api-network-associate Change-Id: Iafaf4a5ae3d3e16a6c649f1d7850fceba732efab --- nova/api/openstack/compute/contrib/networks.py | 35 +++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/compute/contrib/networks.py b/nova/api/openstack/compute/contrib/networks.py index ece331fbb..fb1d3d77e 100644 --- a/nova/api/openstack/compute/contrib/networks.py +++ b/nova/api/openstack/compute/contrib/networks.py @@ -17,6 +17,7 @@ # under the License. +import webob from webob import exc from nova.api.openstack import extensions @@ -113,6 +114,31 @@ class NetworkController(object): def create(self, req, id, body=None): raise exc.HTTPNotImplemented() + def add(self, req, body): + context = req.environ['nova.context'] + authorize(context) + if not body: + raise exc.HTTPUnprocessableEntity() + + network_id = body.get('id', None) + project_id = context.project_id + LOG.debug(_("Associating network %(network)s" + " with project %(project)s") % + {"network": network_id or "", + "project": project_id}) + try: + self.network_api.add_network_to_project( + context, project_id, network_id) + except Exception as ex: + msg = (_("Cannot associate network %(network)s" + " with project %(project)s: %(message)s") % + {"network": network_id or "", + "project": project_id, + "message": getattr(ex, "value", str(ex))}) + raise exc.HTTPBadRequest(explanation=msg) + + return webob.Response(status_int=202) + class Networks(extensions.ExtensionDescriptor): """Admin-only Network Management Extension""" @@ -124,7 +150,10 @@ class Networks(extensions.ExtensionDescriptor): def get_resources(self): member_actions = {'action': 'POST'} - res = extensions.ResourceExtension('os-networks', - NetworkController(), - member_actions=member_actions) + collection_actions = {'add': 'POST'} + res = extensions.ResourceExtension( + 'os-networks', + NetworkController(), + member_actions=member_actions, + collection_actions=collection_actions) return [res] -- cgit