def get_all_jobstates(user, j_id=None): """Get all jobstates. """ args = schemas.args(flask.request.args.to_dict()) query = v1_utils.QueryBuilder(_TABLE, args, _JS_COLUMNS) if not user.is_super_admin(): query.add_extra_condition(_TABLE.c.team_id.in_(user.teams)) # used for counting the number of rows when j_id is not None if j_id is not None: query.add_extra_condition(_TABLE.c.job_id == j_id) # get the number of rows for the '_meta' section nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) return flask.jsonify({'jobstates': rows, '_meta': {'count': nb_rows}})
def get_all_topics(user): args = schemas.args(flask.request.args.to_dict()) # if the user is an admin then he can get all the topics query = v1_utils.QueryBuilder(_TABLE, args, _T_COLUMNS) if not auth.is_admin(user): if 'teams' in args['embed']: raise dci_exc.DCIException('embed=teams not authorized.', status_code=401) query.add_extra_condition( _TABLE.c.id.in_(v1_utils.user_topic_ids(user))) # noqa query.add_extra_condition(_TABLE.c.state != 'archived') # get the number of rows for the '_meta' section nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) return flask.jsonify({'topics': rows, '_meta': {'count': nb_rows}})
def get_all_remotecis(user, t_id=None): args = schemas.args(flask.request.args.to_dict()) # build the query thanks to the QueryBuilder class query = v1_utils.QueryBuilder(_TABLE, args, _R_COLUMNS) # If it's not an admin then restrict the view to the team's file if not auth.is_admin(user): query.add_extra_condition(_TABLE.c.team_id == user['team_id']) if t_id is not None: query.add_extra_condition(_TABLE.c.team_id == t_id) query.add_extra_condition(_TABLE.c.state != 'archived') rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) return flask.jsonify({'remotecis': rows, '_meta': {'count': len(rows)}})
def list_components_files(user, c_id): component = v1_utils.verify_existence_and_get(c_id, _TABLE) v1_utils.verify_team_in_topic(user, component['topic_id']) args = schemas.args(flask.request.args.to_dict()) query = v1_utils.QueryBuilder(models.COMPONENTFILES, args, _CF_COLUMNS) query.add_extra_condition(models.COMPONENTFILES.c.component_id == c_id) nb_rows = query.get_number_of_rows( models.COMPONENTFILES, models.COMPONENTFILES.c.component_id == c_id) # noqa rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, models.COMPONENTFILES.name, None, None) return flask.jsonify({ 'component_files': rows, '_meta': { 'count': nb_rows } })
def get_all_roles(user): args = schemas.args(flask.request.args.to_dict()) query = v1_utils.QueryBuilder(_TABLE, args, _T_COLUMNS) query.add_extra_condition(_TABLE.c.state != 'archived') if not auth.is_admin(user): query.add_extra_condition(_TABLE.c.label != 'SUPER_ADMIN') if user['role_id'] not in [ auth.get_role_id('ADMIN'), auth.get_role_id('SUPER_ADMIN') ]: query.add_extra_condition(_TABLE.c.id == user['role_id']) nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) return flask.jsonify({'roles': rows, '_meta': {'count': nb_rows}})
def get_all_configurations(user, r_id): args = schemas.args(flask.request.args.to_dict()) remoteci = v1_utils.verify_existence_and_get(r_id, _TABLE) if not user.is_in_team(remoteci['team_id']): raise auth.UNAUTHORIZED query = sql.select([_RCONFIGURATIONS]). \ select_from(models.JOIN_REMOTECIS_RCONFIGURATIONS. join(_RCONFIGURATIONS)). \ where(models.JOIN_REMOTECIS_RCONFIGURATIONS.c.remoteci_id == r_id) query = query.where(_RCONFIGURATIONS.c.state != 'archived') sort_list = v1_utils.sort_query(args['sort'], _RCONFIGURATIONS_COLUMNS) where_list = v1_utils.where_query(args['where'], _RCONFIGURATIONS, _RCONFIGURATIONS_COLUMNS) query = v1_utils.add_sort_to_query(query, sort_list) query = v1_utils.add_where_to_query(query, where_list) if args.get('limit', None): query = query.limit(args.get('limit')) if args.get('offset', None): query = query.offset(args.get('offset')) rows = flask.g.db_conn.execute(query).fetchall() query_nb_rows = sql.select([func.count(_RCONFIGURATIONS.c.id)]). \ select_from(models.JOIN_REMOTECIS_RCONFIGURATIONS. join(_RCONFIGURATIONS)). \ where(models.JOIN_REMOTECIS_RCONFIGURATIONS.c.remoteci_id == r_id). \ where(_RCONFIGURATIONS.c.state != 'archived') nb_rows = flask.g.db_conn.execute(query_nb_rows).scalar() res = flask.jsonify({'rconfigurations': rows, '_meta': {'count': nb_rows}}) res.status_code = 200 return res
def list_jobdefinitions(user, topic_ids, by_topic): """Get all jobdefinitions. If topic_id is not None, then return all the jobdefinitions with a topic pointed by topic_id. """ # get the diverse parameters args = schemas.args(flask.request.args.to_dict()) query = v1_utils.QueryBuilder(_TABLE, args, _JD_COLUMNS) if by_topic or not auth.is_admin(user): query.add_extra_condition(_TABLE.c.topic_id.in_(topic_ids)) query.add_extra_condition(_TABLE.c.state != 'archived') nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) return flask.jsonify({'jobdefinitions': rows, '_meta': {'count': nb_rows}})
def get_logs(user): args = schemas.args(flask.request.args.to_dict()) if user['role_id'] not in [ auth.get_role_id('ADMIN'), auth.get_role_id('SUPER_ADMIN') ]: raise auth.UNAUTHORIZED if args['limit'] is None: args['limit'] = 10 query = v1_utils.QueryBuilder(_TABLE, args, _A_COLUMNS) if not auth.is_admin(user): query.add_extra_condition(_TABLE.c.team_id == user['team_id']) nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], None) return flask.jsonify({'audits': rows, '_meta': {'count': nb_rows}})
def get_all_components(user, topic_id): """Get all components of a topic.""" args = schemas.args(flask.request.args.to_dict()) v1_utils.verify_team_in_topic(user, topic_id) query = v1_utils.QueryBuilder(_TABLE, args, _C_COLUMNS) query.add_extra_condition( sql.and_(_TABLE.c.topic_id == topic_id, _TABLE.c.state != 'archived')) nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) # Return only the component which have the export_control flag set to true # if not (auth.is_admin(user)): rows = [row for row in rows if row['export_control']] return flask.jsonify({'components': rows, '_meta': {'count': nb_rows}})
def search_jobs(user): values = schemas.job_search.post(flask.request.json) jobdefinition_id = values.get('jobdefinition_id') configuration = values.get('configuration') config_op = configuration.pop('_op', 'and') args = schemas.args(flask.request.args.to_dict()) query = v1_utils.QueryBuilder(_TABLE, args, _JOBS_COLUMNS, ['configuration']) # If it's not an admin then restrict the view to the team's file if not auth.is_admin(user): query.add_extra_condition(_TABLE.c.team_id == user['team_id']) if jobdefinition_id is not None: query.add_extra_condition(_TABLE.c.jobdefinition_id == jobdefinition_id) # noqa if config_op == 'and': sa_op = sql.expression.and_ elif config_op == 'or': sa_op = sql.expression.or_ filering_rules = [] for k, v in six.iteritems(configuration): path = [] for sk in k.split('.'): path.append(sk) filering_rules.append(_TABLE.c.configuration[path].astext == v) query.add_extra_condition(sa_op(*filering_rules)) nb_rows = query.get_number_of_rows() rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, _TABLE.name, args['embed'], _EMBED_MANY) return flask.jsonify({'jobs': rows, '_meta': {'count': nb_rows}})
def test_args(self): assert schemas.args(self.data) == self.data_expected
def test_extra_args(self): extra_data = utils.dict_merge(self.data, {'foo': 'bar'}) assert schemas.args(extra_data) == self.data_expected
def invalid_args(data, errors): with pytest.raises(exceptions.DCIException) as exc: schemas.args(data) assert exc.value.payload == {'errors': errors}