Exemplo n.º 1
0
    def save(self, session=None):
        from cloudkitty import db

        if session is None:
            session = db.get_session()

        super(HashMapBase, self).save(session=session)
Exemplo n.º 2
0
 def update_script(self, uuid, **kwargs):
     session = db.get_session()
     try:
         with session.begin():
             q = session.query(models.PyScriptsScript)
             q = q.filter(
                 models.PyScriptsScript.script_id == uuid
             )
             script_db = q.with_lockmode('update').one()
             if kwargs:
                 excluded_cols = ['script_id']
                 for col in excluded_cols:
                     if col in kwargs:
                         kwargs.pop(col)
                 for attribute, value in kwargs.items():
                     if hasattr(script_db, attribute):
                         setattr(script_db, attribute, value)
                     else:
                         raise ValueError('No such attribute: {}'.format(
                             attribute))
             else:
                 raise ValueError('No attribute to update.')
             return script_db
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchScript(uuid=uuid)
Exemplo n.º 3
0
 def get_metadata(self, name):
     session = db.get_session()
     q = utils.model_query(
         models.StateInfo,
         session)
     q.filter(models.StateInfo.name == name)
     return q.value(models.StateInfo.s_metadata)
Exemplo n.º 4
0
    def count_resource_rate_list(self, tenant_ids=None, service=None, acces_monthly_billing_table=False, **kwargs):
        model = models.MonthBillingData if acces_monthly_billing_table else models.RatedDataFrame


        session = db.get_session()
        if acces_monthly_billing_table:
            q = session.query(model.resource_id.label('resource_id'), model.res_type.label('res_type'), model.rate.label('rate'))
        else:
            q = session.query(model.resource_id.label('resource_id'), model.res_type.label('res_type'), sqlalchemy.func.sum(model.rate).label('rate'))
        if tenant_ids:
            q = q.filter(
                model.tenant_id.in_(tenant_ids)
            )
        if service:
            q = q.filter(model.res_type == service)
        
        #resources_rate = []
        if acces_monthly_billing_table:
            q = q.filter(
            model.billing_month == kwargs.pop('billing_month'),
            model.res_type != '_NO_DATA_',
            )
        else:
            q = q.filter(
            model.begin >= kwargs.pop('begin'),
            model.end <= kwargs.pop('end'),
            model.res_type != '_NO_DATA_',
            ).group_by(model.resource_id)
        
        
        resources_rate = q.all()
        
        
        return len(resources_rate)
Exemplo n.º 5
0
 def stop_fixture(self):
     model = models.RatedDataFrame
     session = db.get_session()
     q = utils.model_query(
         model,
         session)
     q.delete()
Exemplo n.º 6
0
    def add_time_frame_custom(self, **kwargs):
        """Create a new time frame custom .

        :param begin: Start of the dataframe.
        :param end: End of the dataframe.
        :param tenant_id: tenant_id of the dataframe owner.
        :param unit: Unit of the metric.
        :param qty: Quantity of the metric.
        :param res_type: Type of the resource.
        :param rate: Calculated rate for this dataframe.
        :param desc: Resource description (metadata).
        """

        session = db.get_session()

        # Add invoice details
        frame = models.RatedDataFrame(  
                                        begin = kwargs.get('begin'),
                                        end = kwargs.get('end'),
                                        tenant_id = kwargs.get('tenant_id'),
                                        unit = kwargs.get('unit'),
                                        qty = kwargs.get('qty'),
                                        res_type = kwargs.get('res_type'),
                                        rate = decimal.Decimal(kwargs.get('rate')),
                                        desc = json.dumps(kwargs.get('desc')))
        try:
            with session.begin():
                session.add(frame)

        except sqlalchemy.exc.IntegrityError, exc:
                reason = exc.message
Exemplo n.º 7
0
    def show_aggregated_resource_rate(self, begin=None, end=None, tenant_ids=None, resource_id=None):
        model = models.AggregatedDataFrame

        # Boundary calculation
        if not begin:
            begin = ck_utils.get_month_start()
        if not end:
            end = ck_utils.get_next_month()

        session = db.get_session()
        q = session.query(model.begin.label('begin'), model.end.label('end'), 
                          model.rate.label('rate'), model.qty.label('qty'), 
                          model.unit.label('unit'), model.unit_price.label('unit_price'),
                          model.begining_event_type.label('begin_event'), model.ending_event_type.label('end_event'))
        if tenant_ids:
            q = q.filter(
                model.tenant_id.in_(tenant_ids)
            )

        q = q.filter(
            model.begin >= begin,
            model.end <= end,
            model.res_type != '_NO_DATA_',
            model.resource_id == resource_id
            )
            
        resources_usage_rate = q.all()
        
        return [model.to_resource_usage_rate(entry) for entry in resources_usage_rate]
Exemplo n.º 8
0
 def delete_mapping(self, service):
     session = db.get_session()
     q = utils.model_query(models.ServiceToCollectorMapping, session)
     q = q.filter(models.ServiceToCollectorMapping.service == service)
     r = q.delete()
     if not r:
         raise api.NoSuchMapping(service)
Exemplo n.º 9
0
    def list_mappings(self,
                      service_uuid=None,
                      field_uuid=None,
                      group_uuid=None,
                      no_group=False,
                      **kwargs):

        session = db.get_session()
        q = session.query(models.HashMapMapping)
        if service_uuid:
            q = q.join(
                models.HashMapMapping.service)
            q = q.filter(
                models.HashMapService.service_id == service_uuid)
        elif field_uuid:
            q = q.join(
                models.HashMapMapping.field)
            q = q.filter(models.HashMapField.field_id == field_uuid)
        elif not service_uuid and not field_uuid and not group_uuid:
            raise api.ClientHashMapError(
                'You must specify either service_uuid,'
                ' field_uuid or group_uuid.')
        if 'tenant_uuid' in kwargs:
            q = q.filter(
                models.HashMapMapping.tenant_id == kwargs.get('tenant_uuid'))
        if group_uuid:
            q = q.join(
                models.HashMapMapping.group)
            q = q.filter(models.HashMapGroup.group_id == group_uuid)
        elif no_group:
            q = q.filter(models.HashMapMapping.group_id == None)  # noqa
        res = q.values(
            models.HashMapMapping.mapping_id)
        return [uuid[0] for uuid in res]
Exemplo n.º 10
0
    def save(self, session=None):
        from cloudkitty import db

        if session is None:
            session = db.get_session()

        super(HashMapBase, self).save(session=session)
Exemplo n.º 11
0
    def get_invoice(self, tenant_id=None, tenant=None, invoice_id=None, payment_status=None):

        model = models.InvoiceDetails
        session = db.get_session()

        # Fetch the invoice using tenant ID
        if tenant_id:
                q = session.query(model).order_by(model.id).filter(model.tenant_id == tenant_id)

        # Fetch the invoices using tenant name input
        if tenant:
                q = session.query(model).order_by(model.id).filter(model.tenant_name == tenant)

        # Fetch the invoice using invoice ID
        if invoice_id:
                q = session.query(model).order_by(model.id).filter(model.invoice_id == invoice_id)

        # Fetch the invoice using Payment status
        if payment_status:
                q = session.query(model).order_by(model.id).filter(model.payment_status == payment_status)

        # Fetch all the values
        r = q.all()

        return [entry.to_cloudkitty() for entry in r]
Exemplo n.º 12
0
    def add_invoice(self, invoice_id, invoice_date, invoice_period_from, invoice_period_to, tenant_id, invoice_data, tenant_name, total_cost, paid_cost, balance_cost, payment_status):
        """Create a new invoice entry.

        """

        session = db.get_session()

        # Add invoice details
        invoice = models.InvoiceDetails(
                                        invoice_date = invoice_date,
                                        invoice_period_from = invoice_period_from,
                                        invoice_period_to = invoice_period_to,
                                        tenant_id = tenant_id,
                                        invoice_id = invoice_id,
                                        invoice_data = invoice_data,
                                        tenant_name = tenant_name,
                                        total_cost = total_cost,
                                        paid_cost = paid_cost,
                                        balance_cost = balance_cost,
                                        payment_status = payment_status) 
        try:
            with session.begin():
                session.add(invoice)

        except sqlalchemy.exc.IntegrityError, exc:
                reason = exc.message
Exemplo n.º 13
0
    def list_thresholds(self,
                        service_uuid=None,
                        field_uuid=None,
                        group_uuid=None,
                        no_group=False):

        session = db.get_session()
        q = session.query(models.HashMapThreshold)
        if service_uuid:
            q = q.join(
                models.HashMapThreshold.service)
            q = q.filter(
                models.HashMapService.service_id == service_uuid)
        elif field_uuid:
            q = q.join(
                models.HashMapThreshold.field)
            q = q.filter(models.HashMapField.field_id == field_uuid)
        if group_uuid:
            q = q.join(
                models.HashMapThreshold.group)
            q = q.filter(models.HashMapGroup.group_id == group_uuid)
        elif not service_uuid and not field_uuid:
            raise api.ClientHashMapError(
                'You must specify either service_uuid,'
                ' field_uuid or group_uuid.')
        elif no_group:
            q = q.filter(models.HashMapThreshold.group_id == None)  # noqa
        res = q.values(
            models.HashMapThreshold.threshold_id)
        return [uuid[0] for uuid in res]
Exemplo n.º 14
0
 def update_threshold(self, uuid, **kwargs):
     session = db.get_session()
     try:
         with session.begin():
             q = session.query(models.HashMapThreshold)
             q = q.filter(
                 models.HashMapThreshold.threshold_id == uuid)
             threshold_db = q.with_lockmode('update').one()
             if kwargs:
                 # Resolve FK
                 if 'group_id' in kwargs:
                     group_id = kwargs.pop('group_id')
                     if group_id:
                         group_db = self.get_group(group_id)
                         threshold_db.group_id = group_db.id
                 # Service and Field shouldn't be updated
                 excluded_cols = ['threshold_id', 'service_id', 'field_id']
                 for col in excluded_cols:
                     if col in kwargs:
                         kwargs.pop(col)
                 for attribute, value in six.iteritems(kwargs):
                     if hasattr(threshold_db, attribute):
                         setattr(threshold_db, attribute, value)
                     else:
                         raise api.ClientHashMapError(
                             'No such attribute: {}'.format(
                                 attribute))
             else:
                 raise api.ClientHashMapError('No attribute to update.')
             return threshold_db
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchThreshold(uuid)
Exemplo n.º 15
0
 def list_services(self):
     session = db.get_session()
     q = session.query(models.HashMapService)
     res = q.values(
         models.HashMapService.name
     )
     return res
Exemplo n.º 16
0
 def list_fields(self, service_uuid):
     session = db.get_session()
     q = session.query(models.HashMapField)
     q = q.join(models.HashMapField.service)
     q = q.filter(models.HashMapService.service_id == service_uuid)
     res = q.values(models.HashMapField.field_id)
     return [uuid[0] for uuid in res]
Exemplo n.º 17
0
    def get_total(self, begin=None, end=None, tenant_id=None, service=None, instance_id=None):
        model = models.RatedDataFrame

        # Boundary calculation
        if not begin:
            begin = ck_utils.get_month_start()
        if not end:
            end = ck_utils.get_next_month()

        session = db.get_session()
        q = session.query(
            sqlalchemy.func.sum(model.rate).label('rate'))
        if tenant_id:
            q = q.filter(
                models.RatedDataFrame.tenant_id == tenant_id)
        if service:
            q = q.filter(
                models.RatedDataFrame.res_type == service)
        if instance_id:
            q = q.filter(
                models.RatedDataFrame.desc.like('%'+instance_id+'%'))

        q = q.filter(
            model.begin >= begin,
            model.end <= end)
        rate = q.scalar()
        return rate
Exemplo n.º 18
0
    def list_service_rate(self, tenant_ids=None, acces_monthly_billing_table=False, **kwargs):
        model = models.MonthBillingData if acces_monthly_billing_table else models.RatedDataFrame


        session = db.get_session()
        q = session.query(model.res_type.label('res_type'), sqlalchemy.func.count(sqlalchemy.distinct(model.resource_id)).label('qty'), 
                          sqlalchemy.func.sum(model.rate).label('rate'))
        if tenant_ids:
            q = q.filter(
                model.tenant_id.in_(tenant_ids)
            )
        
        services_rate = []
        if acces_monthly_billing_table:
            services_rate = q.filter(model.billing_month == kwargs.pop('billing_month'),
                                     model.res_type != '_NO_DATA_',
                                     ).group_by(model.res_type).all()
        else:
            services_rate = q.filter(
                model.begin >= kwargs.pop('begin'),
                model.end <= kwargs.pop('end'),
                model.res_type != '_NO_DATA_',
                ).group_by(model.res_type).all()
        
        return [models.RatedDataFrame.to_service_rate(entry) for entry in services_rate]
Exemplo n.º 19
0
    def get_all(self,
                identifier=None,
                fetcher=None,
                collector=None,
                scope_key=None,
                active=1,
                limit=100,
                offset=0):
        """Returns the state of all scopes.

        This function returns the state of all scopes with support for optional
        filters.

        :param identifier: optional scope identifiers to filter on
        :type identifier: list
        :param fetcher: optional scope fetchers to filter on
        :type fetcher: list
        :param collector: optional collectors to filter on
        :type collector: list
        :param fetcher: optional fetchers to filter on
        :type fetcher: list
        :param scope_key: optional scope_keys to filter on
        :type scope_key: list
        :param active: optional active to filter scopes by status
                       (active/deactivated)
        :type active: int
        :param limit: optional to restrict the projection
        :type limit: int
        :param offset: optional to shift the projection
        :type offset: int
        """
        session = db.get_session()
        session.begin()

        q = utils.model_query(self.model, session)
        if identifier:
            q = q.filter(
                self.model.identifier.in_(to_list_if_needed(identifier)))
        if fetcher:
            q = q.filter(self.model.fetcher.in_(to_list_if_needed(fetcher)))
        if collector:
            q = q.filter(self.model.collector.in_(
                to_list_if_needed(collector)))
        if scope_key:
            q = q.filter(self.model.scope_key.in_(
                to_list_if_needed(scope_key)))
        if active is not None and active != []:
            q = q.filter(self.model.active.in_(to_list_if_needed(active)))
        q = apply_offset_and_limit(limit, offset, q)

        r = q.all()
        session.close()

        for item in r:
            item.last_processed_timestamp = tzutils.utc_to_local(
                item.last_processed_timestamp)
            item.scope_activation_toggle_date = tzutils.utc_to_local(
                item.scope_activation_toggle_date)
        return r
Exemplo n.º 20
0
 def list_mappings(self, collector=None):
     session = db.get_session()
     q = utils.model_query(models.ServiceToCollectorMapping, session)
     if collector:
         q = q.filter(
             models.ServiceToCollectorMapping.collector == collector)
     res = q.all()
     return res
Exemplo n.º 21
0
 def list_services(self, collector=None):
     session = db.get_session()
     q = utils.model_query(models.ServiceToCollectorMapping, session)
     if collector:
         q = q.filter(
             models.ServiceToCollectorMapping.collector == collector)
     res = q.distinct().values(models.ServiceToCollectorMapping.service)
     return res
Exemplo n.º 22
0
 def get_mapping(self, service):
     session = db.get_session()
     try:
         q = utils.model_query(models.ServiceToCollectorMapping, session)
         q = q.filter(models.ServiceToCollectorMapping.service == service)
         return q.one()
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchMapping(service)
Exemplo n.º 23
0
 def get_metadata(self, name):
     session = db.get_session()
     return utils.model_query(
         models.StateInfo,
         session
     ).filter_by(
         name=name,
     ).value('s_metadata')
Exemplo n.º 24
0
 def get_service(self, service):
     session = db.get_session()
     try:
         q = session.query(models.HashMapService)
         res = q.filter_by(name=service, ).one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchService(service)
Exemplo n.º 25
0
 def get_metadata(self, name):
     session = db.get_session()
     return utils.model_query(
         models.StateInfo,
         session
     ).filter_by(
         name=name,
     ).value('s_metadata')
Exemplo n.º 26
0
 def get_state(self, tenant_id=None):
     session = db.get_session()
     q = utils.model_query(self.state_model, session)
     if tenant_id:
         q = q.filter(self.state_model.tenant_id == tenant_id)
     q = q.order_by(self.state_model.state.desc())
     r = q.first()
     return ck_utils.dt2ts(r.state) if r else None
Exemplo n.º 27
0
 def get_state(self, tenant_id=None):
     session = db.get_session()
     q = utils.model_query(models.RatedDataFrame, session)
     if tenant_id:
         q = q.filter(models.RatedDataFrame.tenant_id == tenant_id)
     r = q.order_by(models.RatedDataFrame.begin.desc()).first()
     if r:
         return ck_utils.dt2ts(r.begin)
Exemplo n.º 28
0
 def get_state(self, identifier,
               fetcher=None, collector=None, scope_key=None):
     session = db.get_session()
     session.begin()
     r = self._get_db_item(
         session, identifier, fetcher, collector, scope_key)
     session.close()
     return ck_utils.dt2ts(r.state) if r else None
Exemplo n.º 29
0
 def get_state(self, name):
     session = db.get_session()
     try:
         return bool(
             utils.model_query(models.ModuleStateInfo, session).filter_by(
                 name=name, ).value('state'))
     except sqlalchemy.orm.exc.NoResultFound:
         return None
Exemplo n.º 30
0
 def list_services(self):
     session = db.get_session()
     q = utils.model_query(
         models.ServiceToCollectorMapping,
         session)
     res = q.distinct().values(
         models.ServiceToCollectorMapping.service)
     return res
Exemplo n.º 31
0
 def get_state(self, tenant_id=None):
     session = db.get_session()
     q = utils.model_query(self.state_model, session)
     if tenant_id:
         q = q.filter(self.state_model.tenant_id == tenant_id)
     q = q.order_by(self.state_model.state.desc())
     r = q.first()
     return ck_utils.dt2ts(r.state) if r else None
Exemplo n.º 32
0
 def create_mapping(self,
                    cost,
                    map_type='rate',
                    value=None,
                    service_id=None,
                    field_id=None,
                    group_id=None):
     if field_id and service_id:
         raise api.ClientHashMapError('You can only specify one parent.')
     elif not service_id and not field_id:
         raise api.ClientHashMapError('You must specify one parent.')
     elif value and service_id:
         raise api.ClientHashMapError(
             'You can\'t specify a value'
             ' and a service_id.')
     elif not value and field_id:
         raise api.ClientHashMapError(
             'You must specify a value'
             ' for a field mapping.')
     field_fk = None
     if field_id:
         field_db = self.get_field(uuid=field_id)
         field_fk = field_db.id
     service_fk = None
     if service_id:
         service_db = self.get_service(uuid=service_id)
         service_fk = service_db.id
     group_fk = None
     if group_id:
         group_db = self.get_group(uuid=group_id)
         group_fk = group_db.id
     session = db.get_session()
     try:
         with session.begin():
             field_map = models.HashMapMapping(
                 mapping_id=uuidutils.generate_uuid(),
                 value=value,
                 cost=cost,
                 field_id=field_fk,
                 service_id=service_fk,
                 map_type=map_type)
             if group_fk:
                 field_map.group_id = group_fk
             session.add(field_map)
     except exception.DBDuplicateEntry:
         if field_id:
             puuid = field_id
             ptype = 'field'
         else:
             puuid = service_id
             ptype = 'service'
         raise api.MappingAlreadyExists(value, puuid, ptype)
     except exception.DBError:
         raise api.NoSuchType(map_type)
     # FIXME(sheeprine): backref are not populated as they used to be.
     #                   Querying the item again to get backref.
     field_map = self.get_mapping(field_map.mapping_id)
     return field_map
Exemplo n.º 33
0
 def delete_mapping(self, uuid):
     session = db.get_session()
     q = utils.model_query(
         models.HashMapMapping,
         session)
     q = q.filter(models.HashMapMapping.mapping_id == uuid)
     r = q.delete()
     if not r:
         raise api.NoSuchMapping(uuid)
Exemplo n.º 34
0
 def delete_threshold(self, uuid):
     session = db.get_session()
     q = utils.model_query(
         models.HashMapThreshold,
         session)
     q = q.filter(models.HashMapThreshold.threshold_id == uuid)
     r = q.delete()
     if not r:
         raise api.NoSuchThreshold(uuid)
Exemplo n.º 35
0
 def get_threshold(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapThreshold)
         q = q.filter(models.HashMapThreshold.threshold_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchThreshold(uuid)
Exemplo n.º 36
0
 def get_mapping(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapMapping)
         q = q.filter(models.HashMapMapping.mapping_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchMapping(uuid)
Exemplo n.º 37
0
 def get_priority(self, name):
     session = db.get_session()
     q = utils.model_query(models.ModuleStateInfo, session)
     q = q.filter(models.ModuleStateInfo.name == name)
     res = q.value(models.ModuleStateInfo.priority)
     if res:
         return int(res)
     else:
         return 1
Exemplo n.º 38
0
 def delete_mapping(self, service):
     session = db.get_session()
     q = utils.model_query(
         models.ServiceToCollectorMapping,
         session)
     q = q.filter(models.ServiceToCollectorMapping.service == service)
     r = q.delete()
     if not r:
         raise api.NoSuchMapping(service)
Exemplo n.º 39
0
 def get_group(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapGroup)
         q = q.filter(models.HashMapGroup.group_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchGroup(uuid=uuid)
Exemplo n.º 40
0
 def get_state(self, tenant_id=None):
     session = db.get_session()
     q = utils.model_query(self.frame_model, session)
     if tenant_id:
         q = q.filter(self.frame_model.tenant_id == tenant_id)
     q = q.order_by(self.frame_model.begin.desc())
     r = q.first()
     if r:
         return ck_utils.dt2ts(r.begin)
Exemplo n.º 41
0
 def get_state(self, name):
     session = db.get_session()
     try:
         q = utils.model_query(models.ModuleStateInfo, session)
         q = q.filter(models.ModuleStateInfo.name == name)
         res = q.value(models.ModuleStateInfo.state)
         return bool(res)
     except sqlalchemy.orm.exc.NoResultFound:
         return None
Exemplo n.º 42
0
 def delete_threshold(self, uuid):
     session = db.get_session()
     q = utils.model_query(
         models.HashMapThreshold,
         session)
     q = q.filter(models.HashMapThreshold.threshold_id == uuid)
     r = q.delete()
     if not r:
         raise api.NoSuchThreshold(uuid)
Exemplo n.º 43
0
 def delete_field(self, service, field):
     session = db.get_session()
     service_db = self.get_service(service)
     r = utils.model_query(models.HashMapField, session).filter_by(
         service_id=service_db.id,
         name=field,
     ).delete()
     if not r:
         raise api.NoSuchField(service, field)
Exemplo n.º 44
0
 def delete_mapping(self, uuid):
     session = db.get_session()
     q = utils.model_query(
         models.HashMapMapping,
         session)
     q = q.filter(models.HashMapMapping.mapping_id == uuid)
     r = q.delete()
     if not r:
         raise api.NoSuchMapping(uuid)
Exemplo n.º 45
0
 def get_field(self, service, field):
     session = db.get_session()
     try:
         service_db = self.get_service(service)
         q = session.query(models.HashMapField)
         res = q.filter_by(service_id=service_db.id, name=field).one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchField(service, field)
Exemplo n.º 46
0
 def get_mapping(self, service, field, key):
     session = db.get_session()
     try:
         field_db = self.get_field(service, field)
         q = session.query(models.HashMapMapping)
         res = q.filter_by(key=key, field_id=field_db.id).one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchMapping(service, field, key)
Exemplo n.º 47
0
 def list_fields(self, service_uuid):
     session = db.get_session()
     q = session.query(models.HashMapField)
     q = q.join(
         models.HashMapField.service)
     q = q.filter(
         models.HashMapService.service_id == service_uuid)
     res = q.values(models.HashMapField.field_id)
     return [uuid[0] for uuid in res]
Exemplo n.º 48
0
 def delete_mapping(self, service, field, key):
     session = db.get_session()
     field = self.get_field(service, field)
     r = utils.model_query(models.HashMapMapping, session).filter_by(
         field_id=field.id,
         key=key,
     ).delete()
     if not r:
         raise api.NoSuchMapping(service, field, key)
Exemplo n.º 49
0
 def create_mapping(self,
                    cost,
                    map_type='rate',
                    value=None,
                    service_id=None,
                    field_id=None,
                    group_id=None):
     if field_id and service_id:
         raise api.ClientHashMapError('You can only specify one parent.')
     elif not service_id and not field_id:
         raise api.ClientHashMapError('You must specify one parent.')
     elif value and service_id:
         raise api.ClientHashMapError('You can\'t specify a value'
                                      ' and a service_id.')
     elif not value and field_id:
         raise api.ClientHashMapError('You must specify a value'
                                      ' for a field mapping.')
     field_fk = None
     if field_id:
         field_db = self.get_field(uuid=field_id)
         field_fk = field_db.id
     service_fk = None
     if service_id:
         service_db = self.get_service(uuid=service_id)
         service_fk = service_db.id
     group_fk = None
     if group_id:
         group_db = self.get_group(uuid=group_id)
         group_fk = group_db.id
     session = db.get_session()
     try:
         with session.begin():
             field_map = models.HashMapMapping(
                 mapping_id=uuidutils.generate_uuid(),
                 value=value,
                 cost=cost,
                 field_id=field_fk,
                 service_id=service_fk,
                 map_type=map_type)
             if group_fk:
                 field_map.group_id = group_fk
             session.add(field_map)
     except exception.DBDuplicateEntry:
         if field_id:
             puuid = field_id
             ptype = 'field'
         else:
             puuid = service_id
             ptype = 'service'
         raise api.MappingAlreadyExists(value, puuid, ptype)
     except exception.DBError:
         raise api.NoSuchType(map_type)
     # FIXME(sheeprine): backref are not populated as they used to be.
     #                   Querying the item again to get backref.
     field_map = self.get_mapping(field_map.mapping_id)
     return field_map
Exemplo n.º 50
0
 def delete_service(self, service):
     session = db.get_session()
     r = utils.model_query(
         models.HashMapService,
         session
     ).filter_by(
         name=service,
     ).delete()
     if not r:
         raise api.NoSuchService(service)
Exemplo n.º 51
0
 def get_mapping(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapMapping)
         q = q.filter(
             models.HashMapMapping.mapping_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchMapping(uuid)
Exemplo n.º 52
0
 def get_group_from_mapping(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapGroup)
         q = q.join(models.HashMapGroup.mappings)
         q = q.filter(models.HashMapMapping.mapping_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.MappingHasNoGroup(uuid=uuid)
Exemplo n.º 53
0
 def get_threshold(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapThreshold)
         q = q.filter(
             models.HashMapThreshold.threshold_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchThreshold(uuid)
Exemplo n.º 54
0
 def get_group_from_threshold(self, uuid):
     session = db.get_session()
     try:
         q = session.query(models.HashMapGroup)
         q = q.join(models.HashMapGroup.thresholds)
         q = q.filter(models.HashMapThreshold.threshold_id == uuid)
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.ThresholdHasNoGroup(uuid=uuid)
Exemplo n.º 55
0
 def get_service(self, service):
     session = db.get_session()
     try:
         q = session.query(models.HashMapService)
         res = q.filter_by(
             name=service,
         ).one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchService(service)
Exemplo n.º 56
0
 def create_group(self, name):
     session = db.get_session()
     try:
         with session.begin():
             group_db = models.HashMapGroup(
                 name=name, group_id=uuidutils.generate_uuid())
             session.add(group_db)
         return group_db
     except exception.DBDuplicateEntry:
         raise api.GroupAlreadyExists(name, group_db.group_id)
Exemplo n.º 57
0
 def list_fields(self, service):
     session = db.get_session()
     service_db = self.get_service(service)
     q = session.query(models.HashMapField)
     res = q.filter_by(
         service_id=service_db.id
     ).values(
         models.HashMapField.name
     )
     return res
Exemplo n.º 58
0
 def list_mappings(self, service, field):
     session = db.get_session()
     field_db = self.get_field(service, field)
     q = session.query(models.HashMapMapping)
     res = q.filter_by(
         field_id=field_db.id
     ).values(
         models.HashMapMapping.key
     )
     return res
Exemplo n.º 59
0
 def set_metadata(self, name, metadata):
     session = db.get_session()
     try:
         db_state = utils.model_query(models.StateInfo, session).filter_by(
             name=name, ).with_lockmode('update').one()
         db_state.s_metadata = metadata
     except sqlalchemy.orm.exc.NoResultFound:
         db_state = models.StateInfo(name=name, s_metadata=metadata)
         session.add(db_state)
     finally:
         session.flush()
Exemplo n.º 60
0
 def get_state(self,
               identifier,
               fetcher=None,
               collector=None,
               scope_key=None):
     session = db.get_session()
     session.begin()
     r = self._get_db_item(session, identifier, fetcher, collector,
                           scope_key)
     session.close()
     return ck_utils.dt2ts(r.state) if r else None