Skip to content

Commit

Permalink
Make inject_network_info use objects
Browse files Browse the repository at this point in the history
Makes the inject_network_info path use instance object.

Related to blueprint compute-api-objects

Change-Id: I8d6b90cac0c1f638b623def319f676e95b416e09
  • Loading branch information
comstud authored and markmc committed Aug 29, 2013
1 parent 052e5fd commit ca989cd
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _inject_network_info(self, req, id, body):
context = req.environ['nova.context']
authorize(context, 'injectNetworkInfo')
try:
instance = self.compute_api.get(context, id)
instance = self.compute_api.get(context, id, want_objects=True)
self.compute_api.inject_network_info(context, instance)
except exception.InstanceNotFound:
raise exc.HTTPNotFound(_("Server not found"))
Expand Down
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/plugins/v3/admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _inject_network_info(self, req, id, body):
context = req.environ['nova.context']
authorize(context, 'inject_network_info')
try:
instance = self.compute_api.get(context, id)
instance = self.compute_api.get(context, id, want_objects=True)
self.compute_api.inject_network_info(context, instance)
except exception.InstanceNotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
Expand Down
6 changes: 5 additions & 1 deletion nova/cells/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CellsManager(manager.Manager):
Scheduling requests get passed to the scheduler class.
"""
RPC_API_VERSION = '1.22'
RPC_API_VERSION = '1.23'

def __init__(self, *args, **kwargs):
# Mostly for tests.
Expand Down Expand Up @@ -514,3 +514,7 @@ def confirm_resize(self, ctxt, instance):
def reset_network(self, ctxt, instance):
"""Reset networking for an instance in its cell."""
self.msg_runner.reset_network(ctxt, instance)

def inject_network_info(self, ctxt, instance):
"""Inject networking for an instance in its cell."""
self.msg_runner.inject_network_info(ctxt, instance)
9 changes: 9 additions & 0 deletions nova/cells/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,11 @@ def reset_network(self, message, instance):
self._call_compute_api_with_obj(message.ctxt, instance,
'reset_network')

def inject_network_info(self, message, instance):
"""Inject networking for an instance in its cell."""
self._call_compute_api_with_obj(message.ctxt, instance,
'inject_network_info')


class _BroadcastMessageMethods(_BaseMessageMethods):
"""These are the methods that can be called as a part of a broadcast
Expand Down Expand Up @@ -1683,6 +1688,10 @@ def reset_network(self, ctxt, instance):
"""Reset networking for an instance in its cell."""
self._instance_action(ctxt, instance, 'reset_network')

def inject_network_info(self, ctxt, instance):
"""Inject networking for an instance in its cell."""
self._instance_action(ctxt, instance, 'inject_network_info')

@staticmethod
def get_message_types():
return _CELL_MESSAGE_TYPE_TO_MESSAGE_CLS.keys()
Expand Down
9 changes: 9 additions & 0 deletions nova/cells/rpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class CellsAPI(rpc_proxy.RpcProxy):
1.20 - Adds resize_instance() and live_migrate_instance()
1.21 - Adds revert_resize() and confirm_resize()
1.22 - Adds reset_network()
1.23 - Adds inject_network_info()
'''
BASE_RPC_API_VERSION = '1.0'

Expand Down Expand Up @@ -569,3 +570,11 @@ def reset_network(self, ctxt, instance):
self.cast(ctxt,
self.make_msg('reset_network', instance=instance),
version='1.22')

def inject_network_info(self, ctxt, instance):
"""Inject networking for an instance."""
if not CONF.cells.enable:
return
self.cast(ctxt,
self.make_msg('inject_network_info', instance=instance),
version='1.23')
1 change: 1 addition & 0 deletions nova/compute/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,7 @@ def reset_network(self, context, instance):

@wrap_check_policy
@check_instance_lock
@check_instance_cell
def inject_network_info(self, context, instance):
"""Inject network info for the instance."""
self.compute_rpcapi.inject_network_info(context, instance=instance)
Expand Down
9 changes: 2 additions & 7 deletions nova/compute/cells_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ComputeRPCAPIRedirect(object):
'resume_instance', 'terminate_instance',
'soft_delete_instance', 'pause_instance',
'unpause_instance', 'revert_resize',
'confirm_resize', 'reset_network']
'confirm_resize', 'reset_network',
'inject_network_info']

def __init__(self, cells_rpcapi):
self.cells_rpcapi = cells_rpcapi
Expand Down Expand Up @@ -393,12 +394,6 @@ def unlock(self, context, instance):
super(ComputeCellsAPI, self).lock(context, instance)
self._cast_to_cells(context, instance, 'unlock')

@check_instance_cell
def inject_network_info(self, context, instance):
"""Inject network info for the instance."""
super(ComputeCellsAPI, self).inject_network_info(context, instance)
self._cast_to_cells(context, instance, 'inject_network_info')

@wrap_check_policy
@check_instance_cell
def attach_volume(self, context, instance, volume_id, device=None):
Expand Down
8 changes: 4 additions & 4 deletions nova/compute/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def instance_type_get(self, context, instance_type_id):
class ComputeManager(manager.SchedulerDependentManager):
"""Manages the running instances from creation to destruction."""

RPC_API_VERSION = '2.40'
RPC_API_VERSION = '2.41'

def __init__(self, compute_driver=None, *args, **kwargs):
"""Load configuration options and connect to the hypervisor."""
Expand Down Expand Up @@ -2969,9 +2969,9 @@ def add_fixed_ip_to_instance(self, context, network_id, instance):
self.network_api.add_fixed_ip_to_instance(context, instance,
network_id, conductor_api=self.conductor_api)

network_info = self._inject_network_info(context, instance=instance)
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)

# NOTE(russellb) We just want to bump updated_at. See bug 1143466.
Expand All @@ -2995,10 +2995,9 @@ def remove_fixed_ip_from_instance(self, context, address, instance):
self.network_api.remove_fixed_ip_from_instance(context, instance,
address, conductor_api=self.conductor_api)

network_info = self._inject_network_info(context,
instance=instance)
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)

# NOTE(russellb) We just want to bump updated_at. See bug 1143466.
Expand Down Expand Up @@ -3283,6 +3282,7 @@ def _inject_network_info(self, context, instance):
network_info)
return network_info

@object_compat
@wrap_instance_fault
def inject_network_info(self, context, instance):
"""Inject network info, but don't return the info."""
Expand Down
13 changes: 10 additions & 3 deletions nova/compute/rpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class ComputeAPI(nova.openstack.common.rpc.proxy.RpcProxy):
2.39 - Made revert_resize() and confirm_resize() take new-world
instance objects
2.40 - Made reset_network() take new-world instance object
2.41 - Make inject_network_info take new-world instance object
'''

#
Expand Down Expand Up @@ -425,10 +426,16 @@ def inject_file(self, ctxt, instance, path, file_contents):
topic=_compute_topic(self.topic, ctxt, None, instance))

def inject_network_info(self, ctxt, instance):
instance_p = jsonutils.to_primitive(instance)
if self.can_send_version('2.41'):
version = '2.41'
else:
instance = jsonutils.to_primitive(
objects_base.obj_to_primitive(instance))
version = '2.0'
self.cast(ctxt, self.make_msg('inject_network_info',
instance=instance_p),
topic=_compute_topic(self.topic, ctxt, None, instance))
instance=instance),
topic=_compute_topic(self.topic, ctxt, None, instance),
version=version)

def live_migration(self, ctxt, instance, dest, block_migration, host,
migrate_data=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,14 @@ def test_actions(self):
actions = ['pause', 'unpause', 'suspend', 'resume', 'migrate',
'resetNetwork', 'injectNetworkInfo', 'lock',
'unlock']
actions_not_objectified = ['injectNetworkInfo']
method_translations = {'migrate': 'resize',
'resetNetwork': 'reset_network',
'injectNetworkInfo': 'inject_network_info'}

for action in actions:
old_style = action in actions_not_objectified
method = method_translations.get(action)
self.mox.StubOutWithMock(self.compute_api, method or action)
self._test_action(action, method=method, objects=not old_style)
self._test_action(action, method=method)
# Re-mock this.
self.mox.StubOutWithMock(self.compute_api, 'get')

Expand All @@ -181,13 +179,10 @@ def test_actions_with_non_existed_instance(self):
actions = ['pause', 'unpause', 'suspend', 'resume',
'resetNetwork', 'injectNetworkInfo', 'lock',
'unlock', 'os-resetState']
actions_not_objectified = ['injectNetworkInfo']
body_map = {'os-resetState': {'state': 'active'}}
for action in actions:
old_style = action in actions_not_objectified
self._test_non_existing_instance(action,
body_map=body_map,
objects=not old_style)
body_map=body_map)
# Re-mock this.
self.mox.StubOutWithMock(self.compute_api, 'get')

Expand Down
14 changes: 3 additions & 11 deletions nova/tests/api/openstack/compute/plugins/v3/test_admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,12 @@ def test_actions(self):
actions = ['pause', 'unpause', 'suspend', 'resume', 'migrate',
'reset_network', 'inject_network_info', 'lock',
'unlock']
actions_not_objectified = ['inject_network_info']
method_translations = {'migrate': 'resize'}

for action in actions:
old_style = action in actions_not_objectified
method = method_translations.get(action)
self.mox.StubOutWithMock(self.compute_api, method or action)
self._test_action(action, method=method, objects=not old_style)
self._test_action(action, method=method)
# Re-mock this.
self.mox.StubOutWithMock(self.compute_api, 'get')

Expand Down Expand Up @@ -202,31 +200,25 @@ def test_actions_with_non_existed_instance(self):
actions = ['pause', 'unpause', 'suspend', 'resume', 'migrate',
'reset_network', 'inject_network_info', 'lock',
'unlock', 'reset_state', 'migrate_live']
actions_not_objectified = ['inject_network_info']
body_map = {'reset_state': {'state': 'active'},
'migrate_live': {'host': 'hostname',
'block_migration': False,
'disk_over_commit': False}}
for action in actions:
old_style = action in actions_not_objectified
self._test_non_existing_instance(action,
body_map=body_map,
objects=not old_style)
body_map=body_map)
# Re-mock this.
self.mox.StubOutWithMock(self.compute_api, 'get')

def test_actions_with_locked_instance(self):
actions = ['pause', 'unpause', 'suspend', 'resume', 'migrate',
'reset_network', 'inject_network_info']
method_translations = {'migrate': 'resize'}
actions_not_objectified = ['inject_network_info']

for action in actions:
old_style = action in actions_not_objectified
method = method_translations.get(action)
self.mox.StubOutWithMock(self.compute_api, method or action)
self._test_locked_instance(action, method=method,
objects=not old_style)
self._test_locked_instance(action, method=method)
# Re-mock this.
self.mox.StubOutWithMock(self.compute_api, 'get')

Expand Down
7 changes: 7 additions & 0 deletions nova/tests/cells/test_cells_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,3 +770,10 @@ def test_reset_network(self):
self.msg_runner.reset_network(self.ctxt, 'fake-instance')
self.mox.ReplayAll()
self.cells_manager.reset_network(self.ctxt, instance='fake-instance')

def test_inject_network_info(self):
self.mox.StubOutWithMock(self.msg_runner, 'inject_network_info')
self.msg_runner.inject_network_info(self.ctxt, 'fake-instance')
self.mox.ReplayAll()
self.cells_manager.inject_network_info(self.ctxt,
instance='fake-instance')
5 changes: 5 additions & 0 deletions nova/tests/cells/test_cells_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ class FakeMessage(object):
method_translations = {'revert_resize': 'revert_resize',
'confirm_resize': 'confirm_resize',
'reset_network': 'reset_network',
'inject_network_info': 'inject_network_info',
}
tgt_method = method_translations.get(method,
'%s_instance' % method)
Expand Down Expand Up @@ -1271,6 +1272,10 @@ def test_reset_network(self):
self._test_instance_action_method('reset_network',
(), {}, (), {}, False)

def test_inject_network_info(self):
self._test_instance_action_method('inject_network_info',
(), {}, (), {}, False)


class CellsBroadcastMethodsTestCase(test.TestCase):
"""Test case for _BroadcastMessageMethods class. Most of these
Expand Down
9 changes: 9 additions & 0 deletions nova/tests/cells/test_cells_rpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,12 @@ def test_reset_network(self):
expected_args = {'instance': 'fake-instance'}
self._check_result(call_info, 'reset_network',
expected_args, version='1.22')

def test_inject_network_info(self):
call_info = self._stub_rpc_method('cast', None)

self.cells_rpcapi.inject_network_info(self.fake_context,
'fake-instance')
expected_args = {'instance': 'fake-instance'}
self._check_result(call_info, 'inject_network_info',
expected_args, version='1.23')
3 changes: 2 additions & 1 deletion nova/tests/compute/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,8 @@ def fake_driver_inject_network(self, instance, network_info):

instance = jsonutils.to_primitive(self._create_fake_instance())
self.compute.run_instance(self.context, instance=instance)
self.compute.inject_network_info(self.context, instance=instance)
inst_obj = self._objectify(instance)
self.compute.inject_network_info(self.context, instance=inst_obj)
self.assertTrue(called['inject'])
self.compute.terminate_instance(self.context, instance=instance)

Expand Down
3 changes: 2 additions & 1 deletion nova/tests/compute/test_rpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def test_inject_file(self):

def test_inject_network_info(self):
self._test_compute_api('inject_network_info', 'cast',
instance=self.fake_instance)
instance=self.fake_instance,
version='2.41')

def test_live_migration(self):
self._test_compute_api('live_migration', 'cast',
Expand Down

0 comments on commit ca989cd

Please sign in to comment.