Example #1
0
 def update_mapping(self, uuid, **kwargs):
     session = db.get_session()
     try:
         with session.begin():
             q = session.query(models.HashMapMapping)
             q = q.filter(models.HashMapMapping.mapping_id == uuid)
             mapping_db = q.with_lockmode('update').one()
             if kwargs:
                 # NOTE(sheeprine): We want to check that value is not set
                 # to a None value.
                 if mapping_db.field_id and not kwargs.get('value', 'GOOD'):
                     raise api.ClientHashMapError('You must specify a value'
                                                  ' for a field mapping.')
                 # Resolve FK
                 if 'group_id' in kwargs:
                     group_id = kwargs.pop('group_id')
                     if group_id:
                         group_db = self.get_group(group_id)
                         mapping_db.group_id = group_db.id
                 # Service and Field shouldn't be updated
                 excluded_cols = ['mapping_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(mapping_db, attribute):
                         setattr(mapping_db, attribute, value)
                     else:
                         raise api.ClientHashMapError(
                             'No such attribute: {}'.format(attribute))
             else:
                 raise api.ClientHashMapError('No attribute to update.')
             return mapping_db
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchMapping(uuid)
Example #2
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)
Example #3
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
Example #4
0
 def create_threshold(self,
                      level,
                      cost,
                      map_type='rate',
                      service_id=None,
                      field_id=None,
                      group_id=None,
                      tenant_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.')
     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():
             threshold_db = models.HashMapThreshold(
                 threshold_id=uuidutils.generate_uuid(),
                 level=level,
                 cost=cost,
                 field_id=field_fk,
                 service_id=service_fk,
                 map_type=map_type,
                 tenant_id=tenant_id)
             if group_fk:
                 threshold_db.group_id = group_fk
             session.add(threshold_db)
     except exception.DBDuplicateEntry:
         if field_id:
             puuid = field_id
             ptype = 'field'
         else:
             puuid = service_id
             ptype = 'service'
         raise api.ThresholdAlreadyExists(level, 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.
     threshold_db = self.get_threshold(threshold_db.threshold_id)
     return threshold_db
Example #5
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]
Example #6
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]
Example #7
0
 def delete_service(self, name=None, uuid=None):
     session = db.get_session()
     q = utils.model_query(models.HashMapService, session)
     if name:
         q = q.filter(models.HashMapService.name == name)
     elif uuid:
         q = q.filter(models.HashMapService.service_id == uuid)
     else:
         raise api.ClientHashMapError(
             'You must specify either name or uuid.')
     r = q.delete()
     if not r:
         raise api.NoSuchService(name, uuid)
Example #8
0
 def get_service(self, name=None, uuid=None):
     session = db.get_session()
     try:
         q = session.query(models.HashMapService)
         if name:
             q = q.filter(models.HashMapService.name == name)
         elif uuid:
             q = q.filter(models.HashMapService.service_id == uuid)
         else:
             raise api.ClientHashMapError(
                 'You must specify either name or uuid.')
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchService(name=name, uuid=uuid)
Example #9
0
 def get_field(self, uuid=None, service_uuid=None, name=None):
     session = db.get_session()
     try:
         q = session.query(models.HashMapField)
         if uuid:
             q = q.filter(models.HashMapField.field_id == uuid)
         elif service_uuid and name:
             q = q.join(models.HashMapField.service)
             q = q.filter(models.HashMapService.service_id == service_uuid,
                          models.HashMapField.name == name)
         else:
             raise api.ClientHashMapError('You must specify either an uuid'
                                          ' or a service_uuid and a name.')
         res = q.one()
         return res
     except sqlalchemy.orm.exc.NoResultFound:
         raise api.NoSuchField(uuid)