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)
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)
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)