class HostList(base.ObjectListBase, base.MasakariObject): VERSION = '1.0' fields = { 'objects': fields.ListOfObjectsField('Host'), } @base.remotable_classmethod def get_all(cls, context, filters=None, sort_keys=None, sort_dirs=None, limit=None, marker=None): groups = db.host_get_all_by_filters(context, filters=filters, sort_keys=sort_keys, sort_dirs=sort_dirs, limit=limit, marker=marker) return base.obj_make_list(context, cls(context), objects.Host, groups)
class TestListObj(base.ObjectListBase, base.MasakariObject): VERSION = '1.5' fields = {'objects': fields.ListOfObjectsField('TestObj')} obj_relationships = { 'objects': [('1.0', '1.1'), ('1.1', '1.2'), ('1.3', '1.3'), ('1.5', '1.4')] }
class MyList(base.ObjectListBase, base.MasakariObject): VERSION = '1.2' fields = {'objects': fields.ListOfObjectsField('MyObjElement')} obj_relationships = { 'objects': [('1.1', '1.1'), ('1.2', '1.2')], }
class MyObj(base.MasakariPersistentObject, base.MasakariObject, base.MasakariObjectDictCompat): VERSION = '1.6' fields = { 'foo': fields.IntegerField(default=1), 'bar': fields.StringField(), 'missing': fields.StringField(), 'readonly': fields.IntegerField(read_only=True), 'rel_object': fields.ObjectField('MyOwnedObject', nullable=True), 'rel_objects': fields.ListOfObjectsField('MyOwnedObject', nullable=True), 'mutable_default': fields.ListOfStringsField(default=[]), } @staticmethod def _from_db_object(context, obj, db_obj): self = MyObj() self.foo = db_obj['foo'] self.bar = db_obj['bar'] self.missing = db_obj['missing'] self.readonly = 1 self._context = context return self def obj_load_attr(self, attrname): setattr(self, attrname, 'loaded!') @base.remotable_classmethod def query(cls, context): obj = cls(context=context, foo=1, bar='bar') obj.obj_reset_changes() return obj @base.remotable def marco(self): return 'polo' @base.remotable def _update_test(self): self.bar = 'updated' @base.remotable def save(self): self.obj_reset_changes() @base.remotable def refresh(self): self.foo = 321 self.bar = 'refreshed' self.obj_reset_changes() @base.remotable def modify_save_modify(self): self.bar = 'meow' self.save() self.foo = 42 self.rel_object = MyOwnedObject(baz=42) def obj_make_compatible(self, primitive, target_version): super(MyObj, self).obj_make_compatible(primitive, target_version) if target_version == '1.0' and 'bar' in primitive: primitive['bar'] = 'old%s' % primitive['bar']
class MyList(base.ObjectListBase, base.MasakariObject): fields = { 'objects': fields.ListOfObjectsField('MyObj'), }
class Notification(base.MasakariPersistentObject, base.MasakariObject, base.MasakariObjectDictCompat): # Version 1.0: Initial version # Version 1.1: Added recovery_workflow_details field. # Note: This field shouldn't be persisted. VERSION = '1.1' fields = { 'id': fields.IntegerField(), 'notification_uuid': fields.UUIDField(), 'generated_time': fields.DateTimeField(), 'source_host_uuid': fields.UUIDField(), 'type': fields.NotificationTypeField(), 'payload': fields.DictOfStringsField(), 'status': fields.NotificationStatusField(), # NOTE(ShilpaSD): This field shouldn't be stored in db. # The recovery workflow details read from the 'notification_driver' # will be set to this field. 'recovery_workflow_details': fields.ListOfObjectsField('NotificationProgressDetails', default=[]) } @staticmethod def _from_db_object(context, notification, db_notification): for key in notification.fields: if key in NOTIFICATION_OPTIONAL_FIELDS: continue if key != 'payload': setattr(notification, key, db_notification.get(key)) else: payload = db_notification.get("payload") notification.payload = jsonutils.loads(payload) notification.obj_reset_changes() notification._context = context return notification @base.remotable_classmethod def get_by_id(cls, context, id): db_notification = db.notification_get_by_id(context, id) return cls._from_db_object(context, cls(), db_notification) @base.remotable_classmethod def get_by_uuid(cls, context, uuid): db_notification = db.notification_get_by_uuid(context, uuid) return cls._from_db_object(context, cls(), db_notification) @base.remotable def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') updates = self.masakari_obj_get_changes() # NOTE(ShilpaSD): This field doesn't exist in the Notification # db model so don't save it. updates.pop('recovery_workflow_details', None) if 'notification_uuid' not in updates: updates['notification_uuid'] = uuidutils.generate_uuid() LOG.debug('Generated uuid %(uuid)s for notifications', dict(uuid=updates['notification_uuid'])) if 'payload' in updates: updates['payload'] = jsonutils.dumps(updates['payload']) api_utils.notify_about_notification_api( self._context, self, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.START) db_notification = db.notification_create(self._context, updates) api_utils.notify_about_notification_api( self._context, self, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_notification) @base.remotable def save(self): updates = self.masakari_obj_get_changes() updates.pop('id', None) # NOTE(ShilpaSD): This field doesn't exist in the Notification # db model so don't save it. updates.pop('recovery_workflow_details', None) db_notification = db.notification_update(self._context, self.notification_uuid, updates) self._from_db_object(self._context, self, db_notification) @base.remotable def destroy(self): if not self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='destroy', reason='already destroyed') if not self.obj_attr_is_set('notification_uuid'): raise exception.ObjectActionError(action='destroy', reason='no uuid') db.notification_delete(self._context, self.notification_uuid) delattr(self, base.get_attrname('id'))