From 6bd44ccb3a4bf8536f6bca9b81517d2b24c31f14 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 10 Nov 2011 20:45:55 -0500 Subject: Converting fixed ip calls to use instance objects Related to blueprint internal-uuids Change-Id: Ifad7a6ac39b0455f77602d9744f64425ebb3f49c --- nova/api/openstack/contrib/multinic.py | 25 ++++++++++++++------- nova/compute/api.py | 19 +++++++++++----- .../api/openstack/contrib/test_multinic_xs.py | 26 +++++++++++++--------- nova/tests/test_compute.py | 7 ++++++ 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/nova/api/openstack/contrib/multinic.py b/nova/api/openstack/contrib/multinic.py index 85ff423aa..26968646d 100644 --- a/nova/api/openstack/contrib/multinic.py +++ b/nova/api/openstack/contrib/multinic.py @@ -15,12 +15,13 @@ """The multinic extension.""" -from webob import exc import webob +from webob import exc +from nova.api.openstack import extensions from nova import compute +from nova import exception from nova import log as logging -from nova.api.openstack import extensions LOG = logging.getLogger("nova.api.multinic") @@ -63,6 +64,13 @@ class Multinic(extensions.ExtensionDescriptor): return actions + def _get_instance(self, context, instance_id): + try: + return self.compute_api.get(context, instance_id) + except exception.InstanceNotFound: + msg = _("Server not found") + raise exc.HTTPNotFound(msg) + def _add_fixed_ip(self, input_dict, req, id): """Adds an IP on a given network to an instance.""" @@ -71,10 +79,10 @@ class Multinic(extensions.ExtensionDescriptor): msg = _("Missing 'networkId' argument for addFixedIp") raise exc.HTTPUnprocessableEntity(explanation=msg) - # Add the fixed IP + context = req.environ['nova.context'] + instance = self._get_instance(context, id) network_id = input_dict['addFixedIp']['networkId'] - self.compute_api.add_fixed_ip(req.environ['nova.context'], id, - network_id) + self.compute_api.add_fixed_ip(context, instance, network_id) return webob.Response(status_int=202) def _remove_fixed_ip(self, input_dict, req, id): @@ -85,11 +93,12 @@ class Multinic(extensions.ExtensionDescriptor): msg = _("Missing 'address' argument for removeFixedIp") raise exc.HTTPUnprocessableEntity(explanation=msg) - # Remove the fixed IP + context = req.environ['nova.context'] + instance = self._get_instance(context, id) address = input_dict['removeFixedIp']['address'] + try: - self.compute_api.remove_fixed_ip(req.environ['nova.context'], id, - address) + self.compute_api.remove_fixed_ip(context, instance, address) except exceptions.FixedIpNotFoundForSpecificInstance: LOG.exception(_("Unable to find address %r") % address) raise exc.HTTPBadRequest() diff --git a/nova/compute/api.py b/nova/compute/api.py index 2b0db25f4..64884fbde 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1321,17 +1321,26 @@ class API(base.Base): "instance_type_id": new_instance_type['id']}}) @scheduler_api.reroute_compute("add_fixed_ip") - def add_fixed_ip(self, context, instance_id, network_id): + def add_fixed_ip(self, context, instance, network_id): """Add fixed_ip from specified network to given instance.""" - self._cast_compute_message('add_fixed_ip_to_instance', context, + #NOTE(bcwaldon): We need to use the integer id since the + # network manager doesn't support uuids + instance_id = instance['id'] + self._cast_compute_message('add_fixed_ip_to_instance', + context, instance_id, params=dict(network_id=network_id)) @scheduler_api.reroute_compute("remove_fixed_ip") - def remove_fixed_ip(self, context, instance_id, address): + def remove_fixed_ip(self, context, instance, address): """Remove fixed_ip from specified network to given instance.""" - self._cast_compute_message('remove_fixed_ip_from_instance', context, - instance_id, params=dict(address=address)) + #NOTE(bcwaldon): We need to use the integer id since the + # network manager doesn't support uuids + instance_id = instance['id'] + self._cast_compute_message('remove_fixed_ip_from_instance', + context, + instance_id, + params=dict(address=address)) #TODO(tr3buchet): how to run this in the correct zone? def add_network_to_project(self, context, project_id): diff --git a/nova/tests/api/openstack/contrib/test_multinic_xs.py b/nova/tests/api/openstack/contrib/test_multinic_xs.py index cecc4af4f..90999a384 100644 --- a/nova/tests/api/openstack/contrib/test_multinic_xs.py +++ b/nova/tests/api/openstack/contrib/test_multinic_xs.py @@ -23,20 +23,25 @@ from nova import test from nova.tests.api.openstack import fakes +UUID = '70f6db34-de8d-4fbd-aafb-4065bdfa6114' last_add_fixed_ip = (None, None) last_remove_fixed_ip = (None, None) -def compute_api_add_fixed_ip(self, context, instance_id, network_id): +def compute_api_add_fixed_ip(self, context, instance, network_id): global last_add_fixed_ip - last_add_fixed_ip = (instance_id, network_id) + last_add_fixed_ip = (instance['uuid'], network_id) -def compute_api_remove_fixed_ip(self, context, instance_id, address): +def compute_api_remove_fixed_ip(self, context, instance, address): global last_remove_fixed_ip - last_remove_fixed_ip = (instance_id, address) + last_remove_fixed_ip = (instance['uuid'], address) + + +def compute_api_get(self, context, instance_id): + return {'id': 1, 'uuid': instance_id} class FixedIpTest(test.TestCase): @@ -48,6 +53,7 @@ class FixedIpTest(test.TestCase): compute_api_add_fixed_ip) self.stubs.Set(compute.api.API, "remove_fixed_ip", compute_api_remove_fixed_ip) + self.stubs.Set(compute.api.API, 'get', compute_api_get) self.context = context.get_admin_context() def test_add_fixed_ip(self): @@ -55,21 +61,21 @@ class FixedIpTest(test.TestCase): last_add_fixed_ip = (None, None) body = dict(addFixedIp=dict(networkId='test_net')) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') + req = webob.Request.blank('/v1.1/123/servers/%s/action' % UUID) req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 202) - self.assertEqual(last_add_fixed_ip, ('test_inst', 'test_net')) + self.assertEqual(last_add_fixed_ip, (UUID, 'test_net')) def test_add_fixed_ip_no_network(self): global last_add_fixed_ip last_add_fixed_ip = (None, None) body = dict(addFixedIp=dict()) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') + req = webob.Request.blank('/v1.1/123/servers/%s/action' % UUID) req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' @@ -83,21 +89,21 @@ class FixedIpTest(test.TestCase): last_remove_fixed_ip = (None, None) body = dict(removeFixedIp=dict(address='10.10.10.1')) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') + req = webob.Request.blank('/v1.1/123/servers/%s/action' % UUID) req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 202) - self.assertEqual(last_remove_fixed_ip, ('test_inst', '10.10.10.1')) + self.assertEqual(last_remove_fixed_ip, (UUID, '10.10.10.1')) def test_remove_fixed_ip_no_address(self): global last_remove_fixed_ip last_remove_fixed_ip = (None, None) body = dict(removeFixedIp=dict()) - req = webob.Request.blank('/v1.1/123/servers/test_inst/action') + req = webob.Request.blank('/v1.1/123/servers/%s/action' % UUID) req.method = 'POST' req.body = json.dumps(body) req.headers['content-type'] = 'application/json' diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index 9cf580b1f..a4d86deec 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -1833,3 +1833,10 @@ class ComputeAPITestCase(BaseTestCase): i_ref = db.instance_get(self.context, instance_id) self.assertEqual(i_ref['name'], i_ref['uuid']) db.instance_destroy(self.context, i_ref['id']) + + def test_add_remove_fixed_ip(self): + instance_id = self._create_instance() + instance = self.compute_api.get(self.context, instance_id) + self.compute_api.add_fixed_ip(self.context, instance, '1') + self.compute_api.remove_fixed_ip(self.context, instance, '192.168.1.1') + self.compute_api.delete(self.context, instance) -- cgit