#!/usr/bin/python
# Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# 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.

import argparse
import logging
import os
import sys

import os_client_config
from tripleo_common.utils import config


def get_orchestration_client():
    return os_client_config.make_client('orchestration')


def get_args():
    parser = argparse.ArgumentParser(
                description=("tripleo-config-download"),
                formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--stack-name', '-s',
                        default='overcloud',
                        help="Heat stack name")
    parser.add_argument('--output-dir', '-o',
                        default='tripleo-config-download',
                        help="Output directory for downloaded config")

    args = parser.parse_args(sys.argv[1:])
    return args

if __name__ == '__main__':
    args = get_args()

    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.INFO)

    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)

    client = get_orchestration_client()
    stack_config = config.Config(client)
    stack_config.download_config(args.stack_name, args.output_dir)
