From 8204b2492b07438d0569d3807e1c34f196756253 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Thu, 10 Nov 2016 12:19:41 +0100 Subject: [PATCH] Transform aggregate.add_host notification The aggregate.add_host.start and aggregate.add_host.end notifications has been transformed to the versioned notification framework. Note that the legacy event_type was aggregate.addhost but the versioned notification is named to aggregate.add_host to keep the naming consistent with the other event_types. Implements: bp versioned-notification-transformation-queens Change-Id: I980d89f47b944116e5fc9cedd74c43bbd88fbeae --- .../aggregate-add_host-end.json | 19 ++++++++++++ .../aggregate-add_host-start.json | 19 ++++++++++++ nova/compute/api.py | 13 ++++++++ nova/notifications/objects/aggregate.py | 2 ++ nova/tests/functional/api/client.py | 4 +++ .../test_aggregate.py | 30 +++++++++++++++++++ nova/tests/unit/compute/test_compute.py | 13 ++++++-- 7 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 doc/notification_samples/aggregate-add_host-end.json create mode 100644 doc/notification_samples/aggregate-add_host-start.json diff --git a/doc/notification_samples/aggregate-add_host-end.json b/doc/notification_samples/aggregate-add_host-end.json new file mode 100644 index 000000000000..ae1676960de7 --- /dev/null +++ b/doc/notification_samples/aggregate-add_host-end.json @@ -0,0 +1,19 @@ +{ + "priority": "INFO", + "payload": { + "nova_object.version": "1.1", + "nova_object.namespace": "nova", + "nova_object.name": "AggregatePayload", + "nova_object.data": { + "name": "my-aggregate", + "metadata": { + "availability_zone": "nova" + }, + "hosts": ["compute"], + "id": 1, + "uuid": "788608ec-ebdc-45c5-bc7f-e5f24ab92c80" + } + }, + "event_type": "aggregate.add_host.end", + "publisher_id": "nova-api:fake-mini" +} diff --git a/doc/notification_samples/aggregate-add_host-start.json b/doc/notification_samples/aggregate-add_host-start.json new file mode 100644 index 000000000000..4e82af3d78d4 --- /dev/null +++ b/doc/notification_samples/aggregate-add_host-start.json @@ -0,0 +1,19 @@ +{ + "priority": "INFO", + "payload": { + "nova_object.version": "1.1", + "nova_object.namespace": "nova", + "nova_object.name": "AggregatePayload", + "nova_object.data": { + "name": "my-aggregate", + "metadata": { + "availability_zone": "nova" + }, + "hosts": [], + "id": 1, + "uuid": "788608ec-ebdc-45c5-bc7f-e5f24ab92c80" + } + }, + "event_type": "aggregate.add_host.start", + "publisher_id": "nova-api:fake-mini" +} diff --git a/nova/compute/api.py b/nova/compute/api.py index f977edcd2290..ed5f64492f27 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -4771,6 +4771,13 @@ class AggregateAPI(base.Base): raise exception.ComputeHostNotFound(host=host_name) aggregate = objects.Aggregate.get_by_id(context, aggregate_id) + + compute_utils.notify_about_aggregate_action( + context=context, + aggregate=aggregate, + action=fields_obj.NotificationAction.ADD_HOST, + phase=fields_obj.NotificationPhase.START) + self.is_safe_to_update_az(context, aggregate.metadata, hosts=[host_name], aggregate=aggregate) @@ -4784,6 +4791,12 @@ class AggregateAPI(base.Base): compute_utils.notify_about_aggregate_update(context, "addhost.end", aggregate_payload) + compute_utils.notify_about_aggregate_action( + context=context, + aggregate=aggregate, + action=fields_obj.NotificationAction.ADD_HOST, + phase=fields_obj.NotificationPhase.END) + return aggregate @wrap_exception() diff --git a/nova/notifications/objects/aggregate.py b/nova/notifications/objects/aggregate.py index d94516090e90..a71a63503d54 100644 --- a/nova/notifications/objects/aggregate.py +++ b/nova/notifications/objects/aggregate.py @@ -46,6 +46,8 @@ class AggregatePayload(base.NotificationPayloadBase): @base.notification_sample('aggregate-create-end.json') @base.notification_sample('aggregate-delete-start.json') @base.notification_sample('aggregate-delete-end.json') +@base.notification_sample('aggregate-add_host-start.json') +@base.notification_sample('aggregate-add_host-end.json') @nova_base.NovaObjectRegistry.register_notification class AggregateNotification(base.NotificationBase): # Version 1.0: Initial version diff --git a/nova/tests/functional/api/client.py b/nova/tests/functional/api/client.py index d8de466f078c..72322b31129b 100644 --- a/nova/tests/functional/api/client.py +++ b/nova/tests/functional/api/client.py @@ -443,6 +443,10 @@ class TestOpenStackClient(object): def delete_keypair(self, keypair_name): self.api_delete('/os-keypairs/%s' % keypair_name) + def post_aggregate_action(self, aggregate_id, body): + return self.api_post( + '/os-aggregates/%s/action' % aggregate_id, body).body['aggregate'] + def get_active_migrations(self, server_id): return self.api_get('/servers/%s/migrations' % server_id).body['migrations'] diff --git a/nova/tests/functional/notification_sample_tests/test_aggregate.py b/nova/tests/functional/notification_sample_tests/test_aggregate.py index 0d376fb4839d..5f045642a882 100644 --- a/nova/tests/functional/notification_sample_tests/test_aggregate.py +++ b/nova/tests/functional/notification_sample_tests/test_aggregate.py @@ -52,3 +52,33 @@ class TestAggregateNotificationSample( 'uuid': aggregate['uuid'], 'id': aggregate['id']}, actual=fake_notifier.VERSIONED_NOTIFICATIONS[3]) + + def test_aggregate_add_host(self): + aggregate_req = { + "aggregate": { + "name": "my-aggregate", + "availability_zone": "nova"}} + aggregate = self.admin_api.post_aggregate(aggregate_req) + + fake_notifier.reset() + + add_host_req = { + "add_host": { + "host": "compute" + } + } + self.admin_api.post_aggregate_action(aggregate['id'], add_host_req) + + self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'aggregate-add_host-start', + replacements={ + 'uuid': aggregate['uuid'], + 'id': aggregate['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) + self._verify_notification( + 'aggregate-add_host-end', + replacements={ + 'uuid': aggregate['uuid'], + 'id': aggregate['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 7e6f78ed743b..8bb343477ec7 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -11284,9 +11284,10 @@ class ComputeAPIAggrTestCase(BaseTestCase): self.assertRaises(exception.InvalidAggregateActionDelete, self.api.delete_aggregate, self.context, aggr.id) + @mock.patch('nova.compute.utils.notify_about_aggregate_action') @mock.patch.object(availability_zones, 'update_host_availability_zone_cache') - def test_add_host_to_aggregate(self, mock_az): + def test_add_host_to_aggregate(self, mock_az, mock_notify): # Ensure we can add a host to an aggregate. values = _create_service_entries(self.context) fake_zone = values[0][0] @@ -11314,6 +11315,12 @@ class ComputeAPIAggrTestCase(BaseTestCase): self.assertEqual(len(aggr.hosts), 1) mock_az.assert_called_once_with(self.context, fake_host) + mock_notify.assert_has_calls([ + mock.call(context=self.context, aggregate=aggr, + action='add_host', phase='start'), + mock.call(context=self.context, aggregate=aggr, + action='add_host', phase='end')]) + def test_add_host_to_aggr_with_no_az(self): values = _create_service_entries(self.context) fake_zone = values[0][0] @@ -11547,9 +11554,11 @@ class ComputeAPIAggrCallsSchedulerTestCase(test.NoDBTestCase): self.api.delete_aggregate(self.context, 1) delete_aggregate.assert_called_once_with(self.context, agg) + @mock.patch('nova.compute.utils.notify_about_aggregate_action') @mock.patch('nova.compute.rpcapi.ComputeAPI.add_aggregate_host') @mock.patch.object(scheduler_client.SchedulerClient, 'update_aggregates') - def test_add_host_to_aggregate(self, update_aggregates, mock_add_agg): + def test_add_host_to_aggregate(self, update_aggregates, mock_add_agg, + mock_notify): self.api.is_safe_to_update_az = mock.Mock() self.api._update_az_cache_for_host = mock.Mock() agg = objects.Aggregate(name='fake', metadata={})