Esempio n. 1
0
def get_aws_accounts(user):
    """---
    get:
        tags:
            - aws
        produces:
            - application/json
        description: &desc Get AWS accounts
        summary: *desc
        responses:
            200:
                description: List of AWS accounts
                schema:
                    properties:
                        accounts:
                            type: array
                            items:
                                $ref: "#/definitions/AWSAccount"
            403:
                description: Not logged in
    """
    if user:
        now = datetime.utcnow()
        date_from = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
        date_to = now.replace(day=calendar.monthrange(date_from.year, date_from.month)[1], hour=23, minute=59,
                              second=59, microsecond=999999)
        if user.admin:
            res = []
            keys = AWSKey.query.all()
            for key in keys:
                user_id = key.get_aws_user_id()
                key_infos = aws_key_schema.dump(key)[0]
                full = False if not user_id else AWSDetailedLineitem.keys_has_data(key.get_aws_user_id())
                month = False if not full else AWSDetailedLineitem.keys_has_data(key.get_aws_user_id(), date_from=date_from, date_to=date_to)
                key_infos['has_data_full'] = full
                key_infos['has_data_month'] = month
                if key.id_user != user.id:
                    if not key_infos['pretty']:
                        key_infos['pretty'] = key.user.email
                    else:
                        key_infos['pretty'] = key_infos['pretty'] + ' (' + key.user.email + ')'
                res.append(key_infos)
            return jsonify(accounts=res), 200
        keys = []
        for key in user.aws_keys:
            user_id = key.get_aws_user_id()
            key_infos = aws_key_schema.dump(key)[0]
            full = False if not user_id else AWSDetailedLineitem.keys_has_data(key.get_aws_user_id())
            month = False if not full else AWSDetailedLineitem.keys_has_data(key.get_aws_user_id(), date_from=date_from, date_to=date_to)
            key_infos['has_data_full'] = full
            key_infos['has_data_month'] = month
            keys.append(key_infos)

        return jsonify(accounts=keys), 200
    else:
        return jsonify(error="Forbidden"), 403
Esempio n. 2
0
def describe_elb(accounts):
    now = datetime.utcnow()
    date_from = now.replace(hour=0, minute=0, second=0,
                            microsecond=0) - relativedelta(days=30)
    date_to = now.replace(hour=23, minute=59, second=59, microsecond=999999)
    ares = []
    for account in accounts:
        res = []
        elb_usage = AWSDetailedLineitem.get_elb_usage_a_day(
            account.get_aws_user_id(), date_from=date_from, date_to=date_to)
        for elb in AWSELBInfo.get_elb_info(account.get_aws_user_id()):
            for usage in elb_usage:
                if usage['rid'].endswith(elb['name']) or usage['rid'].split(
                        '/')[-2] == elb['name']:
                    usage['region'] = elb['region']
                    usage['name'] = elb['name']
                    usage['instances'] = elb['instances']
                    res.append(usage)
        for d in res:
            if d not in ares:
                ares.append(d)
    if not len(ares):
        if AWSDetailedLineitem.keys_has_data(
            [account.get_aws_user_id() for account in accounts]):
            return jsonify(
                message="You do not have ELB set up in your environment")
        return jsonify(message=get_next_update_estimation_message_aws(
            accounts, AWS_KEY_PROCESSING_INTERVAL_HOURS))
    return jsonify(elbs=ares)