from copy import deepcopy
import os
import ast
import requests
from .common import json
from .common import CATTLE_API_URL
from .common import create_config_file
from .common import CATTLE_TEST_URL
from .common import ADMIN_TOKEN
from .common import get_setting_value_by_name
from .common import get_user_client
from .common import USER_TOKEN
from .common import validate_cluster
from .test_rke_cluster_provisioning import HOST_NAME
from .test_rke_cluster_provisioning import random_name
from .test_rke_cluster_provisioning import rke_config
from .test_rke_cluster_provisioning import K8S_VERSION
from .test_rke_cluster_provisioning import random_test_name
from .test_rke_cluster_provisioning import get_custom_host_registration_cmd

from lib.aws import AmazonWebServices
import multiprocessing


K8S_VERSION_URL = "/settings/k8s-versions-current"
NETWORK_PLUGINS = ["calico", "canal", "flannel", "weave"]
DNS_PROVIDERS = ["coredns", "kube-dns"]
CLUSTER_LIST = []
NODE_COUNT_KDM_CLUSTER = \
    int(os.environ.get("RANCHER_NODE_COUNT_KDM_CLUSTER", 4))
DNS_MATRIX = \
    ast.literal_eval(os.environ.get('RANCHER_DNS_PROVIDER_MATRIX', "False"))


def test_clusters_for_kdm():
    """
    This fuction is used to check the KDM changes.
    It deploys all the different types of k8s clusters - default_k8s_versions,
    across all the network provider types -
    NETWORK_PLUGINS, and all dns provider types - DNS_PROVIDERS
    It then deploys a workload on each cluster,
    checks service discovery - DNS resolution and
    checks the ingress when enabled
    Helper function - validate_custom_cluster_kdm() to create the AWS nodes
    and add to the cluster
    """
    rancher_version = get_setting_value_by_name('server-version')
    if K8S_VERSION == "":
        if str(rancher_version).startswith('v2.2'):
            k8s_v = get_setting_value_by_name('k8s-version-to-images')
            default_k8s_versions = json.loads(k8s_v).keys()
        else:
            k8s_v = get_setting_value_by_name('k8s-versions-current')
            default_k8s_versions = k8s_v.split(",")

    else:
        default_k8s_versions = K8S_VERSION.split(",")
    list_process = []
    network_plugins = NETWORK_PLUGINS
    dns_providers = DNS_PROVIDERS
    print("default_k8s_versions: ", default_k8s_versions)
    for k8s_version in default_k8s_versions:
        rke_config_new = deepcopy(rke_config)
        rke_config_new["kubernetesVersion"] = k8s_version
        node_count = NODE_COUNT_KDM_CLUSTER * len(network_plugins)
        if DNS_MATRIX:
            node_count = node_count * len(dns_providers) * 2
        aws_nodes = \
            AmazonWebServices().create_multiple_nodes(
                node_count, random_test_name(HOST_NAME))
        i = 0
        for network_plugin in network_plugins:
            if network_plugin == "calico" or \
                    network_plugin == "canal" or network_plugin == "weave":
                rke_config_new["network"]["options"] = \
                    {"flannel_backend_type": "vxlan"}
            rke_config_new["network"] = {"type": "networkConfig",
                                         "plugin": network_plugin}
            if DNS_MATRIX:
                for dns_provider in dns_providers:
                    for nodelocaldns in True, False:
                        dns_entry, cluster_name = get_dns_rke_config(
                            dns_provider,
                            network_plugin,
                            nodelocaldns
                        )
                        rke_config_new.update(dns_entry)