#
# Copyright (c) 2019 EXXETA AG and others.
#
# This file is part of k8s-python-tools
# (see https://github.com/EXXETA/k8s-python-tools).
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
from kubernetes.client.rest import ApiException

from lib.common import load_kube, SetType
from kubernetes import client
import time
import datetime

wait_timeout_seconds = 30

# DO NOT EDIT this file manually, use "python codegen.py" in root folder. Use templates/def_check.py.j2



def wait_for_namespace_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Namespace given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Namespace", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "Namespace", name,
                              lambda: api.read_namespace_with_http_info(namespace))


def wait_for_deployment_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Deployment given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Deployment", name, "in namespace", namespace)
    api = client.ExtensionsV1beta1Api()
    return general_away_check(namespace, "Deployment", name,
                              lambda: api.read_namespaced_deployment_status_with_http_info(name, namespace=namespace))


def wait_for_pod_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Pod given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Pod", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "Pod", name,
                              lambda: api.read_namespaced_pod_status_with_http_info(name, namespace=namespace))


def wait_for_service_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Service given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Service", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "Service", name,
                              lambda: api.read_namespaced_service_status_with_http_info(name, namespace=namespace))


def wait_for_replication_controller_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ReplicationController given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "ReplicationController", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "ReplicationController", name,
                              lambda: api.read_namespaced_replication_controller_status_with_http_info(name, namespace=namespace))


def wait_for_persistent_volume_claim_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for PersistentVolumeClaim given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "PersistentVolumeClaim", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "PersistentVolumeClaim", name,
                              lambda: api.read_namespaced_persistent_volume_claim_with_http_info(name, namespace=namespace))


def wait_for_ingress_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Ingress given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Ingress", name, "in namespace", namespace)
    api = client.ExtensionsV1beta1Api()
    return general_away_check(namespace, "Ingress", name,
                              lambda: api.read_namespaced_ingress_status_with_http_info(name, namespace=namespace))


def wait_for_network_policy_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for NetworkPolicy given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "NetworkPolicy", name, "in namespace", namespace)
    api = client.ExtensionsV1beta1Api()
    return general_away_check(namespace, "NetworkPolicy", name,
                              lambda: api.read_namespaced_network_policy_with_http_info(name, namespace=namespace))


def wait_for_job_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Job given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Job", name, "in namespace", namespace)
    api = client.BatchV1Api()
    return general_away_check(namespace, "Job", name,
                              lambda: api.read_namespaced_job_status_with_http_info(name, namespace=namespace))


def wait_for_cron_job_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for CronJob given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "CronJob", name, "in namespace", namespace)
    api = client.BatchV1beta1Api()
    return general_away_check(namespace, "CronJob", name,
                              lambda: api.read_namespaced_cron_job_status_with_http_info(name, namespace=namespace))


def wait_for_config_map_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ConfigMap given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "ConfigMap", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "ConfigMap", name,
                              lambda: api.read_namespaced_config_map_with_http_info(name, namespace=namespace))


def wait_for_secret_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Secret given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Secret", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "Secret", name,
                              lambda: api.read_namespaced_secret_with_http_info(name, namespace=namespace))


def wait_for_service_account_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ServiceAccount given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "ServiceAccount", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "ServiceAccount", name,
                              lambda: api.read_namespaced_service_account_with_http_info(name, namespace=namespace))


def wait_for_resource_quota_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ResourceQuota given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "ResourceQuota", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "ResourceQuota", name,
                              lambda: api.read_namespaced_resource_quota_with_http_info(name, namespace=namespace))


def wait_for_endpoints_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Endpoints given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Endpoints", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "Endpoints", name,
                              lambda: api.read_namespaced_endpoints_with_http_info(name, namespace=namespace))


def wait_for_pod_template_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for PodTemplate given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "PodTemplate", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "PodTemplate", name,
                              lambda: api.read_namespaced_pod_template_with_http_info(name, namespace=namespace))


def wait_for_role_binding_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for RoleBinding given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "RoleBinding", name, "in namespace", namespace)
    api = client.RbacAuthorizationV1Api()
    return general_away_check(namespace, "RoleBinding", name,
                              lambda: api.read_namespaced_role_binding_with_http_info(name, namespace=namespace))


def wait_for_role_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Role given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Role", name, "in namespace", namespace)
    api = client.RbacAuthorizationV1Api()
    return general_away_check(namespace, "Role", name,
                              lambda: api.read_namespaced_role_with_http_info(name, namespace=namespace))


def wait_for_pod_disruption_budget_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for PodDisruptionBudget given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "PodDisruptionBudget", name, "in namespace", namespace)
    api = client.PolicyV1beta1Api()
    return general_away_check(namespace, "PodDisruptionBudget", name,
                              lambda: api.read_namespaced_pod_disruption_budget_with_http_info(name, namespace=namespace))


def wait_for_event_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Event given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Event", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "Event", name,
                              lambda: api.read_namespaced_event_with_http_info(name, namespace=namespace))


def wait_for_lease_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Lease given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "Lease", name, "in namespace", namespace)
    api = client.CoordinationV1beta1Api()
    return general_away_check(namespace, "Lease", name,
                              lambda: api.read_namespaced_lease_with_http_info(name, namespace=namespace))


def wait_for_horizontal_pod_autoscaler_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for HorizontalPodAutoscaler given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "HorizontalPodAutoscaler", name, "in namespace", namespace)
    api = client.AutoscalingV1Api()
    return general_away_check(namespace, "HorizontalPodAutoscaler", name,
                              lambda: api.read_namespaced_horizontal_pod_autoscaler_with_http_info(name, namespace=namespace))


def wait_for_controller_revision_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ControllerRevision given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "ControllerRevision", name, "in namespace", namespace)
    api = client.AppsV1Api()
    return general_away_check(namespace, "ControllerRevision", name,
                              lambda: api.read_namespaced_controller_revision_with_http_info(name, namespace=namespace))


def wait_for_limit_range_is_away(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for LimitRange given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check removal of", "LimitRange", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_away_check(namespace, "LimitRange", name,
                              lambda: api.read_namespaced_limit_range_with_http_info(name, namespace=namespace))


def wait_for_cluster_role_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for ClusterRole given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "ClusterRole", name)
    api = client.RbacAuthorizationV1Api()
    return general_away_check(None, "ClusterRole", name,
                              lambda: api.read_cluster_role_with_http_info(name))


def wait_for_cluster_role_binding_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for ClusterRoleBinding given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "ClusterRoleBinding", name)
    api = client.RbacAuthorizationV1Api()
    return general_away_check(None, "ClusterRoleBinding", name,
                              lambda: api.read_cluster_role_binding_with_http_info(name))


def wait_for_pod_security_policy_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for PodSecurityPolicy given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "PodSecurityPolicy", name)
    api = client.PolicyV1beta1Api()
    return general_away_check(None, "PodSecurityPolicy", name,
                              lambda: api.read_pod_security_policy_with_http_info(name))


def wait_for_persistent_volume_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for PersistentVolume given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "PersistentVolume", name)
    api = client.CoreV1Api()
    return general_away_check(None, "PersistentVolume", name,
                              lambda: api.read_persistent_volume_with_http_info(name))


def wait_for_volume_attachment_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for VolumeAttachment given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "VolumeAttachment", name)
    api = client.StorageV1Api()
    return general_away_check(None, "VolumeAttachment", name,
                              lambda: api.read_volume_attachment_with_http_info(name))


def wait_for_storage_class_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for StorageClass given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "StorageClass", name)
    api = client.StorageV1Api()
    return general_away_check(None, "StorageClass", name,
                              lambda: api.read_storage_class_with_http_info(name))


def wait_for_priority_class_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for PriorityClass given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "PriorityClass", name)
    api = client.SchedulingV1beta1Api()
    return general_away_check(None, "PriorityClass", name,
                              lambda: api.read_priority_class_with_http_info(name))


def wait_for_node_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for Node given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "Node", name)
    api = client.CoreV1Api()
    return general_away_check(None, "Node", name,
                              lambda: api.read_node_with_http_info(name))


def wait_for_custom_resource_definition_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for CustomResourceDefinition given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "CustomResourceDefinition", name)
    api = client.ApiextensionsV1beta1Api()
    return general_away_check(None, "CustomResourceDefinition", name,
                              lambda: api.read_custom_resource_definition_with_http_info(name))


def wait_for_certificate_signing_request_is_away(context, name):
    if name is None:
        raise SystemExit("invalid empty name for CertificateSigningRequest given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check removal of", "CertificateSigningRequest", name)
    api = client.CertificatesV1beta1Api()
    return general_away_check(None, "CertificateSigningRequest", name,
                              lambda: api.read_certificate_signing_request_with_http_info(name))



def wait_for_namespace_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Namespace given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Namespace", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "Namespace", name,
                            lambda: api.read_namespace_with_http_info(namespace))


def wait_for_deployment_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Deployment given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Deployment", name, "in namespace", namespace)
    api = client.ExtensionsV1beta1Api()
    return general_up_check(namespace, "Deployment", name,
                            lambda: api.read_namespaced_deployment_status_with_http_info(name, namespace=namespace))


def wait_for_pod_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Pod given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Pod", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "Pod", name,
                            lambda: api.read_namespaced_pod_status_with_http_info(name, namespace=namespace))


def wait_for_service_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Service given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Service", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "Service", name,
                            lambda: api.read_namespaced_service_status_with_http_info(name, namespace=namespace))


def wait_for_replication_controller_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ReplicationController given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "ReplicationController", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "ReplicationController", name,
                            lambda: api.read_namespaced_replication_controller_status_with_http_info(name, namespace=namespace))


def wait_for_persistent_volume_claim_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for PersistentVolumeClaim given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "PersistentVolumeClaim", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "PersistentVolumeClaim", name,
                            lambda: api.read_namespaced_persistent_volume_claim_with_http_info(name, namespace=namespace))


def wait_for_ingress_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Ingress given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Ingress", name, "in namespace", namespace)
    api = client.ExtensionsV1beta1Api()
    return general_up_check(namespace, "Ingress", name,
                            lambda: api.read_namespaced_ingress_status_with_http_info(name, namespace=namespace))


def wait_for_network_policy_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for NetworkPolicy given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "NetworkPolicy", name, "in namespace", namespace)
    api = client.ExtensionsV1beta1Api()
    return general_up_check(namespace, "NetworkPolicy", name,
                            lambda: api.read_namespaced_network_policy_with_http_info(name, namespace=namespace))


def wait_for_job_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Job given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Job", name, "in namespace", namespace)
    api = client.BatchV1Api()
    return general_up_check(namespace, "Job", name,
                            lambda: api.read_namespaced_job_status_with_http_info(name, namespace=namespace))


def wait_for_cron_job_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for CronJob given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "CronJob", name, "in namespace", namespace)
    api = client.BatchV1beta1Api()
    return general_up_check(namespace, "CronJob", name,
                            lambda: api.read_namespaced_cron_job_status_with_http_info(name, namespace=namespace))


def wait_for_config_map_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ConfigMap given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "ConfigMap", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "ConfigMap", name,
                            lambda: api.read_namespaced_config_map_with_http_info(name, namespace=namespace))


def wait_for_secret_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Secret given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Secret", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "Secret", name,
                            lambda: api.read_namespaced_secret_with_http_info(name, namespace=namespace))


def wait_for_service_account_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ServiceAccount given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "ServiceAccount", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "ServiceAccount", name,
                            lambda: api.read_namespaced_service_account_with_http_info(name, namespace=namespace))


def wait_for_resource_quota_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ResourceQuota given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "ResourceQuota", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "ResourceQuota", name,
                            lambda: api.read_namespaced_resource_quota_with_http_info(name, namespace=namespace))


def wait_for_endpoints_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Endpoints given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Endpoints", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "Endpoints", name,
                            lambda: api.read_namespaced_endpoints_with_http_info(name, namespace=namespace))


def wait_for_pod_template_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for PodTemplate given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "PodTemplate", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "PodTemplate", name,
                            lambda: api.read_namespaced_pod_template_with_http_info(name, namespace=namespace))


def wait_for_role_binding_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for RoleBinding given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "RoleBinding", name, "in namespace", namespace)
    api = client.RbacAuthorizationV1Api()
    return general_up_check(namespace, "RoleBinding", name,
                            lambda: api.read_namespaced_role_binding_with_http_info(name, namespace=namespace))


def wait_for_role_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Role given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Role", name, "in namespace", namespace)
    api = client.RbacAuthorizationV1Api()
    return general_up_check(namespace, "Role", name,
                            lambda: api.read_namespaced_role_with_http_info(name, namespace=namespace))


def wait_for_pod_disruption_budget_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for PodDisruptionBudget given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "PodDisruptionBudget", name, "in namespace", namespace)
    api = client.PolicyV1beta1Api()
    return general_up_check(namespace, "PodDisruptionBudget", name,
                            lambda: api.read_namespaced_pod_disruption_budget_with_http_info(name, namespace=namespace))


def wait_for_event_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Event given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Event", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "Event", name,
                            lambda: api.read_namespaced_event_with_http_info(name, namespace=namespace))


def wait_for_lease_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for Lease given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "Lease", name, "in namespace", namespace)
    api = client.CoordinationV1beta1Api()
    return general_up_check(namespace, "Lease", name,
                            lambda: api.read_namespaced_lease_with_http_info(name, namespace=namespace))


def wait_for_horizontal_pod_autoscaler_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for HorizontalPodAutoscaler given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "HorizontalPodAutoscaler", name, "in namespace", namespace)
    api = client.AutoscalingV1Api()
    return general_up_check(namespace, "HorizontalPodAutoscaler", name,
                            lambda: api.read_namespaced_horizontal_pod_autoscaler_with_http_info(name, namespace=namespace))


def wait_for_controller_revision_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for ControllerRevision given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "ControllerRevision", name, "in namespace", namespace)
    api = client.AppsV1Api()
    return general_up_check(namespace, "ControllerRevision", name,
                            lambda: api.read_namespaced_controller_revision_with_http_info(name, namespace=namespace))


def wait_for_limit_range_is_up(context, namespace, name):
    if name is None:
        raise SystemExit("invalid empty name for LimitRange given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    if namespace is None:
        raise SystemExit("invalid empty name namespace given")
    load_kube(context)
    print("check availability of", "LimitRange", name, "in namespace", namespace)
    api = client.CoreV1Api()
    return general_up_check(namespace, "LimitRange", name,
                            lambda: api.read_namespaced_limit_range_with_http_info(name, namespace=namespace))


def wait_for_cluster_role_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for ClusterRole given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "ClusterRole", name)
    api = client.RbacAuthorizationV1Api()
    return general_up_check(None, "ClusterRole", name,
                            lambda: api.read_cluster_role_with_http_info(name))


def wait_for_cluster_role_binding_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for ClusterRoleBinding given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "ClusterRoleBinding", name)
    api = client.RbacAuthorizationV1Api()
    return general_up_check(None, "ClusterRoleBinding", name,
                            lambda: api.read_cluster_role_binding_with_http_info(name))


def wait_for_pod_security_policy_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for PodSecurityPolicy given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "PodSecurityPolicy", name)
    api = client.PolicyV1beta1Api()
    return general_up_check(None, "PodSecurityPolicy", name,
                            lambda: api.read_pod_security_policy_with_http_info(name))


def wait_for_persistent_volume_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for PersistentVolume given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "PersistentVolume", name)
    api = client.CoreV1Api()
    return general_up_check(None, "PersistentVolume", name,
                            lambda: api.read_persistent_volume_with_http_info(name))


def wait_for_volume_attachment_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for VolumeAttachment given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "VolumeAttachment", name)
    api = client.StorageV1Api()
    return general_up_check(None, "VolumeAttachment", name,
                            lambda: api.read_volume_attachment_with_http_info(name))


def wait_for_storage_class_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for StorageClass given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "StorageClass", name)
    api = client.StorageV1Api()
    return general_up_check(None, "StorageClass", name,
                            lambda: api.read_storage_class_with_http_info(name))


def wait_for_priority_class_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for PriorityClass given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "PriorityClass", name)
    api = client.SchedulingV1beta1Api()
    return general_up_check(None, "PriorityClass", name,
                            lambda: api.read_priority_class_with_http_info(name))


def wait_for_node_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for Node given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "Node", name)
    api = client.CoreV1Api()
    return general_up_check(None, "Node", name,
                            lambda: api.read_node_with_http_info(name))


def wait_for_custom_resource_definition_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for CustomResourceDefinition given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "CustomResourceDefinition", name)
    api = client.ApiextensionsV1beta1Api()
    return general_up_check(None, "CustomResourceDefinition", name,
                            lambda: api.read_custom_resource_definition_with_http_info(name))


def wait_for_certificate_signing_request_is_up(context, name):
    if name is None:
        raise SystemExit("invalid empty name for CertificateSigningRequest given")
    if context is None:
        raise SystemExit("invalid empty name context given")
    load_kube(context)
    print("check availability of", "CertificateSigningRequest", name)
    api = client.CertificatesV1beta1Api()
    return general_up_check(None, "CertificateSigningRequest", name,
                            lambda: api.read_certificate_signing_request_with_http_info(name))

def wait_for_set_is_away(context, namespace, set_type, set_name):
    load_kube(context)
    print("check removal of set", set_name, "of type", set_type, "in namespace", namespace)
    api = client.AppsV1Api()
    def handle_sets():
        if set_type == SetType.replica_set:
            return api.read_namespaced_replica_set_with_http_info(set_name, namespace=namespace)
        elif set_type == SetType.stateful_set:
            return api.read_namespaced_stateful_set_with_http_info(set_name, namespace=namespace)
        elif set_type == SetType.daemon_set:
            return api.read_namespaced_daemon_set_with_http_info(set_name, namespace=namespace)
        else:
            raise SystemExit("Invalid type", set_type)

    return general_away_check(namespace, set_type, set_name, handle_sets)

# internal methods (used by methods above)


def general_away_check(namespace, object_name, name, api_query_lambda):
    return general_status_check(namespace, object_name, name, api_query_lambda, 404)


def general_up_check(namespace, object_name, name, api_query_lambda):
    return general_status_check(namespace, object_name, name, api_query_lambda, 200)


def general_status_check(namespace, object_name, name, api_query_lambda, expected_status):
    status = 0
    attempts = 0
    start_date = time.mktime(datetime.datetime.utcnow().timetuple())
    while status != expected_status:
        try:
            attempts += 1
            ret, status, _ = api_query_lambda()
        except ApiException:
            if namespace is not None:
                print("Status", expected_status,
                      "received.", object_name, name, "in namespace", namespace, "is removed")
            else:
                print("Status", expected_status, "received.", object_name, name)
            status = 404
        time.sleep(0.75)
        if time.mktime(datetime.datetime.utcnow().timetuple()) - start_date > wait_timeout_seconds:
            print(object_name, "status", expected_status,
                  "could not be checked after a timeout of", wait_timeout_seconds, "seconds and", attempts, "checks")
            return False
    return True


def main():
    pass


if __name__ == "__main__":
    main()