#!/usr/bin/env python3

# Copyright (c) 2023 VEXXHOST, Inc.
#
# 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.

# This script is used to sync the CCM manifests from the CCM repo to the
# manifests folder.

import requests
import yaml

MANIFESTS = set(
    [
        "csi-nfs-node.yaml",
        "csi-nfs-controller.yaml",
        "csi-nfs-driverinfo.yaml",
        "rbac-csi-nfs.yaml",
    ]
)
NFS_CSI_TAG = "v4.2.0"


for manifest in MANIFESTS:
    url = f"https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/{NFS_CSI_TAG}/{manifest}"  # noqa E501

    docs = []
    r = requests.get(url)

    for doc in yaml.safe_load_all(r.text):
        if doc["kind"] == "List":
            for item in doc["items"]:
                docs.append(item)
        else:
            docs.append(doc)

    with open(f"magnum_cluster_api/manifests/nfs-csi/{manifest}", "w") as fd:
        yaml.dump_all(docs, fd, default_flow_style=False)
