Exemple #1
0
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'])
Exemple #2
0
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'])
Exemple #3
0
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'])
Exemple #4
0
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 status(context, job_id):
    """
    Get basic status for a job in the MASH server pipeline.
    """
    config_data = get_config(context.obj)

    with handle_errors(config_data['log_level'], config_data['no_color']):
        status_info = get_job_status(config_data, job_id)
        echo_dict(status_info, config_data['no_color'])
Exemple #6
0
def get_schema(context, output_style):
    """
    Get the an annotated json dictionary for a Azure job.
    """
    config_data = get_config(context.obj)

    with handle_errors(config_data['log_level'], config_data['no_color']):
        result = get_job_schema_by_cloud(config_data, output_style, 'azure')

    echo_dict(result, config_data['no_color'])
Exemple #7
0
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'])
Exemple #8
0
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'])
Exemple #9
0
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'])
Exemple #10
0
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'])
Exemple #11
0
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'])
Exemple #12
0
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'])
Exemple #13
0
def show_config(context):
    """
    Prints a dictionary from the client config file based on profile.
    """
    config_data = get_config(context.obj)
    config_file_path = os.path.join(config_data['config_dir'],
                                    config_data['profile'] + '.yaml')

    with handle_errors(config_data['log_level'], config_data['no_color']):
        with open(config_file_path, 'r') as config_file:
            config_values = yaml.safe_load(config_file)

    echo_dict(config_values, config_data['no_color'])
def get(context, job_id, show_data):
    """
    Get info for a job in the MASH server pipeline.
    """
    config_data = get_config(context.obj)

    with handle_errors(config_data['log_level'], config_data['no_color']):
        result = get_job(config_data, job_id)

        if not show_data:
            with suppress(KeyError):
                del result['data']

        echo_dict(result, config_data['no_color'])
def list_jobs(context, api_version, per_page, page):
    """
    List all jobs in the MASH server pipeline.
    """
    config_data = get_config(context.obj)

    with handle_errors(config_data['log_level'], config_data['no_color']):
        kwargs = {}
        if page:
            kwargs['page'] = page
        if per_page:
            kwargs['per_page'] = per_page
        if api_version:
            kwargs['api_version'] = api_version

        result = list_user_jobs(config_data, **kwargs)
        echo_dict(result, config_data['no_color'])
Exemple #16
0
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'])
Exemple #17
0
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'])
Exemple #18
0
def setup_config(config_dir, profile, email, host, port, log_level, color,
                 verify):
    """
    Create a configuration file for the mash command line tool

    For details see man 5 mash_client.conf
    """
    no_color = not color  # Value is stored as no_color in config file

    if verify:
        path_to_cert = click.prompt('Enter path to cert (optional)',
                                    type=str,
                                    default='',
                                    show_default=True)
        if path_to_cert:
            verify = path_to_cert

    config_values = {
        'host': host,
        'log_level': log_level,
        'no_color': no_color,
        'verify': verify
    }

    if email:
        config_values['email'] = email

    if port:
        config_values['port'] = port

    config_dir = os.path.expanduser(config_dir)
    if not os.path.isdir(config_dir):
        os.makedirs(config_dir)

    config_file_path = os.path.join(config_dir, profile + '.yaml')

    with handle_errors(log_level, no_color):
        with open(config_file_path, 'w') as config_file:
            yaml.dump(config_values, config_file, default_flow_style=False)

    echo_dict(config_values, no_color)
Exemple #19
0
def create_user(context, email):
    """
    Handle mash user creation requests.
    """
    config_data = get_config(context.obj)

    with handle_errors(config_data['log_level'], config_data['no_color']):
        pass1 = click.prompt('Enter password', type=str, hide_input=True)
        pass2 = click.prompt('Confirm password', type=str, hide_input=True)

        if pass1 != pass2:
            raise MashClientException('Passwords do not match!')

        job_data = {'email': email, 'password': pass1}
        result = handle_request(config_data,
                                '/v1/user/',
                                job_data=job_data,
                                action='post')

        echo_dict(result, config_data['no_color'])

        if result.get('id'):
            update_config(context.obj, 'email', email)
Exemple #20
0
def add(context, dry_run, document, api_version):
    """
    Send add azure job request to mash server based on provided json document.
    """
    config_data = get_config(context.obj)

    with handle_errors(config_data['log_level'], config_data['no_color']):
        with open(document) as job_file:
            job_data = json.load(job_file)

        if dry_run:
            job_data['dry_run'] = True

        kwargs = {}
        if api_version:
            kwargs['api_version'] = api_version

        result = add_job(config_data, job_data, 'azure', **kwargs)

        if 'msg' in result:
            echo_style(result['msg'], config_data['no_color'])
        else:
            echo_dict(result, config_data['no_color'])
Exemple #21
0
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'])
Exemple #22
0
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'])
Exemple #23
0
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'])