Exemplo n.º 1
0
class GlobalParameterDbApi(TortugaDbApi):
    """
    Global Parameter DB API class
    """

    def __init__(self):
        TortugaDbApi.__init__(self)

        self._globalParametersDbHandler = GlobalParametersDbHandler()

    def getParameter(self, session: Session, name: str) -> Parameter:
        """
        Returns the named parameter
        """

        try:
            dbParam = self._globalParametersDbHandler.getParameter(
                session, name)

            return Parameter.getFromDbDict(dbParam.__dict__)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def getParameterList(self, session: Session) -> TortugaObjectList:
        """
        Get list of all available parameters from the db.

            Returns:
                [parameter]
            Throws:
                DbError
        """

        try:
            dbParameters = self._globalParametersDbHandler.getParameterList(
                session)

            return self.getTortugaObjectList(Parameter, dbParameters)
        except TortugaException:
            raise
        except Exception as ex:
            self._logger.exception(str(ex))
            raise

    def addParameter(self, session: Session, parameter: Parameter) -> None:
        """
        Insert parameter into the db.

            Returns:
                parameterId
            Throws:
                ParameterAlreadyExists
                DbError
        """

        try:
            self._globalParametersDbHandler.addParameter(
                session, parameter)

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise

    def upsertParameter(self, session: Session, parameter: Parameter) -> None:
        try:
            self._globalParametersDbHandler.upsertParameter(
                session, parameter.getName(), parameter.getValue(),
                description=parameter.getDescription())

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception:
            session.rollback()
            self._logger.exception('upsertParameter failed')
            raise

    def deleteParameter(self, session: Session, name: str) -> None:
        """
        Delete parameter from the db.

            Returns:
                None
            Throws:
                ParameterNotFound
                DbError
        """

        try:
            p = self._globalParametersDbHandler.getParameter(session, name)

            self._logger.debug('Deleting parameter [%s]' % (name))

            session.delete(p)

            session.commit()

            self._logger.info('Deleted parameter [%s]' % name)
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise
Exemplo n.º 2
0
class GlobalParameterDbApi(TortugaDbApi):
    """
    Global Parameter DB API class
    """

    def __init__(self):
        TortugaDbApi.__init__(self)

        self._globalParametersDbHandler = GlobalParametersDbHandler()

    def getParameter(self, name):
        """
        Returns the named parameter
        """

        session = DbManager().openSession()

        try:
            dbParam = self._globalParametersDbHandler.getParameter(
                session, name)

            return Parameter.getFromDbDict(dbParam.__dict__)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def getParameterList(self):
        """
        Get list of all available parameters from the db.

            Returns:
                [parameter]
            Throws:
                DbError
        """

        session = DbManager().openSession()

        try:
            dbParameters = self._globalParametersDbHandler.getParameterList(
                session)

            return self.getTortugaObjectList(Parameter, dbParameters)
        except TortugaException:
            raise
        except Exception as ex:
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def addParameter(self, parameter):
        """
        Insert parameter into the db.

            Returns:
                parameterId
            Throws:
                ParameterAlreadyExists
                DbError
        """

        session = DbManager().openSession()

        try:
            self._globalParametersDbHandler.addParameter(
                session, parameter)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()

    def upsertParameter(self, parameter):
        session = DbManager().openSession()

        try:
            self._globalParametersDbHandler.upsertParameter(
                session, parameter.getName(), parameter.getValue(),
                description=parameter.getDescription())

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception:
            session.rollback()
            self.getLogger().exception('upsertParameter failed')
            raise
        finally:
            DbManager().closeSession()

    def deleteParameter(self, name):
        """
        Delete parameter from the db.

            Returns:
                None
            Throws:
                ParameterNotFound
                DbError
        """

        session = DbManager().openSession()

        try:
            self._globalParametersDbHandler.deleteParameter(session, name)

            session.commit()
        except TortugaException as ex:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
        finally:
            DbManager().closeSession()