コード例 #1
0
 def _get_bandwidth_info(account, bucket_names):
     bucket_ids = [
         bucket
         for bucket in (bucket_names if isinstance(bucket_names, list) else [bucket_names])
     ]
     bandwidth_cost = AWSDetailedLineitem.get_s3_bandwidth_info_and_cost_per_name(account.get_aws_user_id(), bucket_ids)
     return bandwidth_cost
コード例 #2
0
def aws_accounts_m_stats_s3bucketsizepername(accounts):
    """---
    get:
        tags:
            - aws
        produces:
            - application/csv
        description: &desc Stats about cost and usage of bandwidth and storag on s3 buckets, organised by name
        summary: *desc
        responses:
            200:
                description: Stats about cost and usage of bandwidth and storag on s3 buckets, organised by name
            403:
                description: Not logged in
            404:
                description: AWS account not registered
    """


    def _create_bandwidth_breakdown(transfer_types_list, csv_row, bucket_bandwidth_stat):
        for elem in transfer_types_list:
            _current_transfer_type = _check_if_in_list(bucket_bandwidth_stat['transfer_stats'], elem, 'type')
            if _current_transfer_type is not None:
                csv_row[elem] = _current_transfer_type['data'] * 1024 * 1024 * 1024 # The is by default given in GB
        return csv_row

    def _create_csv_rows(bucket_list, account, bandwidth_cost, csv_row_all):
        if bucket_list is None:
            return []
        for bucket in bucket_list['buckets']:
            csv_row = {
                'account_id': account.get_aws_user_id(),
                'used_space': bucket['used_space'],
                'name': bucket['name'],
                'storage_cost': _check_if_in_list(bucket['prices'], bucket['provider'], 'provider')['cost']
            }
            bucket_bandwidth_stat = _check_if_in_list(bandwidth_cost, bucket['name'], 'bucket_name')
            if bucket_bandwidth_stat is not None:
                csv_row = _create_bandwidth_breakdown(transfer_types_list, csv_row, bucket_bandwidth_stat)
            csv_row['bandwidth_cost'] = bucket_bandwidth_stat['cost'] if bucket_bandwidth_stat is not None else 0
            csv_row['total_cost'] = csv_row['storage_cost'] + csv_row['bandwidth_cost']
            csv_row_all.append(csv_row)
        return csv_row_all

    assert len(accounts) > 0
    csv_header = ['account_id', 'name', 'used_space', 'storage_cost', 'bandwidth_cost', 'total_cost']
    csv_row_all = []
    for account in accounts:
        bucket_list = AWSStat.latest_s3_space_usage(account)
        bucket_ids = [
            bucket['name']
            for bucket in (bucket_list['buckets'] if bucket_list is not None else [])
        ]
        bandwidth_cost = AWSDetailedLineitem.get_s3_bandwidth_info_and_cost_per_name(account.get_aws_user_id(), bucket_ids)
        transfer_types_list = _build_list_used_transfer_types(bandwidth_cost)
        csv_header = _append_to_header_list(csv_header, transfer_types_list)
        csv_row_all = _create_csv_rows(bucket_list, account, bandwidth_cost, csv_row_all)

    if len(csv_row_all) > 0 and csv_row_all[0] is None:
        csv_row_all = []
    if 'csv' in request.args:
        return Response(generate_csv_clean(csv_row_all, csv_header))
    return jsonify(accounts=csv_row_all)