From dd321e9f2179e6067b971128b940b4972ae2d34f Mon Sep 17 00:00:00 2001
From: licanwei
Date: Fri, 21 Jun 2019 16:53:25 +0800
Subject: [PATCH] improve the process of instance_created.end
In the process of handling instance_created.end,
there is a KeyError exception output log. This is because
invoking get_instance_by_uuid before creating the instance
in the data model.
During the review of https://review.opendev.org/#/c/663489/,
reviewers think that it's better to remove the KeyError exception.
This patche seperates the process of instance_created.end from
other Nova notifications and removes the call of get_instance_by_uuid.
Change-Id: Ie9e2d4f5b32ee7a5b52bbcd50abfa81dcabab7bb
---
.../decision_engine/model/notification/nova.py | 15 ++++++++++++++-
.../notification/test_nova_notifications.py | 17 +++++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/watcher/decision_engine/model/notification/nova.py b/watcher/decision_engine/model/notification/nova.py
index e3e717bb0..5936110c2 100644
--- a/watcher/decision_engine/model/notification/nova.py
+++ b/watcher/decision_engine/model/notification/nova.py
@@ -224,6 +224,19 @@ class VersionedNotification(NovaNotification):
self.update_instance(instance, payload)
+ def instance_created(self, payload):
+ instance_data = payload['nova_object.data']
+ instance_uuid = instance_data['uuid']
+ instance = element.Instance(uuid=instance_uuid)
+ self.cluster_data_model.add_instance(instance)
+
+ node_uuid = instance_data.get('host')
+ if node_uuid:
+ node = self.get_or_create_node(node_uuid)
+ self.cluster_data_model.map_instance(instance, node)
+
+ self.update_instance(instance, payload)
+
def instance_deleted(self, payload):
instance_data = payload['nova_object.data']
instance_uuid = instance_data['uuid']
@@ -240,7 +253,7 @@ class VersionedNotification(NovaNotification):
self.delete_instance(instance, node)
notification_mapping = {
- 'instance.create.end': instance_updated,
+ 'instance.create.end': instance_created,
'instance.lock': instance_updated,
'instance.unlock': instance_updated,
'instance.pause.end': instance_updated,
diff --git a/watcher/tests/decision_engine/model/notification/test_nova_notifications.py b/watcher/tests/decision_engine/model/notification/test_nova_notifications.py
index 968fa13b7..d81ba89ed 100644
--- a/watcher/tests/decision_engine/model/notification/test_nova_notifications.py
+++ b/watcher/tests/decision_engine/model/notification/test_nova_notifications.py
@@ -785,3 +785,20 @@ class TestNovaNotifications(NotificationTestCase):
# update_instance should not be called since we did not add an
# Instance object to the CDM since the CDM does not exist yet.
update_instance.assert_not_called()
+
+ def test_fake_instance_create(self):
+ self.fake_cdmc.cluster_data_model = mock.Mock()
+ handler = novanotification.VersionedNotification(self.fake_cdmc)
+ message = self.load_message('instance-create-end.json')
+
+ # get_instance_by_uuid should not be called when creating instance
+ with mock.patch.object(self.fake_cdmc.cluster_data_model,
+ 'get_instance_by_uuid') as mock_get:
+ handler.info(
+ ctxt=self.context,
+ publisher_id=message['publisher_id'],
+ event_type=message['event_type'],
+ payload=message['payload'],
+ metadata=self.FAKE_METADATA,
+ )
+ mock_get.assert_not_called()