Example #1
0
    def delete(self, oid=None, name=None):
        """Delete property.

        :param oid: property id
        :param name: property name
        :param value: property value
        :return: delete response
        :raises: :class:`gibbonutil.db.TransactionError`
        """
        session = self.get_session()
        if oid is not None:
            prop = session.query(ConfigProp).filter_by(id=oid).first()
        elif name is not None:
            prop = session.query(ConfigProp).filter_by(name=name).first()
        else:
            self.logger.error('Specify at least oid or name')
            raise ModelError('Specify at least oid or name')

        if prop is None:
            self.logger.error('No property found')
            raise ModelError('No property found')

        res = session.delete(prop)

        self.logger.debug('Delete property: %s' % prop)
        return res
Example #2
0
    def get_entity(self, entityclass, oid, for_update=False, *args, **kvargs):
        """Parse oid and get entity by name or by model id or by uuid
        
        :param entityclass: entity model class
        :param oid: entity model id or name or uuid
        :param dict kvargs: additional filters [optional]
        :param for_update: True if model is get for update [default=False]
        :return: list of entityclass
        :raises QueryError: raise :class:`QueryError`           
        """
        session = self.get_session()

        if oid is None:
            raise ModelError('%s %s not found' % (oid, entityclass))

        if isinstance(oid, int):
            oid = str(oid)

        # get obj by uuid
        if match(
                '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
                oid):
            search_field = 'uuid'
            entity = self.query_entities(entityclass,
                                         session,
                                         uuid=oid,
                                         *args,
                                         **kvargs)
        # get obj by id
        elif match('^\d+$', oid):
            search_field = 'id'
            entity = self.query_entities(entityclass,
                                         session,
                                         oid=oid,
                                         *args,
                                         **kvargs)
        # get obj by name
        elif match('[\-\w\d]+', oid):
            search_field = 'name'
            entity = self.query_entities(entityclass,
                                         session,
                                         name=oid,
                                         **kvargs)

        res = None
        if for_update:
            res = entity.with_for_update().first()
        else:
            res = entity.first()

        if entity is None:
            msg = 'No %s found' % entityclass.__name__
            self.logger.error(msg)
            raise ModelError(msg, code=404)

        self.logger.debug2('Query entity %s by %s %s: %s' %
                           (entityclass.__name__, search_field, oid, res))
        return res
Example #3
0
    def update_entity(self, entityclass, *args, **kvargs):
        """Update entity.

        :param entityclass: entity model class
        :param args: positional args
        :param kvargs: date to update. [optional]
        :param kvargs.oid: database id [optional]  
        :param kvargs.uuid: unique id [optional]        
        :return: entity oid
        :raises TransactionError: raise :class:`TransactionError`        
        """
        session = self.get_session()

        # get entity
        oid = kvargs.pop('oid', None)
        uuid = kvargs.pop('uuid', None)
        if oid is not None:
            query = self.query_entities(entityclass, session, oid=oid)
        elif uuid is not None:
            query = self.query_entities(entityclass, session, uuid=uuid)
            oid = uuid
        else:
            raise ModelError('Neither oid nor uuid are been specified',
                             code=400)

        # check entity exists
        entity = query.first()
        if entity is None:
            msg = 'No %s found' % entityclass.__name__
            self.logger.error(msg)
            raise ModelError(msg, code=404)

        params = {}
        for k, v in kvargs.items():
            if v is not None:
                params[k] = v
            # if v is None:
            #     kvargs.pop(k)

        # create data dict with update
        if getattr(entityclass, 'modification_date', None) is not None:
            kvargs['modification_date'] = datetime.today()

        query.update(kvargs)
        session.flush()

        self.logger.debug2('Update %s %s with data: %s' %
                           (entityclass.__name__, oid, kvargs))
        return oid
Example #4
0
    def get(self, app=None, group=None, oid=None, name=None):
        """Get configuration properties.
        
        :param app: property app [optional]
        :param group: property group [optional]
        :param oid: property id [optional]
        :param name: property name [optional]
        :return: list of :class:`ConfigProp`
        :raises: :class:`gibbonutil.db.QueryError`
        """
        session = self.get_session()
        if oid is not None:
            prop = session.query(ConfigProp).filter_by(id=oid).all()
        elif name is not None:
            prop = session.query(ConfigProp).filter_by(name=name).all()
        elif app is not None or group is not None:
            query = session.query(ConfigProp)
            if app is not None:
                query = query.filter_by(app=app)
            if group is not None:
                query = query.filter_by(group=group)
            prop = query.all()
        else:
            prop = session.query(ConfigProp).all()

        if len(prop) == 0:
            self.logger.warn(
                'No properties (app=%s, group=%s oid=%s, name=%s) found' %
                (app, group, oid, name))
            raise ModelError(
                'No properties (app=%s, group=%s, oid=%s, name=%s) found' %
                (app, group, oid, name))

        self.logger.debug('Get properties: %s' % prop)
        return prop
Example #5
0
    def remove_entity(self, entityclass, *args, **kvargs):
        """Remove entity.
        
        :param entityclass: entity model class
        :param args: positional args
        :param kvargs: date to update. [optional]
        :param kvargs.oid: database id [optional]  
        :param kvargs.uuid: unique id [optional]        
        :return: entity oid
        :raises TransactionError: raise :class:`TransactionError`
        """
        session = self.get_session()

        # get entity
        query = self.query_entities(entityclass, session, **kvargs)

        # check entity exists
        entity = query.first()
        if entity is None:
            msg = 'No %s found' % entityclass.__name__
            self.logger.error(msg)
            raise ModelError(msg, code=404)

        # delete entity
        session.delete(entity)

        self.logger.debug2('Remove %s %s' % (entityclass.__name__, entity.id))
        return entity.id
Example #6
0
    def exist_entity(self, entityclass, oid, *args, **kvargs):
        """Parse oid and check entity exists

        :param entityclass: entity model class
        :param oid: entity model id or name or uuid
        :param dict kvargs: additional filters [optional]
        :return: True if exists
        """
        session = self.get_session()

        if oid is None:
            raise ModelError('%s %s not found' % (oid, entityclass))

        if isinstance(oid, int):
            oid = str(oid)

        # get obj by uuid
        if match(
                '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
                oid):
            search_field = 'uuid'
            entity = self.query_entities(entityclass,
                                         session,
                                         uuid=oid,
                                         *args,
                                         **kvargs)
        # get obj by id
        elif match('^\d+$', oid):
            search_field = 'id'
            entity = self.query_entities(entityclass,
                                         session,
                                         oid=oid,
                                         *args,
                                         **kvargs)
        # get obj by name
        elif match('[\-\w\d]+', oid):
            search_field = 'name'
            entity = self.query_entities(entityclass,
                                         session,
                                         name=oid,
                                         **kvargs)

        res = entity.one_or_none()
        resp = False
        if res is not None:
            resp = True

        self.logger.debug2('Check entity %s by %s %s exists: %s' %
                           (entityclass.__name__, search_field, oid, resp))
        return resp
Example #7
0
    def query_entities(self,
                       entityclass,
                       session,
                       oid=None,
                       objid=None,
                       uuid=None,
                       name=None,
                       *args,
                       **kvargs):
        """Get model entities query
        
        :param entityclass: entity model class
        :param session: db session
        :param int oid: entity id. [optional]
        :param str objid: entity authorization id. [optional]
        :param str uuid: entity uuid. [optional]
        :param str name: entity name. [optional]
        :return: list of entityclass
        :raises ModelError: raise :class:`ModelError`      
        """
        #session = self.get_session()
        if oid is not None:
            query = session.query(entityclass).filter_by(id=oid)
        elif objid is not None:
            query = session.query(entityclass).filter_by(objid=objid)
        elif uuid is not None:
            query = session.query(entityclass).filter_by(uuid=uuid)
        elif name is not None:
            query = session.query(entityclass).filter_by(name=name)
        else:
            query = session.query(entityclass)

        entity = query.first()

        if entity is None:
            msg = u'No %s found' % entityclass.__name__
            self.logger.error(msg)
            raise ModelError(msg, code=404)

        self.logger.debug(u'Get %s: %s' %
                          (entityclass.__name__, truncate(entity)))
        return entity
Example #8
0
    def get_event(self, oid):
        """Method used by authentication manager
        
        :param oid: can be db id or event_id
        :return: DbEvent instance
        """
        session = self.get_session()

        # get obj by uuid
        if match('[0-9a-z]+', str(oid)):
            query = session.query(DbEvent).filter_by(event_id=oid)
        # get obj by id
        elif match('[0-9]+', str(oid)):
            query = session.query(DbEvent).filter_by(id=oid)

        entity = query.first()

        if entity is None:
            msg = 'No event found'
            self.logger.error(msg)
            raise ModelError(msg, code=404)

        self.logger.debug('Get event: %s' % (truncate(entity)))
        return entity