Add host field to InstanceActionEvent

This patch adds the host field and obj_make_compatible with
InstanceActionEvent 1.2 version.

We record the event host info in event start method, and do
NOT record the host again in event finish method, because
it's the same event db record which is looked up by the
existing action(via request id) and event name from the
start event, the host of starting and ending is also same.

This would be used by instance action API to show the
``host`` information of the action event occurs.

Part of blueprint: add-host-to-instance-action-events

Change-Id: I356e7eb6b18837cf868bade7babaed96e5828688
This commit is contained in:
Yikun Jiang 2018-03-22 12:11:01 +08:00
parent 1e6564b7d4
commit 32d5a2acc8
5 changed files with 24 additions and 7 deletions

View File

@ -13,6 +13,7 @@
# under the License.
from oslo_utils import timeutils
from oslo_utils import versionutils
from nova import db
from nova import objects
@ -120,7 +121,8 @@ class InstanceActionEvent(base.NovaPersistentObject, base.NovaObject,
base.NovaObjectDictCompat):
# Version 1.0: Initial version
# Version 1.1: event_finish_with_failure decorated with serialize_args
VERSION = '1.1'
# Version 1.2: Add 'host' field
VERSION = '1.2'
fields = {
'id': fields.IntegerField(),
'event': fields.StringField(nullable=True),
@ -129,8 +131,14 @@ class InstanceActionEvent(base.NovaPersistentObject, base.NovaObject,
'finish_time': fields.DateTimeField(nullable=True),
'result': fields.StringField(nullable=True),
'traceback': fields.StringField(nullable=True),
'host': fields.StringField(nullable=True),
}
def obj_make_compatible(self, primitive, target_version):
target_version = versionutils.convert_version_to_tuple(target_version)
if target_version < (1, 2) and 'host' in primitive:
del primitive['host']
@staticmethod
def _from_db_object(context, event, db_event):
for field in event.fields:
@ -140,11 +148,13 @@ class InstanceActionEvent(base.NovaPersistentObject, base.NovaObject,
return event
@staticmethod
def pack_action_event_start(context, instance_uuid, event_name):
def pack_action_event_start(context, instance_uuid, event_name,
host=None):
values = {'event': event_name,
'instance_uuid': instance_uuid,
'request_id': context.request_id,
'start_time': timeutils.utcnow()}
'start_time': timeutils.utcnow(),
'host': host}
return values
@staticmethod
@ -168,9 +178,10 @@ class InstanceActionEvent(base.NovaPersistentObject, base.NovaObject,
return cls._from_db_object(context, cls(), db_event)
@base.remotable_classmethod
def event_start(cls, context, instance_uuid, event_name, want_result=True):
def event_start(cls, context, instance_uuid, event_name, want_result=True,
host=None):
values = cls.pack_action_event_start(context, instance_uuid,
event_name)
event_name, host=host)
db_event = db.action_event_start(context, values)
if want_result:
return cls._from_db_object(context, cls(), db_event)

View File

@ -53,12 +53,14 @@ def format_action(action, expect_traceback=True):
return action
def format_event(event, expect_traceback=True):
def format_event(event, expect_traceback=True, expect_host=False):
'''Remove keys that aren't serialized.'''
to_delete = ['id', 'created_at', 'updated_at', 'deleted_at', 'deleted',
'action_id']
if not expect_traceback:
to_delete.append('traceback')
if not expect_host:
to_delete.append('host')
for key in to_delete:
if key in event:
del(event[key])

View File

@ -70,6 +70,7 @@ FAKE_EVENTS = {
'updated_at': None,
'deleted_at': None,
'deleted': False,
'host': 'host1'
},
{'id': 2,
'action_id': FAKE_ACTION_ID1,
@ -84,6 +85,7 @@ FAKE_EVENTS = {
'updated_at': None,
'deleted_at': None,
'deleted': False,
'host': 'host1'
}
],
FAKE_ACTION_ID2: [{'id': 3,
@ -99,6 +101,7 @@ FAKE_EVENTS = {
'updated_at': None,
'deleted_at': None,
'deleted': False,
'host': 'host2'
}
]
}

View File

@ -54,6 +54,7 @@ fake_event = {
'finish_time': None,
'result': 'fake-result',
'traceback': 'fake-tb',
'host': 'fake-host',
}

View File

@ -1097,7 +1097,7 @@ object_data = {
'ImageMetaProps': '1.19-dc9581ff2b80d8c33462889916b82df0',
'Instance': '2.3-4f98ab23f4b0a25fabb1040c8f5edecc',
'InstanceAction': '1.1-f9f293e526b66fca0d05c3b3a2d13914',
'InstanceActionEvent': '1.1-e56a64fa4710e43ef7af2ad9d6028b33',
'InstanceActionEvent': '1.2-b2f368b8a29d8d872b1f6ea841e820a0',
'InstanceActionEventList': '1.1-13d92fb953030cdbfee56481756e02be',
'InstanceActionList': '1.1-a2b2fb6006b47c27076d3a1d48baa759',
'InstanceDeviceMetadata': '1.0-74d78dd36aa32d26d2769a1b57caf186',