
# Copyright 2017-present Open Networking Foundation
#
# Licensed 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.

"""
    sync_secret.py

    Synchronize Secrets.
"""

import json
from synchronizers.new_base.syncstep import SyncStep
from synchronizers.new_base.modelaccessor import KubernetesSecret

from xosconfig import Config
from multistructlog import create_logger

from kubernetes.client.rest import ApiException
from kubernetes import client as kubernetes_client, config as kubernetes_config

log = create_logger(Config().get('logging'))

class SyncKubernetesSecret(SyncStep):

    """
        SyncKubernetesSecret

        Implements sync step for syncing Secrets.
    """

    provides = [KubernetesSecret]
    observes = KubernetesSecret
    requested_interval = 0

    def __init__(self, *args, **kwargs):
        super(SyncKubernetesSecret, self).__init__(*args, **kwargs)
        kubernetes_config.load_incluster_config()
        self.v1 = kubernetes_client.CoreV1Api()

    def get_secret(self, o):
        """ Given an XOS KubernetesSecret object, read the corresponding Secret from Kubernetes.
            return None if no Secret exists.
        """
        try:
            secret = self.v1.read_namespaced_secret(o.name, o.trust_domain.name)
        except ApiException, e:
            if e.status == 404:
                return None
            raise
        return secret

    def sync_record(self, o):
            secret = self.get_secret(o)
            if not secret:
                secret = kubernetes_client.V1Secret()
                secret.data = json.loads(o.data)
                secret.metadata = kubernetes_client.V1ObjectMeta(name=o.name)

                secret = self.v1.create_namespaced_secret(o.trust_domain.name, secret)
            else:
                secret.data = json.loads(o.data)
                self.v1.patch_namespaced_secret(o.name, o.trust_domain.name, secret)

            if (not o.backend_handle):
                o.backend_handle = secret.metadata.self_link
                o.save(update_fields=["backend_handle"])

    def delete_record(self, port):
        # TODO(smbaker): Implement delete step
        pass

