def get_resource_by_id(user, resource, table, embed_many, ignore_columns=None): args = schemas.args(flask.request.args.to_dict()) resource_name = table.name[0:-1] resource_id = resource['id'] columns = v1_utils.get_columns_name_with_objects(table) query = v1_utils.QueryBuilder(table, args, columns, ignore_columns) if not auth.is_admin(user) and 'team_id' in resource: query.add_extra_condition(table.c.team_id == user['team_id']) if 'state' in resource: query.add_extra_condition(table.c.state != 'archived') query.add_extra_condition(table.c.id == resource_id) rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, table.name, args['embed'], embed_many) if len(rows) < 1: raise dci_exc.DCINotFound(resource_name, resource_id) resource = rows[0] res = flask.jsonify({resource_name: resource}) if 'etag' in resource: res.headers.add_header('ETag', resource['etag']) return res
def get_remoteci_by_id(user, r_id): remoteci = v1_utils.verify_existence_and_get(r_id, _TABLE) if user.is_not_in_team(remoteci['team_id']) and user.is_not_epm(): raise dci_exc.DCINotFound('RemoteCI', remoteci['id']) return base.get_resource_by_id(user, remoteci, _TABLE, _EMBED_MANY, ignore_columns=["keys", "cert_fp"])
def get_remoteci_data_json(user, r_id): query = v1_utils.QueryBuilder(_TABLE, {}, _R_COLUMNS) if user.is_not_super_admin() and user.is_not_epm(): query.add_extra_condition(_TABLE.c.team_id.in_(user.teams_ids)) query.add_extra_condition(_TABLE.c.id == r_id) row = query.execute(fetchone=True) if row is None: raise dci_exc.DCINotFound('RemoteCI', r_id) return row['remotecis_data']
def get_remoteci_data_json(user, r_id): query = v1_utils.QueryBuilder(_TABLE, {}, _R_COLUMNS) if not auth.is_admin(user): query.add_extra_condition(_TABLE.c.team_id == user['team_id']) query.add_extra_condition(_TABLE.c.id == r_id) row = query.execute(fetchone=True) if row is None: raise dci_exc.DCINotFound('RemoteCI', r_id) return row['remotecis_data']
def list_component_file(user, c_id, f_id): component = v1_utils.verify_existence_and_get(c_id, _TABLE) auth.check_export_control(user, component) v1_utils.verify_team_in_topic(user, component['topic_id']) COMPONENT_FILES = models.COMPONENT_FILES where_clause = sql.and_(COMPONENT_FILES.c.id == f_id, COMPONENT_FILES.c.component_id == c_id) query = sql.select([COMPONENT_FILES]).where(where_clause) component_f = flask.g.db_conn.execute(query).fetchone() if component_f is None: raise dci_exc.DCINotFound('Component File', f_id) res = flask.jsonify({'component_file': component_f}) return res
def _get_job(user, job_id, embed): # build the query thanks to the QueryBuilder class args = {'embed': embed} query = v1_utils.QueryBuilder(_TABLE, args, _JOBS_COLUMNS) if not user.is_super_admin(): query.add_extra_condition(_TABLE.c.team_id.in_(user.teams)) query.add_extra_condition(_TABLE.c.id == job_id) 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) if len(rows) != 1: raise dci_exc.DCINotFound('Job', job_id) job = rows[0] return job, nb_rows
def get_resource_by_id(user, resource, table, embed_many=None, ignore_columns=None, resource_name=None, embeds=None, jsonify=True): args = check_and_get_args(flask.request.args.to_dict()) if embeds is not None: # make a copy of the list to avoid side effect args['embed'] = args['embed'] + list(embeds) resource_name = resource_name or table.name[0:-1] resource_id = resource['id'] columns = v1_utils.get_columns_name_with_objects(table) query = v1_utils.QueryBuilder(table, args, columns, ignore_columns) if 'state' in resource: query.add_extra_condition(table.c.state != 'archived') query.add_extra_condition(table.c.id == resource_id) rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, table.name, args['embed'], embed_many) if len(rows) < 1: raise dci_exc.DCINotFound(resource_name, resource_id) resource = rows[0] if jsonify is True: res = flask.jsonify({resource_name: resource}) if 'etag' in resource: res.headers.add_header('ETag', resource['etag']) return res else: return resource
def head(self, filename): try: return self.connection.head_object(self.container, filename) except swiftclient.exceptions.ClientException: raise exceptions.DCINotFound('Content File', filename)
def create_files(user): file_info = get_file_info_from_headers(dict(flask.request.headers)) swift = dci_config.get_store('files') values = dict.fromkeys( ['md5', 'mime', 'jobstate_id', 'job_id', 'name', 'test_id']) values.update(file_info) if values.get('jobstate_id') is None and values.get('job_id') is None: raise dci_exc.DCIException('HTTP headers DCI-JOBSTATE-ID or ' 'DCI-JOB-ID must be specified') if values.get('name') is None: raise dci_exc.DCIException('HTTP header DCI-NAME must be specified') if values['jobstate_id']: query = v1_utils.QueryBuilder(models.JOBSTATES) query.add_extra_condition( models.JOBSTATES.c.id == values['jobstate_id']) row = query.execute(fetchone=True) if row is None: raise dci_exc.DCINotFound('Jobstate', values['jobstate_id']) values['job_id'] = row['jobstates_job_id'] query = v1_utils.QueryBuilder(models.JOBS) if not auth.is_admin(user): query.add_extra_condition(models.JOBS.c.team_id == user['team_id']) query.add_extra_condition(models.JOBS.c.id == values['job_id']) row = query.execute(fetchone=True) if row is None: raise dci_exc.DCINotFound('Job', values['job_id']) file_id = utils.gen_uuid() # ensure the directory which will contains the file actually exist file_path = swift.build_file_path(user['team_id'], values['job_id'], file_id) content = files.get_stream_or_content_from_request(flask.request) swift.upload(file_path, content) s_file = swift.head(file_path) etag = utils.gen_etag() values.update({ 'id': file_id, 'created_at': datetime.datetime.utcnow().isoformat(), 'updated_at': datetime.datetime.utcnow().isoformat(), 'team_id': user['team_id'], 'md5': None, 'size': s_file['content-length'], 'state': 'active', 'etag': etag, }) query = _TABLE.insert().values(**values) with flask.g.db_conn.begin(): flask.g.db_conn.execute(query) result = json.dumps({'file': values}) if values['mime'] == 'application/junit': _, file_descriptor = swift.get(file_path) junit = tsfm.junit2dict(file_descriptor.read()) query = models.TESTS_RESULTS.insert().values({ 'id': utils.gen_uuid(), 'created_at': values['created_at'], 'updated_at': datetime.datetime.utcnow().isoformat(), 'file_id': file_id, 'job_id': values['job_id'], 'name': values['name'], 'success': junit['success'], 'failures': junit['failures'], 'errors': junit['errors'], 'skips': junit['skips'], 'total': junit['total'], 'time': junit['time'] }) flask.g.db_conn.execute(query) files_events.create_event(file_id, models.FILES_CREATE) return flask.Response(result, 201, content_type='application/json')