def test_notify_usage_exists(self): info = { "audit_period_beginning": "start", "audit_period_ending": "end", "bandwidth": "bw_usage", "image_meta": {}, "extra": "info", } instance = {"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") compute_utils.notify_about_instance_usage( self.context, instance, "exists", system_metadata={}, extra_usage_info=info ) self.mox.ReplayAll() self.conductor.notify_usage_exists( self.context, instance, system_metadata={}, extra_usage_info=dict(extra="info") )
def test_notify_usage_exists(self): info = { 'audit_period_beginning': 'start', 'audit_period_ending': 'end', 'bandwidth': 'bw_usage', 'image_meta': {}, 'extra': 'info', } instance = { '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') compute_utils.notify_about_instance_usage(self.context, instance, 'exists', system_metadata={}, extra_usage_info=info) self.mox.ReplayAll() self.conductor.notify_usage_exists(self.context, instance, system_metadata={}, extra_usage_info=dict(extra='info'))
def test_notify_usage_exists(self): info = { 'audit_period_beginning': 'start', 'audit_period_ending': 'end', 'bandwidth': 'bw_usage', 'image_meta': {}, 'extra': 'info', } instance = { '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') compute_utils.notify_about_instance_usage(self.context, instance, 'exists', system_metadata={}, extra_usage_info=info) self.mox.ReplayAll() self.conductor.notify_usage_exists(self.context, instance, system_metadata={}, extra_usage_info=dict(extra='info'))
def notify_usage_exists(notifier, context, instance_ref, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): """Generates 'exists' notification for an instance for usage auditing purposes. :param notifier: a messaging.Notifier :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. :param system_metadata: system_metadata DB entries for the instance, if not None. *NOTE*: Currently unused here in trunk, but needed for potential custom modifications. :param extra_usage_info: Dictionary containing extra values to add or override in the notification if not None. """ audit_start, audit_end = notifications.audit_period_bounds(current_period) bw = notifications.bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data) if system_metadata is None: system_metadata = utils.instance_sys_meta(instance_ref) # add image metadata to the notification: image_meta = notifications.image_meta(system_metadata) extra_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw, image_meta=image_meta) if extra_usage_info: extra_info.update(extra_usage_info) notify_about_instance_usage(notifier, context, instance_ref, 'exists', system_metadata=system_metadata, extra_usage_info=extra_info)
def notify_usage_exists(notifier, context, instance_ref, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): """Generates 'exists' unversioned legacy notification for an instance for usage auditing purposes. :param notifier: a messaging.Notifier :param context: request context for the current operation :param instance_ref: nova.objects.Instance object from which to report usage :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. :param system_metadata: system_metadata override for the instance. If None, the instance_ref.system_metadata will be used. :param extra_usage_info: Dictionary containing extra values to add or override in the notification if not None. """ audit_start, audit_end = notifications.audit_period_bounds(current_period) bw = notifications.bandwidth_usage(context, instance_ref, audit_start, ignore_missing_network_data) if system_metadata is None: system_metadata = utils.instance_sys_meta(instance_ref) # add image metadata to the notification: image_meta = notifications.image_meta(system_metadata) extra_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw, image_meta=image_meta) if extra_usage_info: extra_info.update(extra_usage_info) notify_about_instance_usage(notifier, context, instance_ref, 'exists', extra_usage_info=extra_info)
def notify_usage_exists(context, instance_ref, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): """Generates 'exists' notification for an instance for usage auditing purposes. :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. :param system_metadata: system_metadata DB entries for the instance, if not None. *NOTE*: Currently unused here in trunk, but needed for potential custom modifications. :param extra_usage_info: Dictionary containing extra values to add or override in the notification if not None. """ audit_start, audit_end = notifications.audit_period_bounds(current_period) bw = notifications.bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data) if system_metadata is None: try: if instance_ref.get('deleted'): with utils.temporary_mutation(context, read_deleted='yes'): system_metadata = db.instance_system_metadata_get( context, instance_ref.uuid) else: system_metadata = db.instance_system_metadata_get( context, instance_ref.uuid) except exception.NotFound: system_metadata = {} # add image metadata to the notification: image_meta = notifications.image_meta(system_metadata) extra_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw, image_meta=image_meta) if extra_usage_info: extra_info.update(extra_usage_info) notify_about_instance_usage(context, instance_ref, 'exists', system_metadata=system_metadata, extra_usage_info=extra_info)
def notify_usage_exists(notifier, context, instance_ref, host, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): """Generates 'exists' unversioned legacy and transformed notification for an instance for usage auditing purposes. :param notifier: a messaging.Notifier :param context: request context for the current operation :param instance_ref: nova.objects.Instance object from which to report usage :param host: the host emitting the notification :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. :param system_metadata: system_metadata override for the instance. If None, the instance_ref.system_metadata will be used. :param extra_usage_info: Dictionary containing extra values to add or override in the notification if not None. """ audit_start, audit_end = notifications.audit_period_bounds(current_period) bw = notifications.bandwidth_usage(context, instance_ref, audit_start, ignore_missing_network_data) if system_metadata is None: system_metadata = utils.instance_sys_meta(instance_ref) # add image metadata to the notification: image_meta = notifications.image_meta(system_metadata) extra_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw, image_meta=image_meta) if extra_usage_info: extra_info.update(extra_usage_info) notify_about_instance_usage(notifier, context, instance_ref, 'exists', extra_usage_info=extra_info) audit_period = instance_notification.AuditPeriodPayload( audit_period_beginning=audit_start, audit_period_ending=audit_end) bandwidth = [instance_notification.BandwidthPayload( network_name=label, in_bytes=b['bw_in'], out_bytes=b['bw_out']) for label, b in bw.items()] payload = instance_notification.InstanceExistsPayload( context=context, instance=instance_ref, audit_period=audit_period, bandwidth=bandwidth) notification = instance_notification.InstanceExistsNotification( context=context, priority=fields.NotificationPriority.INFO, publisher=notification_base.NotificationPublisher( host=host, source=fields.NotificationSource.COMPUTE), event_type=notification_base.EventType( object='instance', action=fields.NotificationAction.EXISTS), payload=payload) notification.emit(context)