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
This commit is contained in:
licanwei
2019-06-21 16:53:25 +08:00
parent d98f51c452
commit dd321e9f21
2 changed files with 31 additions and 1 deletions

View File

@@ -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,

View File

@@ -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()