예제 #1
0
class NotificationApiPayloadBase(base.NotificationPayloadBase):
    SCHEMA = {
        'id': ('notification', 'id'),
        'notification_uuid': ('notification', 'notification_uuid'),
        'generated_time': ('notification', 'generated_time'),
        'source_host_uuid': ('notification', 'source_host_uuid'),
        'type': ('notification', 'type'),
        'payload': ('notification', 'payload'),
        'status': ('notification', 'status'),
    }
    # Version 1.0: Initial version
    VERSION = '1.0'
    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(),
    }

    def __init__(self, notification, **kwargs):
        super(NotificationApiPayloadBase, self).__init__(**kwargs)
        self.populate_schema(notification=notification)
예제 #2
0
 def setUp(self):
     super(TestDateTime, self).setUp()
     self.dt = datetime.datetime(2016, 11, 5, tzinfo=iso8601.UTC)
     self.field = fields.DateTimeField()
     self.coerce_good_values = [(self.dt, self.dt),
                                (utils.isotime(self.dt), self.dt)]
     self.coerce_bad_values = [1, 'foo']
     self.to_primitive_values = [(self.dt, utils.isotime(self.dt))]
     self.from_primitive_values = [(utils.isotime(self.dt), self.dt)]
예제 #3
0
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'))
예제 #4
0
class Notification(base.MasakariPersistentObject, base.MasakariObject,
                   base.MasakariObjectDictCompat):

    VERSION = '1.0'

    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(),
        }

    @staticmethod
    def _from_db_object(context, notification, db_notification):

        for key in notification.fields:
            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()

        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'])

        db_notification = db.notification_create(self._context, updates)
        self._from_db_object(self._context, self, db_notification)

    @base.remotable
    def save(self):
        updates = self.masakari_obj_get_changes()

        updates.pop('id', 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'))