diff --git a/nova/notifications/objects/base.py b/nova/notifications/objects/base.py index 7590f76a3e76..2d7b4b46499e 100644 --- a/nova/notifications/objects/base.py +++ b/nova/notifications/objects/base.py @@ -162,11 +162,7 @@ class NotificationPublisher(NotificationObject): @classmethod def from_service_obj(cls, service): - # nova-osapi_compute binary name needs to be translated to nova-api - # notification source enum value. - source = ("nova-api" - if service.binary == "nova-osapi_compute" - else service.binary) + source = fields.NotificationSource.get_source_by_binary(service.binary) return cls(host=service.host, source=source) diff --git a/nova/objects/fields.py b/nova/objects/fields.py index bbdf7bb0dd77..37adc61d7868 100644 --- a/nova/objects/fields.py +++ b/nova/objects/fields.py @@ -809,6 +809,12 @@ class NotificationSource(BaseNovaEnum): ALL = (API, COMPUTE, CONDUCTOR, SCHEDULER, NETWORK, CONSOLEAUTH, CELLS, CONSOLE, METADATA) + @staticmethod + def get_source_by_binary(binary): + # nova-osapi_compute binary name needs to be translated to nova-api + # notification source enum value. + return "nova-api" if binary == "nova-osapi_compute" else binary + class NotificationAction(BaseNovaEnum): UPDATE = 'update' diff --git a/nova/tests/functional/notification_sample_tests/test_service.py b/nova/tests/functional/notification_sample_tests/test_service.py index 0863e7df7551..19cd40dbf686 100644 --- a/nova/tests/functional/notification_sample_tests/test_service.py +++ b/nova/tests/functional/notification_sample_tests/test_service.py @@ -151,6 +151,8 @@ class TestServiceNotificationSample(TestServiceNotificationBase): def test_service_destroy(self): self.compute2 = self.start_service('compute', host='host2') + # This test runs with the latest microversion by default so we get the + # service uuid back from the REST API. compute2_service_id = self.admin_api.get_services( host=self.compute2.host, binary='nova-compute')[0]['id'] self.admin_api.api_delete('os-services/%s' % compute2_service_id) diff --git a/nova/tests/unit/objects/test_fields.py b/nova/tests/unit/objects/test_fields.py index cb7952225575..c4a0e0e0e470 100644 --- a/nova/tests/unit/objects/test_fields.py +++ b/nova/tests/unit/objects/test_fields.py @@ -736,3 +736,13 @@ class TestSchemaGeneration(test.NoDBTestCase): expected = {'type': ['string'], 'pattern': '[a-z]+[0-9]+', 'readonly': False} self.assertEqual(expected, field.get_schema()) + + +class TestNotificationSource(test.NoDBTestCase): + def test_get_source_by_binary(self): + self.assertEqual('nova-api', + fields.NotificationSource.get_source_by_binary( + 'nova-osapi_compute')) + self.assertEqual('nova-metadata', + fields.NotificationSource.get_source_by_binary( + 'nova-metadata'))