From a4c7ab885121b7b76d1bb9c65f215442d1d10053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9la=20Vancsics?= Date: Thu, 6 Jul 2017 08:40:18 +0200 Subject: [PATCH] Transform keypair.import notification The keypair.import.start and keypair.import.end notifications has been transformed to the versioned notification framework. Change-Id: Icadc53d84fa021aae0f9ec2e18e9125de98e00f6 Implements: bp versioned-notification-transformation-queens --- .../keypair-import-end.json | 17 ++++++++++++++ .../keypair-import-start.json | 17 ++++++++++++++ nova/compute/api.py | 19 ++++++++++++--- nova/notifications/objects/base.py | 3 ++- nova/notifications/objects/keypair.py | 2 ++ nova/objects/fields.py | 3 ++- .../notification_sample_base.py | 10 ++++++++ .../notification_sample_tests/test_keypair.py | 23 +++++++++++++++++++ nova/tests/unit/compute/test_keypairs.py | 8 ++++++- .../objects/test_notification.py | 2 +- 10 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 doc/notification_samples/keypair-import-end.json create mode 100644 doc/notification_samples/keypair-import-start.json diff --git a/doc/notification_samples/keypair-import-end.json b/doc/notification_samples/keypair-import-end.json new file mode 100644 index 000000000000..03fbbedeb1e8 --- /dev/null +++ b/doc/notification_samples/keypair-import-end.json @@ -0,0 +1,17 @@ +{ + "priority": "INFO", + "payload": { + "nova_object.version": "1.0", + "nova_object.namespace": "nova", + "nova_object.name": "KeypairPayload", + "nova_object.data": { + "user_id": "fake", + "name": "my-key", + "fingerprint": "1e:2c:9b:56:79:4b:45:77:f9:ca:7a:98:2c:b0:d5:3c", + "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDx8nkQv/zgGgB4rMYmIf+6A4l6Rr+o/6lHBQdW5aYd44bd8JttDCE/F/pNRr0lRE+PiqSPO8nDPHw0010JeMH9gYgnnFlyY3/OcJ02RhIPyyxYpv9FhY+2YiUkpwFOcLImyrxEsYXpD/0d3ac30bNH6Sw9JD9UZHYcpSxsIbECHw== Generated-by-Nova", + "type": "ssh" + } + }, + "event_type": "keypair.import.end", + "publisher_id": "nova-api:fake-mini" +} \ No newline at end of file diff --git a/doc/notification_samples/keypair-import-start.json b/doc/notification_samples/keypair-import-start.json new file mode 100644 index 000000000000..444aa600a138 --- /dev/null +++ b/doc/notification_samples/keypair-import-start.json @@ -0,0 +1,17 @@ +{ + "priority": "INFO", + "payload": { + "nova_object.version": "1.0", + "nova_object.namespace": "nova", + "nova_object.name": "KeypairPayload", + "nova_object.data": { + "user_id": "fake", + "name": "my-key", + "fingerprint": null, + "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDx8nkQv/zgGgB4rMYmIf+6A4l6Rr+o/6lHBQdW5aYd44bd8JttDCE/F/pNRr0lRE+PiqSPO8nDPHw0010JeMH9gYgnnFlyY3/OcJ02RhIPyyxYpv9FhY+2YiUkpwFOcLImyrxEsYXpD/0d3ac30bNH6Sw9JD9UZHYcpSxsIbECHw== Generated-by-Nova", + "type": "ssh" + } + }, + "event_type": "keypair.import.start", + "publisher_id": "nova-api:fake-mini" +} \ No newline at end of file diff --git a/nova/compute/api.py b/nova/compute/api.py index 89c02440c02c..a0ee412a493b 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -4815,16 +4815,29 @@ class KeypairAPI(base.Base): self._notify(context, 'import.start', key_name) - fingerprint = self._generate_fingerprint(public_key, key_type) - keypair = objects.KeyPair(context) keypair.user_id = user_id keypair.name = key_name keypair.type = key_type - keypair.fingerprint = fingerprint + keypair.fingerprint = None keypair.public_key = public_key + + compute_utils.notify_about_keypair_action( + context=context, + keypair=keypair, + action=fields_obj.NotificationAction.IMPORT, + phase=fields_obj.NotificationPhase.START) + + fingerprint = self._generate_fingerprint(public_key, key_type) + + keypair.fingerprint = fingerprint keypair.create() + compute_utils.notify_about_keypair_action( + context=context, + keypair=keypair, + action=fields_obj.NotificationAction.IMPORT, + phase=fields_obj.NotificationPhase.END) self._notify(context, 'import.end', key_name) return keypair diff --git a/nova/notifications/objects/base.py b/nova/notifications/objects/base.py index b8fb032489ea..95bc30cf4f47 100644 --- a/nova/notifications/objects/base.py +++ b/nova/notifications/objects/base.py @@ -52,7 +52,8 @@ class EventType(NotificationObject): # NotificationActionField enum # Version 1.7: REMOVE_FIXED_IP replaced with INTERFACE_DETACH in # NotificationActionField enum - VERSION = '1.7' + # Version 1.8: IMPORT value is added to NotificationActionField enum + VERSION = '1.8' fields = { 'object': fields.StringField(nullable=False), diff --git a/nova/notifications/objects/keypair.py b/nova/notifications/objects/keypair.py index cb419530219f..bb473f50f991 100644 --- a/nova/notifications/objects/keypair.py +++ b/nova/notifications/objects/keypair.py @@ -43,6 +43,8 @@ class KeypairPayload(base.NotificationPayloadBase): @base.notification_sample('keypair-create-end.json') @base.notification_sample('keypair-delete-start.json') @base.notification_sample('keypair-delete-end.json') +@base.notification_sample('keypair-import-start.json') +@base.notification_sample('keypair-import-end.json') @nova_base.NovaObjectRegistry.register_notification class KeypairNotification(base.NotificationBase): # Version 1.0: Initial version diff --git a/nova/objects/fields.py b/nova/objects/fields.py index b7d3791ee073..8f28bfad5652 100644 --- a/nova/objects/fields.py +++ b/nova/objects/fields.py @@ -825,6 +825,7 @@ class NotificationAction(BaseNovaEnum): VOLUME_ATTACH = 'volume_attach' VOLUME_DETACH = 'volume_detach' CREATE = 'create' + IMPORT = 'import' EVACUATE = 'evacuate' RESIZE_FINISH = 'resize_finish' LIVE_MIGRATION_ABORT = 'live_migration_abort' @@ -849,7 +850,7 @@ class NotificationAction(BaseNovaEnum): ALL = (UPDATE, EXCEPTION, DELETE, PAUSE, UNPAUSE, RESIZE, VOLUME_SWAP, SUSPEND, POWER_ON, REBOOT, SHUTDOWN, SNAPSHOT, INTERFACE_ATTACH, POWER_OFF, SHELVE, RESUME, RESTORE, EXISTS, RESCUE, VOLUME_ATTACH, - VOLUME_DETACH, CREATE, EVACUATE, RESIZE_FINISH, + VOLUME_DETACH, CREATE, IMPORT, EVACUATE, RESIZE_FINISH, LIVE_MIGRATION_ABORT, LIVE_MIGRATION_POST_DEST, LIVE_MIGRATION_POST, LIVE_MIGRATION_PRE, LIVE_MIGRATION_ROLLBACK, LIVE_MIGRATION_ROLLBACK_DEST, REBUILD, INTERFACE_DETACH, diff --git a/nova/tests/functional/notification_sample_tests/notification_sample_base.py b/nova/tests/functional/notification_sample_tests/notification_sample_base.py index a9662c74b7e2..2b6757d708d4 100644 --- a/nova/tests/functional/notification_sample_tests/notification_sample_base.py +++ b/nova/tests/functional/notification_sample_tests/notification_sample_base.py @@ -169,6 +169,16 @@ class NotificationSampleTestBase(test.TestCase, }} self.api.post_keypair(keypair_req) + keypair_expected_notifications = [ + 'keypair-import-start', + 'keypair-import-end' + ] + self.assertLessEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + for notification in keypair_expected_notifications: + self._verify_notification( + notification, + actual=fake_notifier.VERSIONED_NOTIFICATIONS.pop(0)) + server = self._build_minimal_create_server_request( self.api, 'some-server', image_uuid='155d900f-4e14-4e4c-a73d-069cbf4541e6', diff --git a/nova/tests/functional/notification_sample_tests/test_keypair.py b/nova/tests/functional/notification_sample_tests/test_keypair.py index a0421be3cf47..917889919b00 100644 --- a/nova/tests/functional/notification_sample_tests/test_keypair.py +++ b/nova/tests/functional/notification_sample_tests/test_keypair.py @@ -55,3 +55,26 @@ class TestKeypairNotificationSample( "public_key": keypair['public_key'] }, actual=fake_notifier.VERSIONED_NOTIFICATIONS[3]) + + def test_keypair_import(self): + pub_key = ('ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDx8nkQv/zgGg' + 'B4rMYmIf+6A4l6Rr+o/6lHBQdW5aYd44bd8JttDCE/F/pNRr0l' + 'RE+PiqSPO8nDPHw0010JeMH9gYgnnFlyY3/OcJ02RhIPyyxYpv' + '9FhY+2YiUkpwFOcLImyrxEsYXpD/0d3ac30bNH6Sw9JD9UZHYc' + 'pSxsIbECHw== Generated-by-Nova') + keypair_req = { + "keypair": { + "name": "my-key", + "user_id": "fake", + "public_key": pub_key, + "type": "ssh"}} + + self.api.post_keypair(keypair_req) + + self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'keypair-import-start', + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) + self._verify_notification( + 'keypair-import-end', + actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) diff --git a/nova/tests/unit/compute/test_keypairs.py b/nova/tests/unit/compute/test_keypairs.py index 4f22be32f4c1..c7b54165ed2d 100644 --- a/nova/tests/unit/compute/test_keypairs.py +++ b/nova/tests/unit/compute/test_keypairs.py @@ -195,7 +195,8 @@ class CreateKeypairTestCase(KeypairAPITestCase, CreateImportSharedTestMixIn): class ImportKeypairTestCase(KeypairAPITestCase, CreateImportSharedTestMixIn): func_name = 'import_key_pair' - def _check_success(self): + @mock.patch('nova.compute.utils.notify_about_keypair_action') + def _check_success(self, mock_notify): keypair = self.keypair_api.import_key_pair(self.ctxt, self.ctxt.user_id, 'foo', @@ -207,6 +208,11 @@ class ImportKeypairTestCase(KeypairAPITestCase, CreateImportSharedTestMixIn): self.assertEqual(self.fingerprint, keypair['fingerprint']) self.assertEqual(self.pub_key, keypair['public_key']) self.assertEqual(self.keypair_type, keypair['type']) + mock_notify.assert_has_calls([ + mock.call(context=self.ctxt, keypair=keypair, + action='import', phase='start'), + mock.call(context=self.ctxt, keypair=keypair, + action='import', phase='end')]) self._check_notifications(action='import') def test_success_ssh(self): diff --git a/nova/tests/unit/notifications/objects/test_notification.py b/nova/tests/unit/notifications/objects/test_notification.py index 4aa6cb2991ae..9c9b3a5876e3 100644 --- a/nova/tests/unit/notifications/objects/test_notification.py +++ b/nova/tests/unit/notifications/objects/test_notification.py @@ -373,7 +373,7 @@ notification_object_data = { 'AuditPeriodPayload': '1.0-2b429dd307b8374636703b843fa3f9cb', 'BandwidthPayload': '1.0-ee2616a7690ab78406842a2b68e34130', 'BlockDevicePayload': '1.0-29751e1b6d41b1454e36768a1e764df8', - 'EventType': '1.7-3a3b2d10c77bc2ad7c3a4dd7ff2d9d9b', + 'EventType': '1.8-0f8fb2dbc76f10c7c56d1680c65ad0cf', 'ExceptionNotification': '1.0-a73147b93b520ff0061865849d3dfa56', 'ExceptionPayload': '1.0-27db46ee34cd97e39f2643ed92ad0cc5', 'FlavorNotification': '1.0-a73147b93b520ff0061865849d3dfa56',