diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 8c0d1d072885..ea99621da151 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -604,13 +604,15 @@ class ComputeManager(manager.SchedulerDependentManager): launched_at=utils.utcnow()) def _notify_about_instance_usage(self, instance, event_suffix, - usage_info=None, network_info=None): - if not usage_info: - usage_info = compute_utils.usage_from_instance(instance, - network_info=network_info) - notifier.notify('compute.%s' % self.host, - 'compute.instance.%s' % event_suffix, - notifier.INFO, usage_info) + network_info=None, + extra_usage_info=None): + # NOTE(sirp): The only thing this wrapper function does extra is handle + # the passing in of `self.host`. Ordinarily this will just be + # `FLAGS.host`, but `Manager`'s gets a chance to override this in its + # `__init__`. + compute_utils.notify_about_instance_usage( + instance, event_suffix, network_info=network_info, + extra_usage_info=extra_usage_info, host=self.host) def _deallocate_network(self, context, instance): if not FLAGS.stub_network: @@ -1299,11 +1301,11 @@ class ComputeManager(manager.SchedulerDependentManager): 'migration_id': migration_ref['id'], 'image': image}}) - usage_info = compute_utils.usage_from_instance(instance_ref, - new_instance_type=new_instance_type['name'], - new_instance_type_id=new_instance_type['id']) + extra_usage_info = dict(new_instance_type=new_instance_type['name'], + new_instance_type_id=new_instance_type['id']) + self._notify_about_instance_usage(instance_ref, "resize.prep.end", - usage_info=usage_info) + extra_usage_info=extra_usage_info) @exception.wrap_exception(notifier=notifier, publisher_id=publisher_id()) @checks_instance_lock @@ -1548,9 +1550,7 @@ class ComputeManager(manager.SchedulerDependentManager): vm_state=vm_states.SUSPENDED, task_state=None) - usage_info = compute_utils.usage_from_instance(instance_ref) - notifier.notify('compute.%s' % self.host, 'compute.instance.suspend', - notifier.INFO, usage_info) + self._notify_about_instance_usage(instance_ref, 'suspend') @exception.wrap_exception(notifier=notifier, publisher_id=publisher_id()) @checks_instance_lock @@ -1570,9 +1570,7 @@ class ComputeManager(manager.SchedulerDependentManager): vm_state=vm_states.ACTIVE, task_state=None) - usage_info = compute_utils.usage_from_instance(instance_ref) - notifier.notify('compute.%s' % self.host, 'compute.instance.resume', - notifier.INFO, usage_info) + self._notify_about_instance_usage(instance_ref, 'resume') @exception.wrap_exception(notifier=notifier, publisher_id=publisher_id()) @wrap_instance_fault diff --git a/nova/compute/utils.py b/nova/compute/utils.py index fd922501cc35..2384d48bc775 100644 --- a/nova/compute/utils.py +++ b/nova/compute/utils.py @@ -67,14 +67,13 @@ def notify_usage_exists(instance_ref, current_period=False): break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) - usage_info = usage_from_instance(instance_ref, - audit_period_beginning=str(audit_start), - audit_period_ending=str(audit_end), - bandwidth=bw) - notifier_api.notify('compute.%s' % FLAGS.host, - 'compute.instance.exists', - notifier_api.INFO, - usage_info) + + extra_usage_info = dict(audit_period_beginning=str(audit_start), + audit_period_ending=str(audit_end), + bandwidth=bw) + + notify_about_instance_usage( + instance_ref, 'exists', extra_usage_info=extra_usage_info) def legacy_network_info(network_model): @@ -192,7 +191,7 @@ def legacy_network_info(network_model): return network_info -def usage_from_instance(instance_ref, network_info=None, **kw): +def _usage_from_instance(instance_ref, network_info=None, **kw): def null_safe_str(s): return str(s) if s else '' @@ -220,3 +219,19 @@ def usage_from_instance(instance_ref, network_info=None, **kw): usage_info.update(kw) return usage_info + + +def notify_about_instance_usage(instance, event_suffix, network_info=None, + extra_usage_info=None, host=None): + if not host: + host = FLAGS.host + + if not extra_usage_info: + extra_usage_info = {} + + usage_info = _usage_from_instance( + instance, network_info=network_info, **extra_usage_info) + + notifier_api.notify('compute.%s' % host, + 'compute.instance.%s' % event_suffix, + notifier_api.INFO, usage_info) diff --git a/nova/tests/api/ec2/test_cloud.py b/nova/tests/api/ec2/test_cloud.py index b37b659ac7bd..43a1b37fff54 100644 --- a/nova/tests/api/ec2/test_cloud.py +++ b/nova/tests/api/ec2/test_cloud.py @@ -95,7 +95,7 @@ class CloudTestCase(test.TestCase): def dumb(*args, **kwargs): pass - self.stubs.Set(compute_utils, 'usage_from_instance', dumb) + self.stubs.Set(compute_utils, 'notify_about_instance_usage', dumb) # set up our cloud self.cloud = cloud.CloudController() self.flags(compute_scheduler_driver='nova.scheduler.' @@ -1560,7 +1560,7 @@ class CloudTestCase(test.TestCase): def dumb(*args, **kwargs): pass - self.stubs.Set(compute_utils, 'usage_from_instance', dumb) + self.stubs.Set(compute_utils, 'notify_about_instance_usage', dumb) # NOTE(comstud): Make 'cast' behave like a 'call' which will # ensure that operations complete self.stubs.Set(rpc, 'cast', rpc.call) diff --git a/nova/tests/api/ec2/test_ec2_validate.py b/nova/tests/api/ec2/test_ec2_validate.py index 183ec1d99eb3..86e943618790 100644 --- a/nova/tests/api/ec2/test_ec2_validate.py +++ b/nova/tests/api/ec2/test_ec2_validate.py @@ -47,7 +47,7 @@ class EC2ValidateTestCase(test.TestCase): def dumb(*args, **kwargs): pass - self.stubs.Set(compute_utils, 'usage_from_instance', dumb) + self.stubs.Set(compute_utils, 'notify_about_instance_usage', dumb) # set up our cloud self.cloud = cloud.CloudController()