Merge "Convert add_fixed_ip_to_instance to objects"

This commit is contained in:
Jenkins
2014-02-19 04:57:24 +00:00
committed by Gerrit Code Review
7 changed files with 33 additions and 20 deletions

View File

@@ -35,9 +35,10 @@ class MultinicController(wsgi.Controller):
super(MultinicController, self).__init__(*args, **kwargs)
self.compute_api = compute.API()
def _get_instance(self, context, instance_id):
def _get_instance(self, context, instance_id, want_objects=False):
try:
return self.compute_api.get(context, instance_id)
return self.compute_api.get(context, instance_id,
want_objects=want_objects)
except exception.InstanceNotFound:
msg = _("Server not found")
raise exc.HTTPNotFound(msg)
@@ -53,7 +54,7 @@ class MultinicController(wsgi.Controller):
msg = _("Missing 'networkId' argument for addFixedIp")
raise exc.HTTPUnprocessableEntity(explanation=msg)
instance = self._get_instance(context, id)
instance = self._get_instance(context, id, want_objects=True)
network_id = body['addFixedIp']['networkId']
self.compute_api.add_fixed_ip(context, instance, network_id)
return webob.Response(status_int=202)

View File

@@ -45,7 +45,8 @@ class MultinicController(wsgi.Controller):
context = req.environ['nova.context']
authorize(context)
instance = common.get_instance(self.compute_api, context, id)
instance = common.get_instance(self.compute_api, context, id,
want_objects=True)
network_id = body['add_fixed_ip']['network_id']
self.compute_api.add_fixed_ip(context, instance, network_id)
return webob.Response(status_int=202)

View File

@@ -412,7 +412,7 @@ class ComputeVirtAPI(virtapi.VirtAPI):
class ComputeManager(manager.Manager):
"""Manages the running instances from creation to destruction."""
target = messaging.Target(version='3.11')
target = messaging.Target(version='3.12')
def __init__(self, compute_driver=None, *args, **kwargs):
"""Load configuration options and connect to the hypervisor."""
@@ -3321,6 +3321,7 @@ class ComputeManager(manager.Manager):
qr_error, instance=instance)
self._set_instance_error_state(context, instance['uuid'])
@object_compat
@wrap_exception()
@reverts_task_state
@wrap_instance_fault
@@ -3335,14 +3336,12 @@ class ComputeManager(manager.Manager):
self.network_api.add_fixed_ip_to_instance(context, instance,
network_id)
inst_obj = instance_obj.Instance._from_db_object(
context, instance_obj.Instance(), instance)
network_info = self._inject_network_info(context, inst_obj)
self.reset_network(context, inst_obj)
network_info = self._inject_network_info(context, instance)
self.reset_network(context, instance)
# NOTE(russellb) We just want to bump updated_at. See bug 1143466.
self._instance_update(context, instance['uuid'],
updated_at=timeutils.utcnow())
instance.updated_at = timeutils.utcnow()
instance.save()
self._notify_about_instance_usage(
context, instance, "create_ip.end", network_info=network_info)

View File

@@ -225,6 +225,7 @@ class ComputeAPI(object):
3.9 - Update rescue_instance() to take an instance object
3.10 - Added get_rdp_console method
3.11 - Update unrescue_instance() to take an object
3.12 - Update add_fixed_ip_to_instance() to take an object
'''
VERSION_ALIASES = {
@@ -280,13 +281,16 @@ class ComputeAPI(object):
slave_info=slave_info)
def add_fixed_ip_to_instance(self, ctxt, instance, network_id):
# NOTE(russellb) Havana compat
version = self._get_compat_version('3.0', '2.0')
instance_p = jsonutils.to_primitive(instance)
if self.client.can_send_version('3.12'):
version = '3.12'
else:
# NOTE(russellb) Havana compat
version = self._get_compat_version('3.0', '2.0')
instance = jsonutils.to_primitive(instance)
cctxt = self.client.prepare(server=_compute_host(None, instance),
version=version)
cctxt.cast(ctxt, 'add_fixed_ip_to_instance',
instance=instance_p, network_id=network_id)
instance=instance, network_id=network_id)
def attach_interface(self, ctxt, instance, network_id, port_id,
requested_ip):

View File

@@ -16,6 +16,7 @@
import webob
from nova import compute
from nova.objects import instance as instance_obj
from nova.openstack.common import jsonutils
from nova import test
from nova.tests.api.openstack import fakes
@@ -38,8 +39,14 @@ def compute_api_remove_fixed_ip(self, context, instance, address):
last_remove_fixed_ip = (instance['uuid'], address)
def compute_api_get(self, context, instance_id):
return {'id': 1, 'uuid': instance_id}
def compute_api_get(self, context, instance_id, want_objects=False):
instance = instance_obj.Instance()
instance.uuid = instance_id
instance.id = 1
instance.vm_state = 'fake'
instance.task_state = 'fake'
instance.obj_reset_changes()
return instance
class FixedIpTest(test.NoDBTestCase):

View File

@@ -3120,7 +3120,7 @@ class ComputeTestCase(BaseTestCase):
self.assertEqual(len(fake_notifier.NOTIFICATIONS), 0)
self.compute.add_fixed_ip_to_instance(self.context, network_id=1,
instance=instance)
instance=self._objectify(instance))
self.assertEqual(len(fake_notifier.NOTIFICATIONS), 2)
self.compute.terminate_instance(self.context,
@@ -8043,7 +8043,8 @@ class ComputeAPITestCase(BaseTestCase):
instance = self._create_fake_instance(params={'host': CONF.host})
self.stubs.Set(self.compute_api.network_api, 'deallocate_for_instance',
lambda *a, **kw: None)
self.compute_api.add_fixed_ip(self.context, instance, '1')
self.compute_api.add_fixed_ip(self.context, self._objectify(instance),
'1')
self.compute_api.remove_fixed_ip(self.context, instance, '192.168.1.1')
self.compute_api.delete(self.context, self._objectify(instance))

View File

@@ -104,7 +104,7 @@ class ComputeRpcAPITestCase(test.TestCase):
def test_add_fixed_ip_to_instance(self):
self._test_compute_api('add_fixed_ip_to_instance', 'cast',
instance=self.fake_instance, network_id='id')
instance=self.fake_instance, network_id='id', version='3.12')
# NOTE(russellb) Havana compat
self.flags(compute='havana', group='upgrade_levels')