summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-06-19 17:40:38 +0000
committerGerrit Code Review <review@openstack.org>2017-06-19 17:40:38 +0000
commitfad9e1a476f7d9d1a55e1b7d105177642e417fb9 (patch)
tree5eaa4f5dd7aa1cb93dbc27d40aac19c611986e0f
parente90e6f01e09d25fede4bf46285a038caeeecb270 (diff)
parent3b72d6058956b238d176d6473c2c538fd60526d5 (diff)
Merge "Add separate instance.create payload type"
-rw-r--r--doc/notification_samples/instance-create-end.json2
-rw-r--r--doc/notification_samples/instance-create-error.json2
-rw-r--r--doc/notification_samples/instance-create-start.json2
-rw-r--r--nova/compute/manager.py32
-rw-r--r--nova/compute/utils.py28
-rw-r--r--nova/notifications/objects/instance.py33
-rw-r--r--nova/tests/unit/compute/test_compute_mgr.py16
-rw-r--r--nova/tests/unit/compute/test_compute_utils.py33
-rw-r--r--nova/tests/unit/notifications/objects/test_notification.py2
9 files changed, 115 insertions, 35 deletions
diff --git a/doc/notification_samples/instance-create-end.json b/doc/notification_samples/instance-create-end.json
index 5f63588..963085f 100644
--- a/doc/notification_samples/instance-create-end.json
+++ b/doc/notification_samples/instance-create-end.json
@@ -66,7 +66,7 @@
66 "user_id":"fake", 66 "user_id":"fake",
67 "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" 67 "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c"
68 }, 68 },
69 "nova_object.name":"InstanceActionPayload", 69 "nova_object.name":"InstanceCreatePayload",
70 "nova_object.namespace":"nova", 70 "nova_object.namespace":"nova",
71 "nova_object.version":"1.2" 71 "nova_object.version":"1.2"
72 }, 72 },
diff --git a/doc/notification_samples/instance-create-error.json b/doc/notification_samples/instance-create-error.json
index 87e4c16..3df344d 100644
--- a/doc/notification_samples/instance-create-error.json
+++ b/doc/notification_samples/instance-create-error.json
@@ -63,7 +63,7 @@
63 "user_id":"fake", 63 "user_id":"fake",
64 "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" 64 "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c"
65 }, 65 },
66 "nova_object.name":"InstanceActionPayload", 66 "nova_object.name":"InstanceCreatePayload",
67 "nova_object.namespace":"nova", 67 "nova_object.namespace":"nova",
68 "nova_object.version":"1.2" 68 "nova_object.version":"1.2"
69 }, 69 },
diff --git a/doc/notification_samples/instance-create-start.json b/doc/notification_samples/instance-create-start.json
index 47a475b3..58c66ca 100644
--- a/doc/notification_samples/instance-create-start.json
+++ b/doc/notification_samples/instance-create-start.json
@@ -53,7 +53,7 @@
53 "user_id":"fake", 53 "user_id":"fake",
54 "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" 54 "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c"
55 }, 55 },
56 "nova_object.name":"InstanceActionPayload", 56 "nova_object.name":"InstanceCreatePayload",
57 "nova_object.namespace":"nova", 57 "nova_object.namespace":"nova",
58 "nova_object.version":"1.2" 58 "nova_object.version":"1.2"
59 }, 59 },
diff --git a/nova/compute/manager.py b/nova/compute/manager.py
index cd208fa..af5a906 100644
--- a/nova/compute/manager.py
+++ b/nova/compute/manager.py
@@ -1918,9 +1918,8 @@ class ComputeManager(manager.Manager):
1918 image_name = image.get('name') 1918 image_name = image.get('name')
1919 self._notify_about_instance_usage(context, instance, 'create.start', 1919 self._notify_about_instance_usage(context, instance, 'create.start',
1920 extra_usage_info={'image_name': image_name}) 1920 extra_usage_info={'image_name': image_name})
1921 compute_utils.notify_about_instance_action( 1921 compute_utils.notify_about_instance_create(
1922 context, instance, self.host, 1922 context, instance, self.host,
1923 action=fields.NotificationAction.CREATE,
1924 phase=fields.NotificationPhase.START) 1923 phase=fields.NotificationPhase.START)
1925 1924
1926 # NOTE(mikal): cache the keystone roles associated with the instance 1925 # NOTE(mikal): cache the keystone roles associated with the instance
@@ -1966,17 +1965,15 @@ class ComputeManager(manager.Manager):
1966 with excutils.save_and_reraise_exception(): 1965 with excutils.save_and_reraise_exception():
1967 self._notify_about_instance_usage(context, instance, 1966 self._notify_about_instance_usage(context, instance,
1968 'create.error', fault=e) 1967 'create.error', fault=e)
1969 compute_utils.notify_about_instance_action( 1968 compute_utils.notify_about_instance_create(
1970 context, instance, self.host, 1969 context, instance, self.host,
1971 action=fields.NotificationAction.CREATE,
1972 phase=fields.NotificationPhase.ERROR, exception=e) 1970 phase=fields.NotificationPhase.ERROR, exception=e)
1973 except exception.ComputeResourcesUnavailable as e: 1971 except exception.ComputeResourcesUnavailable as e:
1974 LOG.debug(e.format_message(), instance=instance) 1972 LOG.debug(e.format_message(), instance=instance)
1975 self._notify_about_instance_usage(context, instance, 1973 self._notify_about_instance_usage(context, instance,
1976 'create.error', fault=e) 1974 'create.error', fault=e)
1977 compute_utils.notify_about_instance_action( 1975 compute_utils.notify_about_instance_create(
1978 context, instance, self.host, 1976 context, instance, self.host,
1979 action=fields.NotificationAction.CREATE,
1980 phase=fields.NotificationPhase.ERROR, exception=e) 1977 phase=fields.NotificationPhase.ERROR, exception=e)
1981 raise exception.RescheduledException( 1978 raise exception.RescheduledException(
1982 instance_uuid=instance.uuid, reason=e.format_message()) 1979 instance_uuid=instance.uuid, reason=e.format_message())
@@ -1985,9 +1982,8 @@ class ComputeManager(manager.Manager):
1985 LOG.debug(e.format_message(), instance=instance) 1982 LOG.debug(e.format_message(), instance=instance)
1986 self._notify_about_instance_usage(context, instance, 1983 self._notify_about_instance_usage(context, instance,
1987 'create.error', fault=e) 1984 'create.error', fault=e)
1988 compute_utils.notify_about_instance_action( 1985 compute_utils.notify_about_instance_create(
1989 context, instance, self.host, 1986 context, instance, self.host,
1990 action=fields.NotificationAction.CREATE,
1991 phase=fields.NotificationPhase.ERROR, exception=e) 1987 phase=fields.NotificationPhase.ERROR, exception=e)
1992 except (exception.FixedIpLimitExceeded, 1988 except (exception.FixedIpLimitExceeded,
1993 exception.NoMoreNetworks, exception.NoMoreFixedIps) as e: 1989 exception.NoMoreNetworks, exception.NoMoreFixedIps) as e:
@@ -1995,9 +1991,8 @@ class ComputeManager(manager.Manager):
1995 instance=instance) 1991 instance=instance)
1996 self._notify_about_instance_usage(context, instance, 1992 self._notify_about_instance_usage(context, instance,
1997 'create.error', fault=e) 1993 'create.error', fault=e)
1998 compute_utils.notify_about_instance_action( 1994 compute_utils.notify_about_instance_create(
1999 context, instance, self.host, 1995 context, instance, self.host,
2000 action=fields.NotificationAction.CREATE,
2001 phase=fields.NotificationPhase.ERROR, exception=e) 1996 phase=fields.NotificationPhase.ERROR, exception=e)
2002 msg = _('Failed to allocate the network(s) with error %s, ' 1997 msg = _('Failed to allocate the network(s) with error %s, '
2003 'not rescheduling.') % e.format_message() 1998 'not rescheduling.') % e.format_message()
@@ -2011,9 +2006,8 @@ class ComputeManager(manager.Manager):
2011 instance=instance) 2006 instance=instance)
2012 self._notify_about_instance_usage(context, instance, 2007 self._notify_about_instance_usage(context, instance,
2013 'create.error', fault=e) 2008 'create.error', fault=e)
2014 compute_utils.notify_about_instance_action( 2009 compute_utils.notify_about_instance_create(
2015 context, instance, self.host, 2010 context, instance, self.host,
2016 action=fields.NotificationAction.CREATE,
2017 phase=fields.NotificationPhase.ERROR, exception=e) 2011 phase=fields.NotificationPhase.ERROR, exception=e)
2018 msg = _('Failed to allocate the network(s), not rescheduling.') 2012 msg = _('Failed to allocate the network(s), not rescheduling.')
2019 raise exception.BuildAbortException(instance_uuid=instance.uuid, 2013 raise exception.BuildAbortException(instance_uuid=instance.uuid,
@@ -2029,18 +2023,16 @@ class ComputeManager(manager.Manager):
2029 exception.InvalidInput) as e: 2023 exception.InvalidInput) as e:
2030 self._notify_about_instance_usage(context, instance, 2024 self._notify_about_instance_usage(context, instance,
2031 'create.error', fault=e) 2025 'create.error', fault=e)
2032 compute_utils.notify_about_instance_action( 2026 compute_utils.notify_about_instance_create(
2033 context, instance, self.host, 2027 context, instance, self.host,
2034 action=fields.NotificationAction.CREATE,
2035 phase=fields.NotificationPhase.ERROR, exception=e) 2028 phase=fields.NotificationPhase.ERROR, exception=e)
2036 raise exception.BuildAbortException(instance_uuid=instance.uuid, 2029 raise exception.BuildAbortException(instance_uuid=instance.uuid,
2037 reason=e.format_message()) 2030 reason=e.format_message())
2038 except Exception as e: 2031 except Exception as e:
2039 self._notify_about_instance_usage(context, instance, 2032 self._notify_about_instance_usage(context, instance,
2040 'create.error', fault=e) 2033 'create.error', fault=e)
2041 compute_utils.notify_about_instance_action( 2034 compute_utils.notify_about_instance_create(
2042 context, instance, self.host, 2035 context, instance, self.host,
2043 action=fields.NotificationAction.CREATE,
2044 phase=fields.NotificationPhase.ERROR, exception=e) 2036 phase=fields.NotificationPhase.ERROR, exception=e)
2045 raise exception.RescheduledException( 2037 raise exception.RescheduledException(
2046 instance_uuid=instance.uuid, reason=six.text_type(e)) 2038 instance_uuid=instance.uuid, reason=six.text_type(e))
@@ -2073,18 +2065,16 @@ class ComputeManager(manager.Manager):
2073 with excutils.save_and_reraise_exception(): 2065 with excutils.save_and_reraise_exception():
2074 self._notify_about_instance_usage(context, instance, 2066 self._notify_about_instance_usage(context, instance,
2075 'create.error', fault=e) 2067 'create.error', fault=e)
2076 compute_utils.notify_about_instance_action( 2068 compute_utils.notify_about_instance_create(
2077 context, instance, self.host, 2069 context, instance, self.host,
2078 action=fields.NotificationAction.CREATE,
2079 phase=fields.NotificationPhase.ERROR, exception=e) 2070 phase=fields.NotificationPhase.ERROR, exception=e)
2080 2071
2081 self._update_scheduler_instance_info(context, instance) 2072 self._update_scheduler_instance_info(context, instance)
2082 self._notify_about_instance_usage(context, instance, 'create.end', 2073 self._notify_about_instance_usage(context, instance, 'create.end',
2083 extra_usage_info={'message': _('Success')}, 2074 extra_usage_info={'message': _('Success')},
2084 network_info=network_info) 2075 network_info=network_info)
2085 compute_utils.notify_about_instance_action(context, instance, 2076 compute_utils.notify_about_instance_create(context, instance,
2086 self.host, action=fields.NotificationAction.CREATE, 2077 self.host, phase=fields.NotificationPhase.END)
2087 phase=fields.NotificationPhase.END)
2088 2078
2089 @contextlib.contextmanager 2079 @contextlib.contextmanager
2090 def _build_resources(self, context, instance, requested_networks, 2080 def _build_resources(self, context, instance, requested_networks,
diff --git a/nova/compute/utils.py b/nova/compute/utils.py
index 784f56b..23ff7a8 100644
--- a/nova/compute/utils.py
+++ b/nova/compute/utils.py
@@ -365,6 +365,34 @@ def notify_about_instance_action(context, instance, host, action, phase=None,
365 notification.emit(context) 365 notification.emit(context)
366 366
367 367
368def notify_about_instance_create(context, instance, host, phase=None,
369 binary='nova-compute', exception=None):
370 """Send versioned notification about instance creation
371
372 :param context: the request context
373 :param instance: the instance being created
374 :param host: the host emitting the notification
375 :param phase: the phase of the creation
376 :param binary: the binary emitting the notification
377 :param exception: the thrown exception (used in error notifications)
378 """
379 fault, priority = _get_fault_and_priority_from_exc(exception)
380 payload = instance_notification.InstanceCreatePayload(
381 instance=instance,
382 fault=fault)
383 notification = instance_notification.InstanceCreateNotification(
384 context=context,
385 priority=priority,
386 publisher=notification_base.NotificationPublisher(
387 host=host, binary=binary),
388 event_type=notification_base.EventType(
389 object='instance',
390 action=fields.NotificationAction.CREATE,
391 phase=phase),
392 payload=payload)
393 notification.emit(context)
394
395
368def notify_about_volume_attach_detach(context, instance, host, action, phase, 396def notify_about_volume_attach_detach(context, instance, host, action, phase,
369 binary='nova-compute', volume_id=None, 397 binary='nova-compute', volume_id=None,
370 exception=None): 398 exception=None):
diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py
index d61d627..0f27bc4 100644
--- a/nova/notifications/objects/instance.py
+++ b/nova/notifications/objects/instance.py
@@ -154,6 +154,23 @@ class InstanceActionVolumeSwapPayload(InstanceActionPayload):
154 154
155 155
156@nova_base.NovaObjectRegistry.register_notification 156@nova_base.NovaObjectRegistry.register_notification
157class InstanceCreatePayload(InstanceActionPayload):
158 # No SCHEMA as all the additional fields are calculated
159
160 # Version 1.2: Initial version. It starts at 1.2 to match with the version
161 # of the InstanceActionPayload at the time when this specific
162 # payload is created as a child of it so that the
163 # instance.create notification using this new payload does not
164 # have decreasing version.
165 VERSION = '1.2'
166
167 def __init__(self, instance, fault):
168 super(InstanceCreatePayload, self).__init__(
169 instance=instance,
170 fault=fault)
171
172
173@nova_base.NovaObjectRegistry.register_notification
157class InstanceUpdatePayload(InstancePayload): 174class InstanceUpdatePayload(InstancePayload):
158 # Version 1.0: Initial version 175 # Version 1.0: Initial version
159 # Version 1.1: locked and display_description added to InstancePayload 176 # Version 1.1: locked and display_description added to InstancePayload
@@ -339,9 +356,6 @@ class InstanceStateUpdatePayload(base.NotificationPayloadBase):
339# @base.notification_sample('instance-unrescue-end.json') 356# @base.notification_sample('instance-unrescue-end.json')
340@base.notification_sample('instance-unshelve-start.json') 357@base.notification_sample('instance-unshelve-start.json')
341@base.notification_sample('instance-unshelve-end.json') 358@base.notification_sample('instance-unshelve-end.json')
342@base.notification_sample('instance-create-start.json')
343@base.notification_sample('instance-create-end.json')
344@base.notification_sample('instance-create-error.json')
345@nova_base.NovaObjectRegistry.register_notification 359@nova_base.NovaObjectRegistry.register_notification
346class InstanceActionNotification(base.NotificationBase): 360class InstanceActionNotification(base.NotificationBase):
347 # Version 1.0: Initial version 361 # Version 1.0: Initial version
@@ -389,3 +403,16 @@ class InstanceActionVolumeNotification(base.NotificationBase):
389 fields = { 403 fields = {
390 'payload': fields.ObjectField('InstanceActionVolumePayload') 404 'payload': fields.ObjectField('InstanceActionVolumePayload')
391 } 405 }
406
407
408@base.notification_sample('instance-create-start.json')
409@base.notification_sample('instance-create-end.json')
410@base.notification_sample('instance-create-error.json')
411@nova_base.NovaObjectRegistry.register_notification
412class InstanceCreateNotification(base.NotificationBase):
413 # Version 1.0: Initial version
414 VERSION = '1.0'
415
416 fields = {
417 'payload': fields.ObjectField('InstanceCreatePayload')
418 }
diff --git a/nova/tests/unit/compute/test_compute_mgr.py b/nova/tests/unit/compute/test_compute_mgr.py
index 2d42285..08b0cdc 100644
--- a/nova/tests/unit/compute/test_compute_mgr.py
+++ b/nova/tests/unit/compute/test_compute_mgr.py
@@ -4360,7 +4360,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
4360 '_shutdown_instance'), 4360 '_shutdown_instance'),
4361 mock.patch.object(self.compute, 4361 mock.patch.object(self.compute,
4362 '_validate_instance_group_policy'), 4362 '_validate_instance_group_policy'),
4363 mock.patch('nova.compute.utils.notify_about_instance_action') 4363 mock.patch('nova.compute.utils.notify_about_instance_create')
4364 ) as (spawn, save, 4364 ) as (spawn, save,
4365 _build_networks_for_instance, _notify_about_instance_usage, 4365 _build_networks_for_instance, _notify_about_instance_usage,
4366 _shutdown_instance, _validate_instance_group_policy, 4366 _shutdown_instance, _validate_instance_group_policy,
@@ -4387,9 +4387,9 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
4387 4387
4388 mock_notify.assert_has_calls([ 4388 mock_notify.assert_has_calls([
4389 mock.call(self.context, self.instance, 'fake-mini', 4389 mock.call(self.context, self.instance, 'fake-mini',
4390 action='create', phase='start'), 4390 phase='start'),
4391 mock.call(self.context, self.instance, 'fake-mini', 4391 mock.call(self.context, self.instance, 'fake-mini',
4392 action='create', phase='error', exception=exc)]) 4392 phase='error', exception=exc)])
4393 4393
4394 save.assert_has_calls([ 4394 save.assert_has_calls([
4395 mock.call(), 4395 mock.call(),
@@ -4797,10 +4797,10 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
4797 self.context, mock.sentinel.instance, 4797 self.context, mock.sentinel.instance,
4798 requested_networks=requested_networks) 4798 requested_networks=requested_networks)
4799 4799
4800 @mock.patch('nova.compute.utils.notify_about_instance_action') 4800 @mock.patch('nova.compute.utils.notify_about_instance_create')
4801 @mock.patch.object(manager.ComputeManager, '_instance_update') 4801 @mock.patch.object(manager.ComputeManager, '_instance_update')
4802 def test_launched_at_in_create_end_notification(self, 4802 def test_launched_at_in_create_end_notification(self,
4803 mock_instance_update, mock_notify_instance_action): 4803 mock_instance_update, mock_notify_instance_create):
4804 4804
4805 def fake_notify(*args, **kwargs): 4805 def fake_notify(*args, **kwargs):
4806 if args[2] == 'create.end': 4806 if args[2] == 'create.end':
@@ -4829,11 +4829,11 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
4829 mock_notify.call_count - 1] 4829 mock_notify.call_count - 1]
4830 self.assertEqual(expected_call, create_end_call) 4830 self.assertEqual(expected_call, create_end_call)
4831 4831
4832 mock_notify_instance_action.assert_has_calls([ 4832 mock_notify_instance_create.assert_has_calls([
4833 mock.call(self.context, self.instance, 'fake-mini', 4833 mock.call(self.context, self.instance, 'fake-mini',
4834 action='create', phase='start'), 4834 phase='start'),
4835 mock.call(self.context, self.instance, 'fake-mini', 4835 mock.call(self.context, self.instance, 'fake-mini',
4836 action='create', phase='end')]) 4836 phase='end')])
4837 4837
4838 def test_access_ip_set_when_instance_set_to_active(self): 4838 def test_access_ip_set_when_instance_set_to_active(self):
4839 4839
diff --git a/nova/tests/unit/compute/test_compute_utils.py b/nova/tests/unit/compute/test_compute_utils.py
index 2802263..d944772 100644
--- a/nova/tests/unit/compute/test_compute_utils.py
+++ b/nova/tests/unit/compute/test_compute_utils.py
@@ -518,6 +518,39 @@ class UsageInfoTestCase(test.TestCase):
518 518
519 self.assertEqual(payload['image_uuid'], uuids.fake_image_ref) 519 self.assertEqual(payload['image_uuid'], uuids.fake_image_ref)
520 520
521 def test_notify_about_instance_create(self):
522 instance = create_instance(self.context)
523
524 compute_utils.notify_about_instance_create(
525 self.context,
526 instance,
527 host='fake-compute',
528 phase='start')
529
530 self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS))
531 notification = fake_notifier.VERSIONED_NOTIFICATIONS[0]
532
533 self.assertEqual('INFO', notification['priority'])
534 self.assertEqual('instance.create.start', notification['event_type'])
535 self.assertEqual('nova-compute:fake-compute',
536 notification['publisher_id'])
537
538 payload = notification['payload']['nova_object.data']
539 self.assertEqual('fake', payload['tenant_id'])
540 self.assertEqual('fake', payload['user_id'])
541 self.assertEqual(instance['uuid'], payload['uuid'])
542
543 flavorid = flavors.get_flavor_by_name('m1.tiny')['flavorid']
544 flavor = payload['flavor']['nova_object.data']
545 self.assertEqual(flavorid, str(flavor['flavorid']))
546
547 for attr in ('display_name', 'created_at', 'launched_at',
548 'state', 'task_state', 'display_description', 'locked',
549 'auto_disk_config'):
550 self.assertIn(attr, payload, "Key %s not in payload" % attr)
551
552 self.assertEqual(uuids.fake_image_ref, payload['image_uuid'])
553
521 def test_notify_about_volume_swap(self): 554 def test_notify_about_volume_swap(self):
522 instance = create_instance(self.context) 555 instance = create_instance(self.context)
523 556
diff --git a/nova/tests/unit/notifications/objects/test_notification.py b/nova/tests/unit/notifications/objects/test_notification.py
index 3d75391..70b1860 100644
--- a/nova/tests/unit/notifications/objects/test_notification.py
+++ b/nova/tests/unit/notifications/objects/test_notification.py
@@ -381,6 +381,8 @@ notification_object_data = {
381 'InstanceActionVolumeSwapNotification': 381 'InstanceActionVolumeSwapNotification':
382 '1.0-a73147b93b520ff0061865849d3dfa56', 382 '1.0-a73147b93b520ff0061865849d3dfa56',
383 'InstanceActionVolumeSwapPayload': '1.2-d7925b763e0795f8e5c1aa0e95bd67bd', 383 'InstanceActionVolumeSwapPayload': '1.2-d7925b763e0795f8e5c1aa0e95bd67bd',
384 'InstanceCreateNotification': '1.0-a73147b93b520ff0061865849d3dfa56',
385 'InstanceCreatePayload': '1.2-b7b2481bcd0e1edcc1970ef7150df5aa',
384 'InstancePayload': '1.2-a1988f6fe728bd4b478353a85c48ad55', 386 'InstancePayload': '1.2-a1988f6fe728bd4b478353a85c48ad55',
385 'InstanceStateUpdatePayload': '1.0-07e111c0fa0f6db0f79b0726d593e3da', 387 'InstanceStateUpdatePayload': '1.0-07e111c0fa0f6db0f79b0726d593e3da',
386 'InstanceUpdateNotification': '1.0-a73147b93b520ff0061865849d3dfa56', 388 'InstanceUpdateNotification': '1.0-a73147b93b520ff0061865849d3dfa56',