#!/usr/bin/env python3

# *****************************************************************************
# Copyright (c) 2024 IBM Corporation and other Contributors.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# *****************************************************************************

from kubernetes import client, config
from kubernetes.config.config_exception import ConfigException
from mas.devops.db2 import validate_db2_config
import argparse
import logging

import urllib3
urllib3.disable_warnings()


if __name__ == "__main__":
    # Initialize the properties we need
    parser = argparse.ArgumentParser()

    # Primary Options
    parser.add_argument("--mas-instance-id", required=True)
    parser.add_argument("--mas-app-id", required=True)
    parser.add_argument("--database-role", default='primary', required=False)
    parser.add_argument("--log-level", required=False, choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="WARNING")

    args, unknown = parser.parse_known_args()

    log_level = getattr(logging, args.log_level)
    logging.basicConfig()
    logging.getLogger('mas.devops.db2').setLevel(level=log_level)

    try:
        # Try to load in-cluster configuration
        config.load_incluster_config()
        print("Loaded in-cluster configuration")
    except ConfigException:
        # If that fails, fall back to kubeconfig file
        config.load_kube_config()
        print("Loaded kubeconfig file")

    validate_db2_config(
        client.api_client.ApiClient(),
        args.mas_instance_id,
        args.mas_app_id,
        args.database_role
    )
