def add(context, name, bucket, zone, testing_account, is_publishing_account, credentials): """ Add a GCE account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(credentials) as credentials_file: creds = json.load(credentials_file) data = { 'account_name': name, 'bucket': bucket, 'credentials': creds, 'region': zone } if testing_account: data['testing_account'] = testing_account if is_publishing_account: data['is_publishing_account'] = True result = handle_request_with_token(config_data, '/v1/accounts/gce/', data) echo_dict(result, config_data['no_color'])
def add(context, name, bucket, region, availability_domain, compartment_id, oci_user_id, tenancy, signing_key_file): """ Add a OCI account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(signing_key_file) as key_file: signing_key = key_file.read() data = { 'account_name': name, 'bucket': bucket, 'region': region, 'availability_domain': availability_domain, 'compartment_id': compartment_id, 'oci_user_id': oci_user_id, 'tenancy': tenancy, 'signing_key': signing_key } result = handle_request_with_token(config_data, '/v1/accounts/oci/', data) echo_dict(result, config_data['no_color'])
def wait(context, wait_time, job_id): """ Wait for job to arrive at finished or failed status. By default waits 5 minutes between status queries. """ config_data = get_config(context.obj) state = 'undefined' while True: with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/jobs/{0}'.format(job_id), action='get' ) state = result['state'] if state not in ('running', 'undefined'): break time.sleep(wait_time) click.echo(state)
def update(context, name, bucket, zone, testing_account, credentials): """ Update a GCE account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if credentials: with open(credentials) as credentials_file: creds = json.load(credentials_file) data['credentials'] = creds if bucket: data['bucket'] = bucket if zone: data['region'] = zone if testing_account: data['testing_account'] = testing_account if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/gce/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def add(context, name, bucket, region, security_group_id, vswitch_id, access_key, access_secret): """ Add a Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = { 'account_name': name, 'bucket': bucket, 'credentials': { 'access_key': access_key, 'access_secret': access_secret }, 'region': region } if security_group_id: data['security_group_id'] = security_group_id if vswitch_id: data['vswitch_id'] = vswitch_id result = handle_request_with_token(config_data, '/v1/accounts/aliyun/', data) echo_dict(result, config_data['no_color'])
def get(context, jti): """ Return information for token matching jti. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/auth/token/{jti}'.format(jti=jti), action='get') echo_dict(result, config_data['no_color'])
def token_list(context): """ Return a list of JWT tokens for user. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token(config_data, '/v1/auth/token', action='get') echo_dict(result, config_data['no_color'])
def list_aliyun_accounts(context): """ Get a list of all Aliyun accounts. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token(config_data, '/v1/accounts/aliyun/', action='get') echo_dict(result, config_data['no_color'])
def get_user(context): """ Get mash user info. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token(config_data, '/v1/user/', action='get') echo_dict(result, config_data['no_color'])
def update( context, additional_regions, group, name, region, subnet, access_key_id, secret_access_key ): """ Update an EC2 account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if all([access_key_id, secret_access_key]): data['credentials'] = { 'access_key_id': access_key_id, 'secret_access_key': secret_access_key } elif any([access_key_id, secret_access_key]): echo_style( 'Both secret_access_key and access_key_id are required ' 'when updating credentials.', config_data['no_color'], fg='red' ) sys.exit(1) if additional_regions: regions = additional_regions_repl() if regions: data['additional_regions'] = regions if group: data['group'] = group if region: data['region'] = region if subnet: data['subnet'] = subnet if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/ec2/{name}'.format(name=name), data ) echo_dict(result, config_data['no_color'])
def delete(context, name): """ Delete an Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), action='delete') echo_style(result['msg'], config_data['no_color'])
def get(context, name): """ Get info for a Aliyun account. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), action='get') echo_dict(result, config_data['no_color'])
def delete(context, jti): """ If no jti is provided delete all tokens. Otherwise delete token matching the jti. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): if jti: result = handle_request_with_token( config_data, '/v1/auth/token/{jti}'.format(jti=jti), action='delete') elif click.confirm('Are you sure you want to delete all tokens?'): result = handle_request_with_token(config_data, '/v1/auth/token', action='delete') else: echo_style('No tokens deleted', config_data['no_color'], fg='red') sys.exit(1) echo_style(result['msg'], config_data['no_color'])
def get_job_status(config_data, job_id, raise_for_status=True): result = handle_request_with_token(config_data, '/v1/jobs/{0}'.format(job_id), action='get', raise_for_status=raise_for_status) if 'state' not in result: return result status_info = {'state': result['state']} if result['state'] == 'running' and 'current_service' in result: status_info['current_service'] = result['current_service'] return status_info
def delete_user(context): """ Handle mash user deletion requests. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): if click.confirm('Are you sure you want to delete user?'): result = handle_request_with_token(config_data, '/v1/user/', action='delete') echo_style(result['msg'], config_data['no_color']) else: echo_style('Aborted', config_data['no_color'], fg='red') sys.exit(1)
def update(context, name, bucket, region, security_group_id, vswitch_id, access_key, access_secret): """ Update a Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if all([access_key, access_secret]): data['credentials'] = { 'access_key': access_key, 'access_secret': access_secret } elif any([access_key, access_secret]): echo_style( 'Both access_secret and access_key are required ' 'when updating credentials.', config_data['no_color'], fg='red') sys.exit(1) if bucket: data['bucket'] = bucket if region: data['region'] = region if security_group_id: data['security_group_id'] = security_group_id if vswitch_id: data['vswitch_id'] = vswitch_id if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def list_user_jobs(config_data, raise_for_status=True, page=None, per_page=None, api_version='v1'): job_data = {} if page: job_data['page'] = page if per_page: job_data['per_page'] = per_page return handle_request_with_token( config_data, '/{api_version}/jobs/'.format(api_version=api_version), job_data=job_data, action='get', raise_for_status=raise_for_status)
def update(context, name, bucket, region, availability_domain, compartment_id, oci_user_id, tenancy, signing_key_file): """ Update a OCI account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if signing_key_file: with open(signing_key_file) as key_file: signing_key = key_file.read() data['signing_key'] = signing_key if bucket: data['bucket'] = bucket if region: data['region'] = region if availability_domain: data['availability_domain'] = availability_domain if compartment_id: data['compartment_id'] = compartment_id if oci_user_id: data['oci_user_id'] = oci_user_id if tenancy: data['tenancy'] = tenancy if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/oci/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def add_job(config_data, job_data, cloud, api_version=None, raise_for_status=True): versions = { 'aliyun': 'v1', 'azure': 'v1', 'ec2': 'v1', 'gce': 'v1', 'oci': 'v1' } if not api_version: api_version = versions[cloud] return handle_request_with_token(config_data, '/{api_version}/jobs/{cloud}/'.format( api_version=api_version, cloud=cloud), job_data, raise_for_status=raise_for_status)
def get_job_test_results(config_data, job_id, raise_for_status=True): result = handle_request_with_token(config_data, '/v1/jobs/{0}'.format(job_id), action='get', raise_for_status=raise_for_status) if 'job_id' not in result: # An error occurred and raise_for_status is False. # job_id is required in all jobs. return result try: raw_test_results = result['data']['test_results'] except KeyError: return {'msg': 'The job has no test results.'} try: result_data = json.loads(raw_test_results) except json.decoder.JSONDecodeError: return {'msg': 'The job\'s test results are malformed.'} return result_data
def add(context, name, region, source_container, source_resource_group, source_storage_account, credentials): """ Add an Azure account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(credentials) as credentials_file: creds = json.load(credentials_file) data = { 'account_name': name, 'credentials': creds, 'region': region, 'source_container': source_container, 'source_resource_group': source_resource_group, 'source_storage_account': source_storage_account } result = handle_request_with_token(config_data, '/v1/accounts/azure/', data) echo_dict(result, config_data['no_color'])
def add( context, additional_regions, group, name, partition, region, subnet, access_key_id, secret_access_key ): """ Add an EC2 account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = { 'account_name': name, 'credentials': { 'access_key_id': access_key_id, 'secret_access_key': secret_access_key }, 'partition': partition, 'region': region } if additional_regions: data['additional_regions'] = additional_regions_repl() if group: data['group'] = group if subnet: data['subnet'] = subnet result = handle_request_with_token( config_data, '/v1/accounts/ec2/', data ) echo_dict(result, config_data['no_color'])
def update(context, name, region, source_container, source_resource_group, source_storage_account, credentials): """ Update an Azure account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if credentials: with open(credentials) as credentials_file: creds = json.load(credentials_file) data['credentials'] = creds if region: data['region'] = region if source_container: data['source_container'] = source_container if source_resource_group: data['source_resource_group'] = source_resource_group if source_storage_account: data['source_storage_account'] = source_storage_account if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/azure/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def get_job(config_data, job_id, raise_for_status=True): return handle_request_with_token(config_data, '/v1/jobs/{0}'.format(job_id), action='get', raise_for_status=raise_for_status)