def create_users(user): values = v1_utils.common_values_dict(user) values.update(schemas.user.post(flask.request.json)) if not (auth.is_admin(user) or auth.is_admin_user(user, values['team_id'])): raise auth.UNAUTHORIZED role_id = values.get('role_id', auth.get_role_id('USER')) if not auth.is_admin(user) and role_id == auth.get_role_id('SUPER_ADMIN'): raise auth.UNAUTHORIZED values.update({ 'password': auth.hash_password(values.get('password')), 'role_id': role_id }) query = _TABLE.insert().values(**values) try: flask.g.db_conn.execute(query) except sa_exc.IntegrityError: raise dci_exc.DCICreationConflict(_TABLE.name, 'name') # remove the password in the result for security reasons del values['password'] return flask.Response(json.dumps({'user': values}), 201, headers={'ETag': values['etag']}, content_type='application/json')
def create_users(user): values = flask.request.json check_json_is_valid(create_user_schema, values) values.update(v1_utils.common_values_dict()) if user.is_not_super_admin(): raise dci_exc.Unauthorized() values.update({ 'password': auth.hash_password(values.get('password')), 'fullname': values.get('fullname', values['name']), 'timezone': values.get('timezone', 'UTC'), 'sso_username': None }) query = _TABLE.insert().values(**values) try: flask.g.db_conn.execute(query) except sa_exc.IntegrityError: raise dci_exc.DCICreationConflict(_TABLE.name, 'name') # remove the password in the result for security reasons del values['password'] return flask.Response(json.dumps({'user': values}), 201, headers={'ETag': values['etag']}, content_type='application/json')
def put_user(user, user_id): # get If-Match header if_match_etag = utils.check_and_get_etag(flask.request.headers) values = schemas.user.put(flask.request.json) puser = dict(_verify_existence_and_get_user(user_id)) if puser['id'] != str(user_id): if not (auth.is_admin(user) or auth.is_admin_user(user, puser['team_id'])): raise auth.UNAUTHORIZED # TODO(yassine): if the user wants to change the team, then check its done # by a super admin. ie. team_name='admin' values['etag'] = utils.gen_etag() if 'password' in values: values['password'] = auth.hash_password(values.get('password')) where_clause = sql.and_(_TABLE.c.etag == if_match_etag, _TABLE.c.id == user_id) query = _TABLE.update().where(where_clause).values(**values) result = flask.g.db_conn.execute(query) if not result.rowcount: raise dci_exc.DCIConflict('User', user_id) return flask.Response(None, 204, headers={'ETag': values['etag']}, content_type='application/json')
def put_user(user, user_id): values = flask.request.json check_json_is_valid(update_user_schema, values) if_match_etag = utils.check_and_get_etag(flask.request.headers) # to update a user the caller must be a super admin if user.is_not_super_admin(): raise dci_exc.Unauthorized() values['etag'] = utils.gen_etag() if 'password' in values: values['password'] = auth.hash_password(values.get('password')) where_clause = sql.and_(_TABLE.c.etag == if_match_etag, _TABLE.c.id == user_id) query = _TABLE.update().returning(*_TABLE.columns).\ where(where_clause).values(**values) result = flask.g.db_conn.execute(query) if not result.rowcount: raise dci_exc.DCIConflict('User', user_id) _result = dict(result.fetchone()) del _result['password'] return flask.Response(json.dumps({'user': _result}), 200, headers={'ETag': values['etag']}, content_type='application/json')
def put_user(user, user_id): # get If-Match header if_match_etag = utils.check_and_get_etag(flask.request.headers) values = schemas.user.put(flask.request.json) # to update a user the caller must be either the admin of its team # or a super admin puser = dict(_verify_existence_and_get_user(user_id)) if puser['id'] != str(user_id) and not user.is_in_team(puser['team_id']): raise auth.UNAUTHORIZED # if the caller wants to update the team_id then it must be done by a super # admin if 'team_id' in values and not user.is_super_admin() and \ not user.is_team_product_owner(values['team_id']): raise auth.UNAUTHORIZED values['etag'] = utils.gen_etag() if 'password' in values: values['password'] = auth.hash_password(values.get('password')) where_clause = sql.and_(_TABLE.c.etag == if_match_etag, _TABLE.c.id == user_id) query = _TABLE.update().where(where_clause).values(**values) result = flask.g.db_conn.execute(query) if not result.rowcount: raise dci_exc.DCIConflict('User', user_id) return flask.Response(None, 204, headers={'ETag': values['etag']}, content_type='application/json')
def put_current_user(user): if_match_etag = utils.check_and_get_etag(flask.request.headers) values = schemas.current_user.put(flask.request.json) current_password = values['current_password'] encrypted_password = user['password'] if not auth.check_passwords_equal(current_password, encrypted_password): raise dci_exc.DCIException('current_password invalid') new_password = values.get('new_password') if new_password: encrypted_password = auth.hash_password(new_password) etag = utils.gen_etag() query = _TABLE.update().where( sql.and_(_TABLE.c.etag == if_match_etag, _TABLE.c.id == user['id'])).values({ 'etag': etag, 'fullname': values.get('fullname') or user['fullname'], 'email': values.get('email') or user['email'], 'timezone': values.get('timezone') or user['timezone'], 'password': encrypted_password, }) flask.g.db_conn.execute(query) return flask.Response(None, 204, headers={'ETag': etag}, content_type='application/json')
def provision(db_conn): def db_insert(model_item, **kwargs): query = model_item.insert().values(**kwargs) return db_conn.execute(query).inserted_primary_key[0] user_pw_hash = auth.hash_password('user') user_admin_pw_hash = auth.hash_password('user_admin') admin_pw_hash = auth.hash_password('admin') # Create teams team_admin_id = db_insert(models.TEAMS, name='admin') team_user_id = db_insert(models.TEAMS, name='user') # Create users db_insert(models.USERS, name='user', role='user', password=user_pw_hash, team_id=team_user_id) db_insert(models.USERS, name='user_admin', role='admin', password=user_admin_pw_hash, team_id=team_user_id) db_insert(models.USERS, name='admin', role='admin', password=admin_pw_hash, team_id=team_admin_id)
def provision(db_conn): def db_insert(model_item, **kwargs): query = model_item.insert().values(**kwargs) return db_conn.execute(query).inserted_primary_key[0] team_admin_id = db_insert(models.TEAMS, name='admin') super_admin_role = { 'name': 'Super Admin', 'label': 'SUPER_ADMIN', 'description': 'Admin of the platform', } super_admin_role_id = db_insert(models.ROLES, **super_admin_role) db_insert(models.USERS, name='admin', role_id=super_admin_role_id, password=auth.hash_password('admin'), team_id=team_admin_id)
def put_identity(user): if_match_etag = utils.check_and_get_etag(flask.request.headers) values = flask.request.json check_json_is_valid(update_current_user_schema, values) if user.is_not_read_only_user(): current_password = values['current_password'] encrypted_password = user.password if not auth.check_passwords_equal(current_password, encrypted_password): raise dci_exc.DCIException('current_password invalid') new_values = {} new_password = values.get('new_password') if new_password: encrypted_password = auth.hash_password(new_password) new_values['password'] = encrypted_password etag = utils.gen_etag() new_values.update({ 'etag': etag, 'fullname': values.get('fullname') or user.fullname, 'email': values.get('email') or user.email, 'timezone': values.get('timezone') or user.timezone }) query = _TABLE.update().returning(*_TABLE.columns).where( sql.and_(_TABLE.c.etag == if_match_etag, _TABLE.c.id == user.id)).values(new_values) result = flask.g.db_conn.execute(query) if not result.rowcount: raise dci_exc.DCIConflict('User', user.id) _result = dict(result.fetchone()) del _result['password'] return flask.Response(json.dumps({'user': _result}), 200, headers={'ETag': etag}, content_type='application/json')
def provision(db_conn): def db_insert(model_item, return_pk=True, **kwargs): query = model_item.insert().values(**kwargs) if return_pk: return db_conn.execute(query).inserted_primary_key[0] else: db_conn.execute(query) # Create teams team_admin_id = db_insert(models.TEAMS, name='admin') team_user_id = db_insert(models.TEAMS, name='user') db_insert(models.TEAMS, name='product') db_insert(models.TEAMS, name='Red Hat') team_epm_id = db_insert(models.TEAMS, name='EPM') # Create users user_pw_hash = auth.hash_password('user') u_id = db_insert(models.USERS, name='user', sso_username='******', password=user_pw_hash, fullname='User', email='*****@*****.**', team_id=team_user_id) db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=team_user_id) user_no_team_pw_hash = auth.hash_password('user_no_team') u_id = db_insert(models.USERS, name='user_no_team', sso_username='******', password=user_no_team_pw_hash, fullname='User No Team', email='*****@*****.**', team_id=None) db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=None) epm_pw_hash = auth.hash_password('epm') u_id = db_insert(models.USERS, name='epm', sso_username='******', password=epm_pw_hash, fullname='Partner Engineer', email='*****@*****.**', team_id=team_epm_id) db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=team_epm_id) admin_pw_hash = auth.hash_password('admin') u_id = db_insert(models.USERS, name='admin', sso_username='******', password=admin_pw_hash, fullname='Admin', email='*****@*****.**', team_id=team_admin_id) db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=team_admin_id) # Create a product db_insert(models.PRODUCTS, name='Awesome product', label='AWSM', description='My Awesome product') # Create a product db_insert(models.PRODUCTS, name='Best product', label='BEST', description='My best product')
def init_db(db_conn, minimal, file): """Initialize the database with fake datas Create an admin team and 2 other teams HP and DELL Create 3 topics, 1 common and 2 scoped, 1 for each team """ db_ins = functools.partial(db_insert, db_conn) time = time_helper() # Create a super admin team_admin = db_ins(models.TEAMS, name='admin') # Create the three mandatory roles super_admin_role = { 'name': 'Super Admin', 'label': 'SUPER_ADMIN', 'description': 'Admin of the platform', } admin_role = { 'name': 'Admin', 'label': 'ADMIN', 'description': 'Admin of a team', } user_role = { 'name': 'User', 'label': 'USER', 'description': 'Regular User', } admin_role_id = db_ins(models.ROLES, **admin_role) user_role_id = db_ins(models.ROLES, **user_role) super_admin_role_id = db_ins(models.ROLES, **super_admin_role) db_ins(models.USERS, name='admin', role_id=super_admin_role_id, team_id=team_admin, password=auth.hash_password('admin')) if minimal: return # Create two other teams team_hp = db_ins(models.TEAMS, name='hp') team_dell = db_ins(models.TEAMS, name='dell') # Creates according users, 1 admin 1 user for other teams db_ins(models.USERS, name='user_hp', role_id=user_role_id, team_id=team_hp, password=auth.hash_password('password')) db_ins(models.USERS, name='admin_hp', role_id=admin_role_id, team_id=team_hp, password=auth.hash_password('password')) db_ins(models.USERS, name='user_dell', role_id=user_role_id, team_id=team_dell, password=auth.hash_password('password')) db_ins(models.USERS, name='admin_dell', role_id=admin_role_id, team_id=team_dell, password=auth.hash_password('password')) # Create 3 topics, 1 common and 2 scoped topic_common = db_ins(models.TOPICS, name='topic_common') topic_hp = db_ins(models.TOPICS, name='topic_HP') topic_dell = db_ins(models.TOPICS, name='topic_DELL') # Attach teams to topics db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_common, team_id=team_admin) db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_hp, team_id=team_admin) db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_dell, team_id=team_admin) db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_common, team_id=team_hp) db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_hp, team_id=team_hp) db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_common, team_id=team_dell) db_ins(models.JOINS_TOPICS_TEAMS, topic_id=topic_dell, team_id=team_dell) # Create 2 remotecis per team remoteci_hp_1 = { 'name': 'HP_1', 'team_id': team_hp, 'data': { 'storage': 'netapp', 'network': 'HP', 'hardware': 'Intel', 'virtualization': 'KVM' } } remoteci_hp_1 = db_ins(models.REMOTECIS, **remoteci_hp_1) remoteci_hp_2 = { 'name': 'HP_2', 'team_id': team_hp, 'data': { 'storage': 'ceph', 'network': 'Cisco', 'hardware': 'HP', 'virtualization': 'VMWare' } } remoteci_hp_2 = db_ins(models.REMOTECIS, **remoteci_hp_2) remoteci_dell_1 = { 'name': 'Dell_1', 'team_id': team_dell, 'data': { 'storage': 'swift', 'network': 'Juniper', 'hardware': 'Dell', 'virtualization': 'Xen' } } remoteci_dell_1 = db_ins(models.REMOTECIS, **remoteci_dell_1) remoteci_dell_2 = { 'name': 'Dell_2', 'team_id': team_dell, 'data': { 'storage': 'AWS', 'network': 'Brocade', 'hardware': 'Huawei', 'virtualization': 'HyperV' } } remoteci_dell_2 = db_ins(models.REMOTECIS, **remoteci_dell_2) # Create 2 components per topic component_common_1 = db_ins(models.COMPONENTS, topic_id=topic_common, type='git', name='Khaleesi', created_at=time[3][15]) component_common_2 = db_ins(models.COMPONENTS, topic_id=topic_common, type='image', name='RDO Manager', created_at=time[2][20]) component_hp_1 = db_ins(models.COMPONENTS, topic_id=topic_hp, type='package', name='OSP director', created_at=time[3][5]) component_hp_2 = db_ins(models.COMPONENTS, topic_id=topic_hp, type='gerrit_review', name='DCI-control-server', created_at=time[2][2]) component_dell_1 = db_ins(models.COMPONENTS, topic_id=topic_dell, type='git', name='Khaleesi', created_at=time[2][21]) component_dell_2 = db_ins(models.COMPONENTS, topic_id=topic_dell, type='package', name='OSP director', created_at=time[3][12]) # Create 2 jobdefinitions per topic jobdef_common_1 = db_ins(models.JOBDEFINITIONS, topic_id=topic_common, name='Common tox v0.8', component_types=['git']) jobdef_common_2 = db_ins(models.JOBDEFINITIONS, topic_id=topic_common, name='Common tox v2.1.1', component_types=['git']) jobdef_hp_1 = db_ins(models.JOBDEFINITIONS, topic_id=topic_hp, name='HP tempest v0.4.2', component_types=['OSP director']) jobdef_hp_2 = db_ins(models.JOBDEFINITIONS, topic_id=topic_hp, name='HP tempest v1.1', component_types=['package', 'gerrit_review']) jobdef_dell_1 = db_ins(models.JOBDEFINITIONS, topic_id=topic_dell, name='Dell khaleesi-tempest v0.8', component_types=['git']) jobdef_dell_2 = db_ins(models.JOBDEFINITIONS, topic_id=topic_dell, name='Dell khaleesi-tempest v1.2.15', component_types=['git', 'package']) # Creates 3 tests type test_common = db_ins(models.TESTS, name='tox', team_id=team_admin) test_hp = db_ins(models.TESTS, name='tempest', team_id=team_hp) test_dell = db_ins(models.TESTS, name='khaleesi-tempest', team_id=team_dell) db_ins(models.JOIN_JOBDEFINITIONS_TESTS, jobdefinition_id=jobdef_common_1, test_id=test_common) db_ins(models.JOIN_JOBDEFINITIONS_TESTS, jobdefinition_id=jobdef_common_2, test_id=test_common) db_ins(models.JOIN_JOBDEFINITIONS_TESTS, jobdefinition_id=jobdef_hp_1, test_id=test_hp) db_ins(models.JOIN_JOBDEFINITIONS_TESTS, jobdefinition_id=jobdef_hp_2, test_id=test_hp) db_ins(models.JOIN_JOBDEFINITIONS_TESTS, jobdefinition_id=jobdef_dell_1, test_id=test_dell) db_ins(models.JOIN_JOBDEFINITIONS_TESTS, jobdefinition_id=jobdef_dell_2, test_id=test_dell) # Creates 4 jobs for each jobdefinition (4*6=24 in total for pagination) job_id = db_ins(models.JOBS, status='new', jobdefinition_id=jobdef_common_1, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[0][1], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_1) job_id = db_ins(models.JOBS, status='new', jobdefinition_id=jobdef_common_1, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[0][2], updated_at=time[0][2], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_1) job_id = db_ins(models.JOBS, status='pre-run', jobdefinition_id=jobdef_common_1, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[0][2], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_1) job_id = db_ins(models.JOBS, status='pre-run', jobdefinition_id=jobdef_common_2, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[0][3], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_2) job_id = db_ins(models.JOBS, status='running', jobdefinition_id=jobdef_common_2, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[0][10], updated_at=time[0][3], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_2) job_id = db_ins(models.JOBS, status='running', jobdefinition_id=jobdef_common_2, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[0][14], updated_at=time[0][7], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_2) job_id = db_ins(models.JOBS, status='post-run', jobdefinition_id=jobdef_hp_1, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[1][0], updated_at=time[0][10], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_1) job_id = db_ins(models.JOBS, status='post-run', jobdefinition_id=jobdef_hp_1, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[0][20], updated_at=time[0][2], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_1) job_id = db_ins(models.JOBS, status='failure', jobdefinition_id=jobdef_hp_1, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[2][10], updated_at=time[1][3], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_1) job_id = db_ins(models.JOBS, status='failure', jobdefinition_id=jobdef_hp_2, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[1][1], updated_at=time[0][0], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_2) job_id = db_ins(models.JOBS, status='success', jobdefinition_id=jobdef_hp_2, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[3][12], updated_at=time[2][20], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_2) job_id = db_ins(models.JOBS, status='success', jobdefinition_id=jobdef_hp_2, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[3][20], updated_at=time[0][6], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_2) job_id = db_ins(models.JOBS, status='killed', jobdefinition_id=jobdef_hp_2, remoteci_id=remoteci_hp_1, team_id=team_hp, created_at=time[1][8], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_2) job_id = db_ins(models.JOBS, status='killed', jobdefinition_id=jobdef_hp_2, remoteci_id=remoteci_hp_2, team_id=team_hp, created_at=time[2][12], updated_at=time[1][6], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_hp_2) job_id = db_ins(models.JOBS, status='new', jobdefinition_id=jobdef_common_1, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[0][1], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_1) job_id = db_ins(models.JOBS, status='new', jobdefinition_id=jobdef_common_1, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[0][2], updated_at=time[0][2], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_1) job_id = db_ins(models.JOBS, status='pre-run', jobdefinition_id=jobdef_common_1, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[0][2], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_1) job_id = db_ins(models.JOBS, status='pre-run', jobdefinition_id=jobdef_common_2, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[0][3], updated_at=time[0][1], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_2) job_id = db_ins(models.JOBS, status='running', jobdefinition_id=jobdef_common_2, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[0][10], updated_at=time[0][3], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_2) job_id = db_ins(models.JOBS, status='running', jobdefinition_id=jobdef_common_2, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[0][14], updated_at=time[0][7], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_common_2) job_id = db_ins(models.JOBS, status='post-run', jobdefinition_id=jobdef_dell_1, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[1][0], updated_at=time[0][10], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_dell_1) job_id = db_ins(models.JOBS, status='post-run', jobdefinition_id=jobdef_dell_1, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[0][20], updated_at=time[0][2], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_dell_1) job_dell_9 = db_ins(models.JOBS, status='failure', jobdefinition_id=jobdef_dell_1, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[2][10], updated_at=time[1][3], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_dell_9, component_id=component_dell_1) job_dell_10 = db_ins(models.JOBS, status='failure', jobdefinition_id=jobdef_dell_2, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[1][1], updated_at=time[0][0], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_dell_10, component_id=component_dell_2) job_dell_11 = db_ins(models.JOBS, status='success', jobdefinition_id=jobdef_dell_2, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[3][12], updated_at=time[2][20], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_dell_11, component_id=component_dell_2) job_dell_12 = db_ins(models.JOBS, status='success', jobdefinition_id=jobdef_dell_2, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[3][20], updated_at=time[0][0], configuration=STACK_DETAILS, user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_dell_12, component_id=component_dell_2) job_id = db_ins(models.JOBS, status='killed', jobdefinition_id=jobdef_dell_2, remoteci_id=remoteci_dell_1, team_id=team_dell, created_at=time[1][4], updated_at=time[0][3], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_dell_2) job_id = db_ins(models.JOBS, status='killed', jobdefinition_id=jobdef_dell_2, remoteci_id=remoteci_dell_2, team_id=team_dell, created_at=time[2][8], updated_at=time[1][2], user_agent='python-dciclient_0.1.0') db_ins(models.JOIN_JOBS_COMPONENTS, job_id=job_id, component_id=component_dell_2) # Creates jobstates attached to jobs, just create a subset of them to # avoid explosion of complexity # DELL Job 9 db_ins(models.JOBSTATES, status='new', team_id=team_dell, created_at=time[2][10], job_id=job_dell_9) db_ins(models.JOBSTATES, status='pre-run', team_id=team_dell, created_at=time[2][1], job_id=job_dell_9) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[2][0], job_id=job_dell_9) db_ins(models.JOBSTATES, status='post-run', team_id=team_dell, created_at=time[1][5], job_id=job_dell_9) db_ins(models.JOBSTATES, status='failure', team_id=team_dell, created_at=time[1][3], job_id=job_dell_9) # DELL Job 10 db_ins(models.JOBSTATES, status='new', team_id=team_dell, created_at=time[1][1], job_id=job_dell_10) db_ins(models.JOBSTATES, status='pre-run', team_id=team_dell, created_at=time[1][0], job_id=job_dell_10) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[0][23], job_id=job_dell_10) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[0][15], job_id=job_dell_10) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[0][11], job_id=job_dell_10) db_ins(models.JOBSTATES, status='post-run', team_id=team_dell, created_at=time[0][2], job_id=job_dell_10) db_ins(models.JOBSTATES, status='post-run', team_id=team_dell, created_at=time[0][1], job_id=job_dell_10) db_ins(models.JOBSTATES, status='failure', team_id=team_dell, created_at=time[0][0], job_id=job_dell_10) # Dell Job 11 db_ins(models.JOBSTATES, status='new', team_id=team_dell, created_at=time[3][12], job_id=job_dell_11) db_ins(models.JOBSTATES, status='pre-run', team_id=team_dell, created_at=time[3][11], job_id=job_dell_11) db_ins(models.JOBSTATES, status='pre-run', team_id=team_dell, created_at=time[3][10], job_id=job_dell_11) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[3][1], job_id=job_dell_11) db_ins(models.JOBSTATES, status='post-run', team_id=team_dell, created_at=time[2][22], job_id=job_dell_11) db_ins(models.JOBSTATES, status='success', team_id=team_dell, created_at=time[2][20], job_id=job_dell_11) # DELL Job 12 db_ins(models.JOBSTATES, status='new', team_id=team_dell, created_at=time[3][20], job_id=job_dell_12) db_ins(models.JOBSTATES, status='pre-run', team_id=team_dell, created_at=time[3][15], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[3][14], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[3][2], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[2][18], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[1][5], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[1][0], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[0][22], job_id=job_dell_12) db_ins(models.JOBSTATES, status='running', team_id=team_dell, created_at=time[0][13], job_id=job_dell_12) db_ins(models.JOBSTATES, status='post-run', team_id=team_dell, created_at=time[0][12], job_id=job_dell_12) db_ins(models.JOBSTATES, status='post-run', team_id=team_dell, created_at=time[0][10], job_id=job_dell_12) job_dell_12_12 = db_ins(models.JOBSTATES, status='success', team_id=team_dell, created_at=time[0][0], job_id=job_dell_12) # create files only for the last job i.e: dell_12 f_id = db_ins(models.FILES, name='', mime='application/junit', created_at=time[0][0], team_id=team_dell, job_id=job_dell_12) swift = dci_config.get_store('files') if file: file_path = swift.build_file_path(team_dell, job_dell_12, f_id) swift.upload(file_path, JUNIT_TEMPEST) f_id2 = db_ins(models.FILES, name='Rally test suite', mime='application/junit', created_at=time[0][0], team_id=team_dell, job_id=job_dell_12) if file: file_path = swift.build_file_path(team_dell, job_dell_12, f_id2) swift.upload(file_path, JUNIT_RALLY) f_id = db_ins(models.FILES, name='foo.txt', mime='text/play', created_at=time[0][0], team_id=team_dell, jobstate_id=job_dell_12_12) if file: file_path = swift.build_file_path(team_dell, job_dell_12, f_id) swift.upload(file_path, 'some content') f_id = db_ins(models.FILES, name='bar.txt', mime='text/play', created_at=time[0][0], team_id=team_dell, jobstate_id=job_dell_12_12) if file: file_path = swift.build_file_path(team_dell, job_dell_12, f_id) swift.upload(file_path, 'some other content')
def provision(db_conn): def db_insert(model_item, return_pk=True, **kwargs): query = model_item.insert().values(**kwargs) if return_pk: return db_conn.execute(query).inserted_primary_key[0] else: db_conn.execute(query) # Create teams team_admin_id = db_insert(models.TEAMS, name="admin") db_insert(models.TEAMS, name="Red Hat") db_insert(models.TEAMS, name="EPM") team_user_id = db_insert(models.TEAMS, name="user") # Create users user_pw_hash = auth.hash_password("user") u_id = db_insert(models.USERS, name="user", sso_username="******", password=user_pw_hash, fullname="User", email="*****@*****.**") db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=team_user_id) user_no_team_pw_hash = auth.hash_password("user_no_team") u_id = db_insert(models.USERS, name="user_no_team", sso_username="******", password=user_no_team_pw_hash, fullname="User No Team", email="*****@*****.**") db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=None) product_owner_pw_hash = auth.hash_password("product_owner") u_id = db_insert( models.USERS, name="product_owner", sso_username="******", password=product_owner_pw_hash, fullname="Product Owner", email="*****@*****.**", ) db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id) admin_pw_hash = auth.hash_password("admin") u_id = db_insert( models.USERS, name="admin", sso_username="******", password=admin_pw_hash, fullname="Admin", email="*****@*****.**", ) db_insert(models.JOIN_USERS_TEAMS, return_pk=False, user_id=u_id, team_id=team_admin_id) # Create a product p_id = db_insert( models.PRODUCTS, return_pk=True, name="dci_product", label="dci_product", description="My Awesome product", ) # associate the team user to the product db_insert(models.JOIN_PRODUCTS_TEAMS, product_id=p_id, team_id=team_user_id)
def provision(db_conn): def db_insert(model_item, **kwargs): query = model_item.insert().values(**kwargs) return db_conn.execute(query).inserted_primary_key[0] user_pw_hash = auth.hash_password('user') user_admin_pw_hash = auth.hash_password('user_admin') product_owner_pw_hash = auth.hash_password('product_owner') admin_pw_hash = auth.hash_password('admin') # Create teams team_admin_id = db_insert(models.TEAMS, name='admin') team_product_id = db_insert(models.TEAMS, name='product') team_user_id = db_insert(models.TEAMS, name='user', parent_id=team_product_id) # Create the three mandatory roles super_admin_role = { 'name': 'Super Admin', 'label': 'SUPER_ADMIN', 'description': 'Admin of the platform', } product_owner_role = { 'name': 'Product Owner', 'label': 'PRODUCT_OWNER', 'description': 'Product Owner', } admin_role = { 'name': 'Admin', 'label': 'ADMIN', 'description': 'Admin of a team', } user_role = { 'name': 'User', 'label': 'USER', 'description': 'Regular User', } remoteci_role = { 'name': 'RemoteCI', 'label': 'REMOTECI', 'description': 'A RemoteCI', } feeder_role = { 'name': 'Feeder', 'label': 'FEEDER', 'description': 'A Feeder', } admin_role_id = db_insert(models.ROLES, **admin_role) user_role_id = db_insert(models.ROLES, **user_role) super_admin_role_id = db_insert(models.ROLES, **super_admin_role) product_owner_role_id = db_insert(models.ROLES, **product_owner_role) db_insert(models.ROLES, **remoteci_role) db_insert(models.ROLES, **feeder_role) # Create users db_insert(models.USERS, name='user', sso_username='******', role_id=user_role_id, password=user_pw_hash, fullname='User', email='*****@*****.**', team_id=team_user_id) db_insert(models.USERS, name='user_admin', sso_username='******', role_id=admin_role_id, password=user_admin_pw_hash, fullname='User Admin', email='*****@*****.**', team_id=team_user_id) db_insert(models.USERS, name='product_owner', sso_username='******', role_id=product_owner_role_id, password=product_owner_pw_hash, fullname='Product Owner', email='*****@*****.**', team_id=team_product_id) db_insert(models.USERS, name='admin', sso_username='******', role_id=super_admin_role_id, password=admin_pw_hash, fullname='Admin', email='*****@*****.**', team_id=team_admin_id) # Create a product db_insert(models.PRODUCTS, name='Awesome product', label='AWSM', description='My Awesome product', team_id=team_product_id)
def upgrade(): op.create_table( 'components', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('name', sa.String(255), unique=True, nullable=False), sa.Column('type', sa.String(255), nullable=False), sa.Column('canonical_project_name', sa.String), sa.Column('data', sa_utils.JSONType), sa.Column('sha', sa.Text), sa.Column('title', sa.Text), sa.Column('message', sa.Text), sa.Column('url', sa.Text), sa.Column('git', sa.Text), sa.Column('ref', sa.Text)) op.create_table( 'tests', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('name', sa.String(255), nullable=False, unique=True), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('data', sa_utils.JSONType)) op.create_table( 'jobdefinitions', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('name', sa.String(255)), sa.Column('priority', sa.Integer, default=0), sa.Column('test_id', sa.String(36), sa.ForeignKey('tests.id', ondelete="CASCADE"), nullable=False)) op.create_table( 'jobdefinition_components', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('component_id', sa.String(36), sa.ForeignKey('components.id', ondelete="CASCADE"), nullable=False), sa.Column('jobdefinition_id', sa.String(36), sa.ForeignKey('jobdefinitions.id', ondelete="CASCADE"), nullable=False), sa.UniqueConstraint('component_id', 'jobdefinition_id')) op.create_table( 'teams', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('name', sa.String(255), unique=True, nullable=False)) op.create_table( 'remotecis', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('name', sa.String(255), unique=True), sa.Column('data', sa_utils.JSONType), sa.Column('active', sa.BOOLEAN, default=True), sa.Column('team_id', sa.String(36), sa.ForeignKey('teams.id', ondelete="CASCADE"), nullable=False)) op.create_table( 'jobs', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('recheck', sa.Boolean, default=False), # new, pre-run, running, post-run, success, failure sa.Column('status', sa.String(255), default='new'), sa.Column('jobdefinition_id', sa.String(36), sa.ForeignKey('jobdefinitions.id', ondelete="CASCADE"), nullable=False), sa.Column('remoteci_id', sa.String(36), sa.ForeignKey('remotecis.id', ondelete="CASCADE"), nullable=False), sa.Column('team_id', sa.String(36), sa.ForeignKey('teams.id', ondelete="CASCADE"), nullable=False)) op.create_table( 'jobstates', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), # new, pre-run, running, post-run, success, failure sa.Column('status', sa.String(255), nullable=False), sa.Column('comment', sa.Text), sa.Column('job_id', sa.String(36), sa.ForeignKey('jobs.id', ondelete="CASCADE"), nullable=False), sa.Column('team_id', sa.String(36), sa.ForeignKey('teams.id', ondelete="CASCADE"), nullable=False)) op.create_table( 'files', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('name', sa.String(255), nullable=False), sa.Column('content', sa.Text, nullable=False), sa.Column('mime', sa.String), sa.Column('md5', sa.String(32)), sa.Column('jobstate_id', sa.String(36), sa.ForeignKey('jobstates.id', ondelete="CASCADE"), nullable=False), sa.Column('team_id', sa.String(36), sa.ForeignKey('teams.id', ondelete="CASCADE"), nullable=False)) op.create_table( 'users', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('name', sa.String(255), unique=True, nullable=False), sa.Column('password', sa.Text, nullable=False), sa.Column('role', sa.String(255), default='user', nullable=False), sa.Column('team_id', sa.String(36), sa.ForeignKey('teams.id', ondelete="CASCADE"), nullable=False)) op.create_table( 'user_remotecis', sa.Column('id', sa.String(36), primary_key=True, default=utils.gen_uuid), sa.Column('created_at', sa.DateTime(), default=datetime.datetime.utcnow, nullable=False), sa.Column('updated_at', sa.DateTime(), onupdate=datetime.datetime.utcnow, default=datetime.datetime.utcnow, nullable=False), sa.Column('etag', sa.String(40), nullable=False, default=utils.gen_etag, onupdate=utils.gen_etag), sa.Column('user_id', sa.String(36), sa.ForeignKey('users.id', ondelete="CASCADE"), nullable=False), sa.Column('remoteci_id', sa.String(36), sa.ForeignKey('remotecis.id', ondelete="CASCADE"), nullable=False), sa.UniqueConstraint('user_id', 'remoteci_id')) db_conn = op.get_bind() team_id = utils.gen_uuid() team_values = { 'id': team_id, 'created_at': datetime.datetime.utcnow().isoformat(), 'updated_at': datetime.datetime.utcnow().isoformat(), 'etag': utils.gen_etag(), 'name': 'admin' } db_conn.execute(TEAMS.insert().values(**team_values)) user_id = utils.gen_uuid() user_values = { 'id': user_id, 'created_at': datetime.datetime.utcnow().isoformat(), 'updated_at': datetime.datetime.utcnow().isoformat(), 'etag': utils.gen_etag(), 'name': 'admin', 'role': 'admin', 'team_id': team_id, 'password': auth.hash_password('admin'), } db_conn.execute(USERS.insert().values(**user_values))