Exemplo n.º 1
0
    def setUp(self):
        super(TestCustomField, self).setUp()
        self.field = senlin_fields.CustomListField(attr_name='dependant')
        dep = mock.Mock()
        dep.dependant = '123'
        self.coerce_good_values = [([dep], ['123']), ([dep], ['123'])]
        self.coerce_bad_values = ['BOGUS']

        self.to_primitive_values = [([dep], [dep])]
        self.from_primitive_values = [([dep], [dep])]
Exemplo n.º 2
0
class Action(base.SenlinObject, base.VersionedObjectDictCompat):
    """Senlin action object."""

    fields = {
        'id': fields.UUIDField(),
        'created_at': fields.DateTimeField(),
        'updated_at': fields.DateTimeField(nullable=True),
        'name': fields.StringField(),
        'context': fields.JsonField(),
        'target': fields.UUIDField(),
        'action': fields.StringField(),
        'cause': fields.StringField(),
        'owner': fields.UUIDField(nullable=True),
        'interval': fields.IntegerField(nullable=True),
        'start_time': fields.FloatField(nullable=True),
        'end_time': fields.FloatField(nullable=True),
        'timeout': fields.IntegerField(nullable=True),
        'status': fields.StringField(),
        'status_reason': fields.StringField(nullable=True),
        'control': fields.StringField(nullable=True),
        'inputs': fields.JsonField(nullable=True),
        'outputs': fields.JsonField(nullable=True),
        'data': fields.JsonField(nullable=True),
        'user': fields.StringField(),
        'project': fields.StringField(),
        'domain': fields.StringField(nullable=True),
        'dep_on': fields.CustomListField(attr_name='depended', nullable=True),
        'dep_by': fields.CustomListField(attr_name='dependent', nullable=True),
    }

    @classmethod
    def create(cls, context, values):
        obj = db_api.action_create(context, values)
        return cls._from_db_object(context, cls(context), obj)

    @classmethod
    def find(cls, context, identity, **kwargs):
        """Find an action with the given identity.

        :param context: An instance of the request context.
        :param identity: The UUID, name or short-id of an action.
        :param dict kwargs: Other query parameters.
        :return: A DB object of action or an exception `ResourceNotFound` if
                 no matching action is found.
        """
        if uuidutils.is_uuid_like(identity):
            action = cls.get(context, identity, **kwargs)
            if not action:
                action = cls.get_by_name(context, identity, **kwargs)
        else:
            action = cls.get_by_name(context, identity, **kwargs)
            if not action:
                action = cls.get_by_short_id(context, identity, **kwargs)

        if not action:
            raise exception.ResourceNotFound(type='action', id=identity)

        return action

    @classmethod
    def get(cls, context, action_id, **kwargs):
        obj = db_api.action_get(context, action_id, **kwargs)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def get_by_name(cls, context, name, **kwargs):
        obj = db_api.action_get_by_name(context, name, **kwargs)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def get_by_short_id(cls, context, short_id, **kwargs):
        obj = db_api.action_get_by_short_id(context, short_id, **kwargs)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def action_list_active_scaling(cls, context, cluster_id, **kwargs):
        objs = db_api.action_list_active_scaling(context, cluster_id, **kwargs)
        return [cls._from_db_object(context, cls(), obj) for obj in objs]

    @classmethod
    def get_all(cls, context, **kwargs):
        objs = db_api.action_get_all(context, **kwargs)
        return [cls._from_db_object(context, cls(), obj) for obj in objs]

    @classmethod
    def get_all_by_owner(cls, context, owner):
        objs = db_api.action_get_all_by_owner(context, owner)
        return [cls._from_db_object(context, cls(), obj) for obj in objs]

    @classmethod
    def get_all_active_by_target(cls, context, target):
        objs = db_api.action_get_all_active_by_target(context, target)
        return [cls._from_db_object(context, cls(), obj) for obj in objs]

    @classmethod
    def check_status(cls, context, action_id, timestamp):
        return db_api.action_check_status(context, action_id, timestamp)

    @classmethod
    def mark_succeeded(cls, context, action_id, timestamp):
        return db_api.action_mark_succeeded(context, action_id, timestamp)

    @classmethod
    def mark_ready(cls, context, action_id, timestamp):
        return db_api.action_mark_ready(context, action_id, timestamp)

    @classmethod
    def mark_failed(cls, context, action_id, timestamp, reason=None):
        return db_api.action_mark_failed(context, action_id, timestamp, reason)

    @classmethod
    def mark_cancelled(cls, context, action_id, timestamp):
        return db_api.action_mark_cancelled(context, action_id, timestamp)

    @classmethod
    def acquire(cls, context, action_id, owner, timestamp):
        return db_api.action_acquire(context, action_id, owner, timestamp)

    @classmethod
    def acquire_random_ready(cls, context, owner, timestamp):
        return db_api.action_acquire_random_ready(context, owner, timestamp)

    @classmethod
    def acquire_first_ready(cls, context, owner, timestamp):
        return db_api.action_acquire_first_ready(context, owner, timestamp)

    @classmethod
    def abandon(cls, context, action_id, values=None):
        return db_api.action_abandon(context, action_id, values)

    @classmethod
    def signal(cls, context, action_id, value):
        return db_api.action_signal(context, action_id, value)

    @classmethod
    def signal_query(cls, context, action_id):
        return db_api.action_signal_query(context, action_id)

    @classmethod
    def lock_check(cls, context, action_id, owner=None):
        return db_api.action_lock_check(context, action_id, owner)

    @classmethod
    def update(cls, context, action_id, values):
        return db_api.action_update(context, action_id, values)

    @classmethod
    def delete(cls, context, action_id):
        db_api.action_delete(context, action_id)

    @classmethod
    def delete_by_target(cls,
                         context,
                         target,
                         action=None,
                         action_excluded=None,
                         status=None):
        """Delete an action with the target and other given params.

        :param target: The ID of the target cluster/node
        :param action: A list of actions to be included.
        :param action_excluded: A list of actions to be excluded.
        :param status: A list of statuses to be delete filtered.
        :return: None.
        """
        return db_api.action_delete_by_target(context,
                                              target,
                                              action=action,
                                              action_excluded=action_excluded,
                                              status=status)

    def to_dict(self):
        action_dict = {
            'id': self.id,
            'name': self.name,
            'action': self.action,
            'target': self.target,
            'cause': self.cause,
            'owner': self.owner,
            'interval': self.interval,
            'start_time': self.start_time,
            'end_time': self.end_time,
            'timeout': self.timeout,
            'status': self.status,
            'status_reason': self.status_reason,
            'inputs': self.inputs,
            'outputs': self.outputs,
            'depends_on': self.dep_on,
            'depended_by': self.dep_by,
            'created_at': utils.isotime(self.created_at),
            'updated_at': utils.isotime(self.updated_at),
            'data': self.data,
            'user': self.user,
            'project': self.project,
        }
        return action_dict
Exemplo n.º 3
0
class Cluster(base.SenlinObject, base.VersionedObjectDictCompat):
    """Senlin cluster object."""

    fields = {
        'id': fields.UUIDField(),
        'name': fields.StringField(),
        'profile_id': fields.UUIDField(),
        'parent': fields.UUIDField(nullable=True),
        'init_at': fields.DateTimeField(),
        'created_at': fields.DateTimeField(nullable=True),
        'updated_at': fields.DateTimeField(nullable=True),
        'min_size': fields.IntegerField(nullable=True),
        'max_size': fields.IntegerField(nullable=True),
        'desired_capacity': fields.IntegerField(nullable=True),
        'next_index': fields.IntegerField(nullable=True),
        'timeout': fields.IntegerField(nullable=True),
        'status': fields.StringField(),
        'status_reason': fields.StringField(nullable=True),
        'metadata': fields.JsonField(nullable=True),
        'data': fields.JsonField(nullable=True),
        'user': fields.StringField(),
        'project': fields.StringField(),
        'domain': fields.StringField(nullable=True),
        'dependents': fields.JsonField(nullable=True),
        'config': fields.JsonField(nullable=True),
        'profile_name': fields.StringField(),
        'nodes': fields.CustomListField(attr_name='id', nullable=True),
        'policies': fields.CustomListField(attr_name='id', nullable=True),
    }

    @staticmethod
    def _from_db_object(context, obj, db_obj):
        if db_obj is None:
            return None
        for field in obj.fields:
            if field == 'metadata':
                obj['metadata'] = db_obj['meta_data']
            elif field == 'profile_name':
                obj['profile_name'] = db_obj['profile'].name
            else:
                obj[field] = db_obj[field]

        obj._context = context
        obj.obj_reset_changes()

        return obj

    @classmethod
    def create(cls, context, values):
        values = cls._transpose_metadata(values)
        values['init_at'] = timeutils.utcnow(True)
        obj = db_api.cluster_create(context, values)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def find(cls, context, identity, project_safe=True):
        cluster = None
        if uuidutils.is_uuid_like(identity):
            cluster = cls.get(context, identity, project_safe=project_safe)
            if not cluster:
                cluster = cls.get_by_name(context, identity,
                                          project_safe=project_safe)
        else:
            cluster = cls.get_by_name(context, identity,
                                      project_safe=project_safe)
            # maybe it is a short form of UUID
            if not cluster:
                cluster = cls.get_by_short_id(context, identity,
                                              project_safe=project_safe)

        if not cluster:
            raise exc.ResourceNotFound(type='cluster', id=identity)

        return cluster

    @classmethod
    def get(cls, context, cluster_id, **kwargs):
        obj = db_api.cluster_get(context, cluster_id, **kwargs)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def get_by_name(cls, context, name, **kwargs):
        obj = db_api.cluster_get_by_name(context, name, **kwargs)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def get_by_short_id(cls, context, short_id, **kwargs):
        obj = db_api.cluster_get_by_short_id(context, short_id, **kwargs)
        return cls._from_db_object(context, cls(), obj)

    @classmethod
    def get_all(cls, context, **kwargs):
        objs = db_api.cluster_get_all(context, **kwargs)
        return [cls._from_db_object(context, cls(), obj) for obj in objs]

    @classmethod
    def get_next_index(cls, context, cluster_id):
        return db_api.cluster_next_index(context, cluster_id)

    @classmethod
    def count_all(cls, context, **kwargs):
        return db_api.cluster_count_all(context, **kwargs)

    @classmethod
    def update(cls, context, obj_id, values):
        values = cls._transpose_metadata(values)
        values['updated_at'] = timeutils.utcnow(True)
        return db_api.cluster_update(context, obj_id, values)

    @classmethod
    def delete(cls, context, obj_id):
        db_api.cluster_delete(context, obj_id)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'profile_id': self.profile_id,
            'user': self.user,
            'project': self.project,
            'domain': self.domain,
            'init_at': utils.isotime(self.init_at),
            'created_at': utils.isotime(self.created_at),
            'updated_at': utils.isotime(self.updated_at),
            'min_size': self.min_size,
            'max_size': self.max_size,
            'desired_capacity': self.desired_capacity,
            'timeout': self.timeout,
            'status': self.status,
            'status_reason': self.status_reason,
            'metadata': self.metadata or {},
            'data': self.data or {},
            'dependents': self.dependents or {},
            'config': self.config or {},
            'profile_name': self.profile_name,
            'nodes': self.nodes,
            'policies': self.policies
        }