#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.aws import *  # pylint: disable=wildcard-import,unused-wildcard-import


def parse_aws_elbv2_summary(info):
    application_lbs, network_lbs = [], []
    for row in parse_aws(info):
        lb_type = row.get('Type')
        if lb_type == "application":
            application_lbs.append(row)
        elif lb_type == "network":
            network_lbs.append(row)
    return application_lbs, network_lbs


def inventory_aws_elbv2_summary_application(parsed):
    application_lbs, _network_lbs = parsed
    if application_lbs:
        return [(None, {})]


def check_aws_elbv2_summary_application(item, params, parsed):
    application_lbs, _network_lbs = parsed
    return check_aws_elb_summary_generic(item, params, application_lbs)


check_info['aws_elbv2_summary'] = {
    'parse_function': parse_aws_elbv2_summary,
    'inventory_function': inventory_aws_elbv2_summary_application,
    'check_function': check_aws_elbv2_summary_application,
    'service_description': 'AWS/ApplicationELB Summary',
}


def inventory_aws_elbv2_summary_network(parsed):
    _application_lbs, network_lbs = parsed
    if network_lbs:
        return [(None, {})]


def check_aws_elbv2_summary_network(item, params, parsed):
    _application_lbs, network_lbs = parsed
    return check_aws_elb_summary_generic(item, params, network_lbs)


check_info['aws_elbv2_summary.network'] = {
    'inventory_function': inventory_aws_elbv2_summary_network,
    'check_function': check_aws_elbv2_summary_network,
    'service_description': 'AWS/NetworkELB Summary',
}
