Merge "Lazy-load resource_properties_data for events"

changes/69/437169/1
Jenkins 6 years ago committed by Gerrit Code Review
commit d947a4ab7f

@ -37,8 +37,8 @@ class Event(
'physical_resource_id': fields.StringField(nullable=True),
'resource_status_reason': fields.StringField(nullable=True),
'resource_type': fields.StringField(nullable=True),
'rsrc_prop_data': fields.ObjectField(
rpd.ResourcePropertiesData),
'rsrc_prop_data_id': fields.ObjectField(
fields.IntegerField(nullable=True)),
'created_at': fields.DateTimeField(read_only=True),
'updated_at': fields.DateTimeField(nullable=True),
}
@ -46,13 +46,13 @@ class Event(
@staticmethod
def _from_db_object(context, event, db_event):
for field in event.fields:
if field == 'resource_status_reason':
# this works whether db_event is a dict or db ref
event[field] = db_event['_resource_status_reason']
else:
event[field] = db_event[field]
if db_event['rsrc_prop_data']:
event['rsrc_prop_data'] = \
rpd.ResourcePropertiesData._from_db_object(
rpd.ResourcePropertiesData(context), context,
db_event['rsrc_prop_data'])
event._resource_properties = event['rsrc_prop_data'].data
if db_event['rsrc_prop_data_id'] is not None:
event._resource_properties = None
else:
event._resource_properties = db_event['resource_properties'] or {}
event._context = context
@ -61,6 +61,10 @@ class Event(
@property
def resource_properties(self):
if self._resource_properties is None:
rpd_obj = rpd.ResourcePropertiesData.get_by_id(
self._context, self.rsrc_prop_data_id)
self._resource_properties = rpd_obj.data or {}
return self._resource_properties
@classmethod
@ -92,8 +96,11 @@ class Event(
@classmethod
def create(cls, context, values):
return cls._from_db_object(context, cls(),
db_api.event_create(context, values))
# Using dict() allows us to be done with the sqlalchemy/model
# layer in one call, rather than hitting that layer for every
# field in _from_db_object().
return cls._from_db_object(context, cls(context=context),
dict(db_api.event_create(context, values)))
def identifier(self, stack_identifier):
"""Return a unique identifier for the event."""

@ -46,6 +46,11 @@ class ResourcePropertiesData(
elif db_rpd['encrypted'] and rpd['data'] is not None:
rpd['data'] = crypt.decrypted_dict(rpd['data'])
# TODO(cwolfe) setting the context here should go away, that
# should have been done with the initialisation of the rpd
# object. For now, maintaining consistency with other
# _from_db_object methods.
rpd._context = context
rpd.obj_reset_changes()
return rpd
@ -68,3 +73,9 @@ class ResourcePropertiesData(
result[prop_name] = encrypted_value
return (True, result)
return (False, data)
@staticmethod
def get_by_id(context, id):
db_ref = db_api.resource_prop_data_get(context, id)
return ResourcePropertiesData._from_db_object(
ResourcePropertiesData(context=context), context, db_ref)

@ -261,11 +261,11 @@ class EventTest(EventCommon):
self.ctx,
{'stack_id': self.stack.id,
'uuid': str(uuid.uuid4()),
'rsrc_prop_data': rpd_db_obj})
'rsrc_prop_data_id': rpd_db_obj.id})
e_obj = event_object.Event().get_by_id(utils.dummy_context(),
e_obj.id)
# properties data appears unencrypted to event object
self.assertEqual(data, e_obj.rsrc_prop_data.data)
self.assertEqual(data, e_obj.resource_properties)
class EventEncryptedTest(EventCommon):
@ -282,7 +282,7 @@ class EventEncryptedTest(EventCommon):
# verify the resource_properties_data db data is encrypted
e_obj = event_object.Event.get_by_id(self.resource.context, e.id)
rpd_id = e_obj['rsrc_prop_data'].id
rpd_id = e_obj['rsrc_prop_data_id']
results = self.resource.context.session.query(
models.ResourcePropertiesData).filter_by(
id=rpd_id)
@ -293,4 +293,4 @@ class EventEncryptedTest(EventCommon):
# verify encrypted data is decrypted when retrieved through
# heat object layer
ev = event_object.Event.get_by_id(self.ctx, e.id)
self.assertEqual({'Foo': 'goo'}, ev.rsrc_prop_data.data)
self.assertEqual({'Foo': 'goo'}, ev.resource_properties)

Loading…
Cancel
Save