From c00fd06bb4fb7c81c9e601e524956e969bea9db2 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Wed, 12 Nov 2014 14:38:03 -0800 Subject: [PATCH] Make notify_usage_exists() take an Instance object This makes the notify_usage_exists() method for conductor take an Instance object instead of a dict primitive. Related to blueprint kilo-objects Change-Id: I932c46e20f1d203f9587a39381563d58a10a3485 --- nova/compute/manager.py | 6 ++-- nova/conductor/manager.py | 8 ++++- nova/conductor/rpcapi.py | 12 +++++-- nova/tests/unit/conductor/test_conductor.py | 38 ++++++++++----------- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 7ecdf87c99..72baf69919 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -2770,8 +2770,7 @@ class ComputeManager(manager.Manager): # TODO(jaypipes): Move generate_image_url() into the nova.image.api orig_image_ref_url = glance.generate_image_url(orig_image_ref) extra_usage_info = {'image_ref_url': orig_image_ref_url} - self.conductor_api.notify_usage_exists(context, - obj_base.obj_to_primitive(instance), + self.conductor_api.notify_usage_exists(context, instance, current_period=True, system_metadata=orig_sys_metadata, extra_usage_info=extra_usage_info) @@ -4113,8 +4112,7 @@ class ComputeManager(manager.Manager): :param clean_shutdown: give the GuestOS a chance to stop """ self.conductor_api.notify_usage_exists( - context, obj_base.obj_to_primitive(instance), - current_period=True) + context, instance, current_period=True) self._notify_about_instance_usage(context, instance, 'shelve.start') def update_task_state(task_state, expected_state=task_states.SHELVING): diff --git a/nova/conductor/manager.py b/nova/conductor/manager.py index 017aee0c27..89d8536905 100644 --- a/nova/conductor/manager.py +++ b/nova/conductor/manager.py @@ -79,7 +79,7 @@ class ConductorManager(manager.Manager): namespace. See the ComputeTaskManager class for details. """ - target = messaging.Target(version='2.0') + target = messaging.Target(version='2.1') def __init__(self, *args, **kwargs): super(ConductorManager, self).__init__(service_name='conductor', @@ -341,6 +341,12 @@ class ConductorManager(manager.Manager): def notify_usage_exists(self, context, instance, current_period, ignore_missing_network_data, system_metadata, extra_usage_info): + if not isinstance(instance, objects.Instance): + attrs = ['metadata', 'system_metadata'] + instance = objects.Instance._from_db_object(context, + objects.Instance(), + instance, + expected_attrs=attrs) compute_utils.notify_usage_exists(self.notifier, context, instance, current_period, ignore_missing_network_data, diff --git a/nova/conductor/rpcapi.py b/nova/conductor/rpcapi.py index 1be17e4ede..e0b01fe6ca 100644 --- a/nova/conductor/rpcapi.py +++ b/nova/conductor/rpcapi.py @@ -151,6 +151,8 @@ class ConductorAPI(object): * Remove instance_get_by_uuid() * Remove agent_build_get_by_triple() + * 2.1 - Make notify_usage_exists() take an instance object + ... Juno supports message version 2.0. So, any changes to existing methods in 2.x after that point should be done such that they can handle the version_cap being set to 2.0. @@ -322,13 +324,17 @@ class ConductorAPI(object): def notify_usage_exists(self, context, instance, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): - instance_p = jsonutils.to_primitive(instance) + if self.client.can_send_version('2.1'): + version = '2.1' + else: + version = '2.0' + instance = jsonutils.to_primitive(instance) system_metadata_p = jsonutils.to_primitive(system_metadata) extra_usage_info_p = jsonutils.to_primitive(extra_usage_info) - cctxt = self.client.prepare() + cctxt = self.client.prepare(version=version) return cctxt.call( context, 'notify_usage_exists', - instance=instance_p, + instance=instance, current_period=current_period, ignore_missing_network_data=ignore_missing_network_data, system_metadata=system_metadata_p, diff --git a/nova/tests/unit/conductor/test_conductor.py b/nova/tests/unit/conductor/test_conductor.py index 6fe007c6e1..9bb73791de 100644 --- a/nova/tests/unit/conductor/test_conductor.py +++ b/nova/tests/unit/conductor/test_conductor.py @@ -288,7 +288,10 @@ class _BaseTestCase(object): self.context, 'task', 'begin', 'end', 'host', 'errors', 'message') self.assertEqual(result, 'result') - def test_notify_usage_exists(self): + @mock.patch.object(notifications, 'audit_period_bounds') + @mock.patch.object(notifications, 'bandwidth_usage') + @mock.patch.object(compute_utils, 'notify_about_instance_usage') + def test_notify_usage_exists(self, mock_notify, mock_bw, mock_audit): info = { 'audit_period_beginning': 'start', 'audit_period_ending': 'end', @@ -296,30 +299,27 @@ class _BaseTestCase(object): 'image_meta': {}, 'extra': 'info', } - instance = { - 'system_metadata': [], - } + instance = objects.Instance(id=1, system_metadata={}) - self.mox.StubOutWithMock(notifications, 'audit_period_bounds') - self.mox.StubOutWithMock(notifications, 'bandwidth_usage') - self.mox.StubOutWithMock(compute_utils, 'notify_about_instance_usage') - - notifications.audit_period_bounds(False).AndReturn(('start', 'end')) - notifications.bandwidth_usage(instance, 'start', True).AndReturn( - 'bw_usage') - notifier = self.conductor_manager.notifier - compute_utils.notify_about_instance_usage(notifier, - self.context, instance, - 'exists', - system_metadata={}, - extra_usage_info=info) - - self.mox.ReplayAll() + mock_audit.return_value = ('start', 'end') + mock_bw.return_value = 'bw_usage' self.conductor.notify_usage_exists(self.context, instance, False, True, system_metadata={}, extra_usage_info=dict(extra='info')) + class MatchInstance(object): + def __eq__(self, thing): + return thing.id == instance.id + + notifier = self.conductor_manager.notifier + mock_audit.assert_called_once_with(False) + mock_bw.assert_called_once_with(MatchInstance(), 'start', True) + mock_notify.assert_called_once_with(notifier, self.context, + MatchInstance(), + 'exists', system_metadata={}, + extra_usage_info=info) + def test_security_groups_trigger_members_refresh(self): self.mox.StubOutWithMock(self.conductor_manager.security_group_api, 'trigger_members_refresh')