def iter_environment_level_values(environment, levels): app.logger.debug("Getting environment level values. Env: %s, " "levels: %s", environment.id, levels) env_levels = db.EnvironmentHierarchyLevel.get_for_environment(environment) app.logger.debug("Environment levels got. Env: %s, levels: %s", environment.id, [l.name for l in env_levels]) if len(env_levels) < len(levels): raise errors.TuningboxNotFound( "Levels {0} can't be matched with " "environment {1} levels: {2}".format( [l[0] for l in levels], environment.id, [l.name for l in env_levels] ) ) level_pairs = zip(env_levels, levels) for env_level, (level_name, level_value) in level_pairs: if env_level.name != level_name: raise errors.TuningboxNotFound( "Unexpected level name '{0}'. Expected '{1}'.".format( level_name, env_level.name) ) level_value_db = db.get_or_create( db.EnvironmentHierarchyLevelValue, level=env_level, value=level_value, ) yield level_value_db
def get_by_id_or_name(self, id_or_name, fail_on_none=True): if isinstance(id_or_name, int): result = self.get(id_or_name) else: result = self.filter_by(name=id_or_name).one_or_none() if fail_on_none and result is None: raise errors.TuningboxNotFound( "Object not found by name or id {0}".format(id_or_name)) return result
def load_objects(model, ids): if ids is None: return None result = [] for obj_id in ids: obj = model.query.filter_by(id=obj_id).one_or_none() if obj is None: raise errors.TuningboxNotFound( "{0} not found by identifier: " "{1}".format(model.__tablename__, obj_id) ) result.append(obj) return result
def load_objects_by_id_or_name(model, identifiers): if identifiers is None: return None result = [] for identifier in identifiers: obj = model.query.get_by_id_or_name( identifier, fail_on_none=False) if obj is None: raise errors.TuningboxNotFound( "{0} not found by identifier: " "{1}".format(model.__tablename__, identifier) ) result.append(obj) return result
def get_resource_values(environment, levels, res_def): level_value = hierarchy_levels.get_environment_level_value( environment, levels) res_values = db.ResourceValues.query.filter_by( environment_id=environment.id, resource_definition_id=res_def.id, level_value=level_value, ).all() if not res_values: raise errors.TuningboxNotFound( "Resource values not found by environment {0}, " "resource definition {1} for levels {2}".format( environment.id, res_def.id, levels ) ) elif len(res_values) > 1: raise errors.TuningboxIntegrityError( "Found more than one resource values for environment {0}, " "resource definition {1} for levels {2}".format( environment.id, res_def.id, levels ) ) return res_values[0]
def get_resource_definition(id_or_name, environment_id): query = db.ResourceDefinition.query.join(db.Component). \ join(db.Environment.environment_components_table). \ filter_by(environment_id=environment_id) if isinstance(id_or_name, int): query = query.filter(db.ResourceDefinition.id == id_or_name) else: query = query.filter(db.ResourceDefinition.name == id_or_name) result = query.all() if not result: raise errors.TuningboxNotFound( "{0} not found by {1} in environment {2}".format( db.ResourceDefinition.__tablename__, id_or_name, environment_id ) ) elif len(result) > 1: raise sa_exc.MultipleResultsFound return result[0]
def find_or_404(cls, **attrs): item = cls.query.filter_by(**attrs).one_or_none() if not item: raise errors.TuningboxNotFound("{0} not found by {1}".format( cls.__name__, attrs)) return item
def get_or_404(cls, ident): result = cls.query.get(ident) if result is None: raise errors.TuningboxNotFound("{0} not found by id {1}".format( cls.__name__, ident)) return result