#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2018             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


def parse_docker_network(info):
    # pylint: disable=undefined-variable
    version = docker_get_version(info)

    if version is None:
        networks = parse_legacy_docker_network_inspect(info)
        return DeprecatedDict((n["Id"], n) for n in networks)

    networks = (docker_json_get_obj(line) for line in info[1:])
    return {n["Id"]: n for n in networks if n is not None}


def inv_docker_node_network_networks(info, inventory_tree, status_data_tree):
    parsed = parse_docker_network(info)

    for network_id, network in parsed.iteritems():

        sorted_container_items = sorted(network["Containers"].items())

        containers = [
            {
                "name": container["Name"],
                "id": docker_get_short_id(container_id),  # pylint: disable=undefined-variable
                "ipv4_address": container["IPv4Address"],
                "ipv6_address": container["IPv6Address"],
                "mac_address": container["MacAddress"],
            } for container_id, container in sorted_container_items
        ]

        network_name = network["Name"]
        path = "software.applications.docker.networks.%s." % network_name
        inventory_network = inventory_tree.get_dict(path)
        status_network = status_data_tree.get_dict(path)

        status_network.update({
            "name": network_name,
            "containers": containers,
        })

        inventory_network.update({
            "name": network_name,
            "network_id": docker_get_short_id(network_id),  # pylint: disable=undefined-variable
            "scope": network["Scope"],
        })

        try:
            inventory_network["host_ifname"] = network["Options"]["com.docker.network.bridge.name"]
        except KeyError:
            pass

        labels = docker_format_labels(network)  # pylint: disable=undefined-variable
        if labels:
            inventory_network["labels"] = labels


inv_info["docker_node_network.networks"] = {  # pylint: disable=undefined-variable
    "includes": ['docker.include', 'legacy_docker.include'],
    "inv_function": inv_docker_node_network_networks,
}
