Beispiel #1
0
def list_violations(config, output, since, region, meta, remeta, limit, all,
                    **kwargs):
    '''List violations'''
    url = config.get('url')
    if not url:
        raise click.ClickException(
            'Missing configuration URL. Please run "stups configure".')

    kwargs['accounts'] = kwargs.get('accounts') or config.get('accounts')

    token = get_token()

    params = {'size': limit, 'sort': 'id,DESC'}
    params['from'] = parse_since(since)
    params['application-ids'] = kwargs.get('applications')
    params['application-version-ids'] = kwargs.get('application_versions')
    params.update(kwargs)

    r = request(url, '/api/violations', token, params=params)
    r.raise_for_status()
    data = r.json()['content']

    if (all):
        params['checked'] = 'true'
        r = request(url, '/api/violations', token, params=params)
        r.raise_for_status()
        data.extend(r.json()['content'])

    rows = []
    for row in data:
        if region and row['region'] != region:
            continue
        if meta and not meta_matches(row['meta_info'], meta):
            continue
        if remeta and not meta_matches_re(format_meta_info(row['meta_info']),
                                          remeta):
            continue
        row['violation_type'] = row['violation_type']['id']
        row['created_time'] = parse_time(row['created'])
        row['meta_info'] = format_meta_info(row['meta_info'])
        rows.append(row)

    # we get the newest violations first, but we want to print them in order
    rows.reverse()

    with OutputFormat(output):
        print_table(
            [
                'account_id', 'region', 'id', 'violation_type', 'instance_id',
                'application_id', 'application_version_id', 'meta_info',
                'comment', 'created_time'
            ],
            rows,
            titles={
                'created_time': 'Created',
                'application_id': 'Application',
                'application_version_id': 'Application Version'
            })
Beispiel #2
0
def list_violations(config, output, since, region, meta, remeta, limit, all, **kwargs):
    '''List violations'''
    url = config.get('url')
    if not url:
        raise click.ClickException('Missing configuration URL. Please run "stups configure".')

    kwargs['accounts'] = kwargs.get('accounts') or config.get('accounts')

    token = get_token()

    params = {'size': limit, 'sort': 'id,DESC'}
    params['from'] = parse_since(since)
    params['application-ids'] = kwargs.get('applications')
    params['application-version-ids'] = kwargs.get('application_versions')
    params.update(kwargs)

    r = request(url, '/api/violations', token, params=params)
    r.raise_for_status()
    data = r.json()['content']

    if (all):
        params['checked'] = 'true'
        r = request(url, '/api/violations', token, params=params)
        r.raise_for_status()
        data.extend(r.json()['content'])

    rows = []
    for row in data:
        if region and row['region'] != region:
            continue
        if meta and not meta_matches(row['meta_info'], meta):
            continue
        if remeta and not meta_matches_re(format_meta_info(row['meta_info']), remeta):
            continue
        row['violation_type'] = row['violation_type']['id']
        row['created_time'] = parse_time(row['created'])
        row['meta_info'] = format_meta_info(row['meta_info'])
        rows.append(row)

    # we get the newest violations first, but we want to print them in order
    rows.reverse()

    with OutputFormat(output):
        print_table(['account_id',
                     'region',
                     'id',
                     'violation_type',
                     'instance_id',
                     'application_id',
                     'application_version_id',
                     'meta_info',
                     'comment',
                     'created_time'],
                    rows, titles={'created_time': 'Created',
                                  'application_id': 'Application',
                                  'application_version_id': 'Application Version'})
Beispiel #3
0
def types(config, output):
    '''List violation types'''
    url = config.get('url')
    if not url:
        raise click.ClickException(
            'Missing configuration URL. Please run "stups configure".')

    token = get_token()

    r = request(url, '/api/violation-types', token)
    r.raise_for_status()
    data = r.json()

    rows = []
    for row in data:
        row['created_time'] = parse_time(row['created'])
        rows.append(row)

    rows.sort(key=lambda r: r['id'])

    with OutputFormat(output):
        print_table(['id', 'violation_severity', 'created_time', 'help_text'],
                    rows,
                    titles={
                        'created_time': 'Created',
                        'violation_severity': 'Sev.'
                    })
Beispiel #4
0
def resolve_violations(config, comment, since, region, meta, remeta, limit, violation_ids, **kwargs):
    '''Resolve violations'''
    url = config.get('url')
    if not url:
        raise click.ClickException('Missing configuration URL. Please run "stups configure".')

    kwargs['accounts'] = kwargs.get('accounts') or config.get('accounts')

    if all([not violation_ids, not kwargs['accounts'], not kwargs['type'], not region]):
        raise click.UsageError('At least one of --accounts, --type, --region or --violation-ids must be specified')

    token = get_token()

    params = {'size': limit, 'sort': 'id,DESC'}
    params['from'] = parse_since(since)
    params['application-ids'] = kwargs.get('applications')
    params['application-version-ids'] = kwargs.get('application_versions')
    params.update(kwargs)
    data = {}
    if violation_ids:
        data['content'] = []
        for violation_id in violation_ids.split(','):
            r = request(url, '/api/violations/{}'.format(violation_id), token, params=params)
            r.raise_for_status()
            data['content'].append(r.json())
    else:
        r = request(url, '/api/violations', token, params=params)
        r.raise_for_status()
        data = r.json()

    for row in data['content']:
        if region and row['region'] != region:
            continue
        if meta and not meta_matches(row['meta_info'], meta):
            continue
        if remeta and not meta_matches_re(format_meta_info(row['meta_info']), remeta):
            continue
        if row['comment']:
            # already resolved, skip
            continue
        with Action('Resolving violation {}/{} {} {}..'.format(row['account_id'], row['region'],
                                                               row['violation_type']['id'], row['id'])):
            r = session.post(url + '/api/violations/{}/resolution'.format(row['id']), data=comment,
                             headers={'Authorization': 'Bearer {}'.format(token)})
            r.raise_for_status()
Beispiel #5
0
def resolve_violations(config, comment, since, region, meta, remeta, limit, violation_ids, **kwargs):
    '''Resolve violations'''
    url = config.get('url')
    if not url:
        raise click.ClickException('Missing configuration URL. Please run "stups configure".')

    kwargs['accounts'] = kwargs.get('accounts') or config.get('accounts')

    if all([not violation_ids, not kwargs['accounts'], not kwargs['type'], not region]):
        raise click.UsageError('At least one of --accounts, --type, --region or --violation-ids must be specified')

    token = get_token()

    params = {'size': limit, 'sort': 'id,DESC'}
    params['from'] = parse_since(since)
    params.update(kwargs)
    data = {}
    if violation_ids:
        data['content'] = []
        for violation_id in violation_ids.split(','):
            r = request(url, '/api/violations/{}'.format(violation_id), token, params=params)
            r.raise_for_status()
            data['content'].append(r.json())
    else:
        r = request(url, '/api/violations', token, params=params)
        r.raise_for_status()
        data = r.json()

    for row in data['content']:
        if region and row['region'] != region:
            continue
        if meta and not meta_matches(row['meta_info'], meta):
            continue
        if remeta and not meta_matches_re(format_meta_info(row['meta_info']), remeta):
            continue
        if row['comment']:
            # already resolved, skip
            continue
        with Action('Resolving violation {}/{} {} {}..'.format(row['account_id'], row['region'],
                                                               row['violation_type']['id'], row['id'])):
            r = session.post(url + '/api/violations/{}/resolution'.format(row['id']), data=comment,
                             headers={'Authorization': 'Bearer {}'.format(token)})
            r.raise_for_status()
Beispiel #6
0
def types(config, output):
    '''List violation types'''
    url = config.get('url')
    if not url:
        raise click.ClickException('Missing configuration URL. Please run "stups configure".')

    token = get_token()

    r = request(url, '/api/violation-types', token)
    r.raise_for_status()
    data = r.json()

    rows = []
    for row in data:
        row['created_time'] = parse_time(row['created'])
        rows.append(row)

    rows.sort(key=lambda r: r['id'])

    with OutputFormat(output):
        print_table(['id', 'violation_severity', 'created_time', 'help_text'],
                    rows, titles={'created_time': 'Created', 'violation_severity': 'Sev.'})