From bd0ba826e4cc764f257d20c748c390caf73aa619 Mon Sep 17 00:00:00 2001 From: Zhihai Song Date: Thu, 16 Jun 2016 15:08:26 +0800 Subject: [PATCH] Transform instance.restore notifications The instance.restore.start and instance.restore.end notifications are transformed to the versioned framework. Change-Id: I4459ec05a7153af7a0f04adcccb9d99cb5f661e5 Implements: bp versioned-notification-transformation-newton --- .../instance-restore-end.json | 62 +++++++++++++++++++ .../instance-restore-start.json | 62 +++++++++++++++++++ nova/compute/manager.py | 6 ++ nova/notifications/objects/instance.py | 4 +- .../test_instance.py | 25 ++++++++ nova/tests/unit/compute/test_compute_mgr.py | 17 +++++ 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 doc/notification_samples/instance-restore-end.json create mode 100644 doc/notification_samples/instance-restore-start.json diff --git a/doc/notification_samples/instance-restore-end.json b/doc/notification_samples/instance-restore-end.json new file mode 100644 index 000000000000..46afe08ebf2b --- /dev/null +++ b/doc/notification_samples/instance-restore-end.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.restore.end", + "payload":{ + "nova_object.data":{ + "architecture":"x86_64", + "availability_zone":null, + "created_at":"2012-10-29T13:42:11Z", + "deleted_at":null, + "display_name":"some-server", + "fault":null, + "host":"compute", + "host_name":"some-server", + "ip_addresses": [{ + "nova_object.name": "IpPayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0", + "nova_object.data": { + "mac": "fa:16:3e:4c:2c:30", + "address": "192.168.1.3", + "port_uuid": "ce531f90-199f-48c0-816c-13e38010b442", + "meta": {}, + "version": 4, + "label": "private-network", + "device_name": "tapce531f90-19" + } + }], + "kernel_id":"", + "launched_at":"2012-10-29T13:42:11Z", + "image_uuid": "155d900f-4e14-4e4c-a73d-069cbf4541e6", + "metadata":{}, + "node":"fake-mini", + "os_type":null, + "progress":0, + "ramdisk_id":"", + "reservation_id":"r-npxv0e40", + "state":"active", + "task_state":null, + "power_state":"running", + "tenant_id":"6f70656e737461636b20342065766572", + "terminated_at":null, + "flavor": { + "nova_object.name": "FlavorPayload", + "nova_object.data": { + "flavorid": "a22d5517-147c-4147-a0d1-e698df5cd4e3", + "root_gb": 1, + "vcpus": 1, + "ephemeral_gb": 0, + "memory_mb": 512 + }, + "nova_object.version": "1.0", + "nova_object.namespace": "nova" + }, + "user_id":"fake", + "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" + }, + "nova_object.name":"InstanceActionPayload", + "nova_object.namespace":"nova", + "nova_object.version":"1.0" + }, + "priority":"INFO", + "publisher_id":"nova-compute:compute" +} \ No newline at end of file diff --git a/doc/notification_samples/instance-restore-start.json b/doc/notification_samples/instance-restore-start.json new file mode 100644 index 000000000000..6106b8ed711b --- /dev/null +++ b/doc/notification_samples/instance-restore-start.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.restore.start", + "payload":{ + "nova_object.data":{ + "architecture":"x86_64", + "availability_zone":null, + "created_at":"2012-10-29T13:42:11Z", + "deleted_at":null, + "display_name":"some-server", + "fault":null, + "host":"compute", + "host_name":"some-server", + "ip_addresses": [{ + "nova_object.name": "IpPayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0", + "nova_object.data": { + "mac": "fa:16:3e:4c:2c:30", + "address": "192.168.1.3", + "port_uuid": "ce531f90-199f-48c0-816c-13e38010b442", + "meta": {}, + "version": 4, + "label": "private-network", + "device_name": "tapce531f90-19" + } + }], + "kernel_id":"", + "launched_at":"2012-10-29T13:42:11Z", + "image_uuid": "155d900f-4e14-4e4c-a73d-069cbf4541e6", + "metadata":{}, + "node":"fake-mini", + "os_type":null, + "progress":0, + "ramdisk_id":"", + "reservation_id":"r-npxv0e40", + "state":"soft-delete", + "task_state":"restoring", + "power_state":"running", + "tenant_id":"6f70656e737461636b20342065766572", + "terminated_at":null, + "flavor": { + "nova_object.name": "FlavorPayload", + "nova_object.data": { + "flavorid": "a22d5517-147c-4147-a0d1-e698df5cd4e3", + "root_gb": 1, + "vcpus": 1, + "ephemeral_gb": 0, + "memory_mb": 512 + }, + "nova_object.version": "1.0", + "nova_object.namespace": "nova" + }, + "user_id":"fake", + "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" + }, + "nova_object.name":"InstanceActionPayload", + "nova_object.namespace":"nova", + "nova_object.version":"1.0" + }, + "priority":"INFO", + "publisher_id":"nova-compute:compute" +} \ No newline at end of file diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 132f011c1abc..155bb6dac3cd 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -2534,6 +2534,9 @@ class ComputeManager(manager.Manager): def restore_instance(self, context, instance): """Restore a soft-deleted instance on this host.""" self._notify_about_instance_usage(context, instance, "restore.start") + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.RESTORE, + phase=fields.NotificationPhase.START) try: self.driver.restore(instance) except NotImplementedError: @@ -2545,6 +2548,9 @@ class ComputeManager(manager.Manager): instance.task_state = None instance.save(expected_task_state=task_states.RESTORING) self._notify_about_instance_usage(context, instance, "restore.end") + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.RESTORE, + phase=fields.NotificationPhase.END) @staticmethod def _set_migration_status(migration, status): diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py index da5fb79b85fd..e871edbbcf07 100644 --- a/nova/notifications/objects/instance.py +++ b/nova/notifications/objects/instance.py @@ -247,8 +247,8 @@ class InstanceStateUpdatePayload(base.NotificationPayloadBase): @base.notification_sample('instance-shelve-end.json') # @base.notification_sample('instance-resume-start.json') # @base.notification_sample('instance-resume-end.json') -# @base.notification_sample('instance-restore-start.json') -# @base.notification_sample('instance-restore-end.json') +@base.notification_sample('instance-restore-start.json') +@base.notification_sample('instance-restore-end.json') @nova_base.NovaObjectRegistry.register_notification class InstanceActionNotification(base.NotificationBase): # Version 1.0: Initial version diff --git a/nova/tests/functional/notification_sample_tests/test_instance.py b/nova/tests/functional/notification_sample_tests/test_instance.py index f186b4de61cf..ac10179230b2 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -314,3 +314,28 @@ class TestInstanceNotificationSample( notification_sample_base.NotificationSampleTestBase.ANY, 'uuid': server['id']}, actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) + + def test_delete_restore_server(self): + self.flags(reclaim_instance_interval=30) + server = self._boot_a_server( + extra_params={'networks': [{'port': self.neutron.port_1['id']}]}) + self.api.delete_server(server['id']) + self._wait_for_state_change(self.api, server, 'SOFT_DELETED') + self.api.post_server_action(server['id'], {'restore': {}}) + self._wait_for_state_change(self.api, server, 'ACTIVE') + + self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'instance-restore-start', + replacements={ + 'reservation_id': + notification_sample_base.NotificationSampleTestBase.ANY, + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) + self._verify_notification( + 'instance-restore-end', + replacements={ + 'reservation_id': + notification_sample_base.NotificationSampleTestBase.ANY, + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) diff --git a/nova/tests/unit/compute/test_compute_mgr.py b/nova/tests/unit/compute/test_compute_mgr.py index 886e9793e7aa..480b4c2f76dd 100755 --- a/nova/tests/unit/compute/test_compute_mgr.py +++ b/nova/tests/unit/compute/test_compute_mgr.py @@ -3214,6 +3214,23 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase): self.assertIsNone(instance.task_state) self.assertEqual(vm_states.ACTIVE, instance.vm_state) + def test_instance_restore_notification(self): + inst_obj = fake_instance.fake_instance_obj(self.context, + vm_state=vm_states.SOFT_DELETED) + with test.nested( + mock.patch.object(nova.compute.utils, + 'notify_about_instance_action'), + mock.patch.object(self.compute, '_notify_about_instance_usage'), + mock.patch.object(objects.Instance, 'save'), + mock.patch.object(self.compute.driver, 'restore') + ) as (fake_notify, fake_notify_usage, fake_save, fake_restore): + self.compute.restore_instance(self.context, inst_obj) + fake_notify.assert_has_calls([ + mock.call(self.context, inst_obj, 'fake-mini', + action='restore', phase='start'), + mock.call(self.context, inst_obj, 'fake-mini', + action='restore', phase='end')]) + class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase): def setUp(self):