#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Backup Foreman configuration to YAML files

"""
import sys, getopt
import os
import yaml
from foreman import Foreman

def show_help():
    """Print on screen how to use this script.
    """
    print('foreman.py -f <foreman_host> -p <port> -u <username> -s <secret>')

def backup(backup_dir, resource, resource_function):
    """Backup Foreman resource as YAML file into a directory.

    A new directory named <resource> will be created inside <backup_dir>. Each
    resource fetched by <resource_function> will be saved in an own YAML file
    called <resource_name>.yaml in <backup_dir>/<resource>.

    Args:
      backup_dir (str): Directory where to create the backup files
      resource (str): Name of the resource to backup (e.g. 'architectures')
      resource_function (def): Name of function to call to get a dict of resources
    """
    backup_dir = os.path.join(backup_dir, resource)

    if not os.path.exists(backup_dir):
        os.makedirs(backup_dir)

    for item in resource_function:
        backup_file = os.path.join(backup_dir, item.get('name').replace('/', '_') + '.yaml')
        with open(backup_file, 'w') as backup_file:
            yaml.safe_dump(item, backup_file, default_flow_style=False)

def main(argv):
    """ Main

    Backup Foreman resources
    """
    theforeman = None
    foreman_host = os.environ.get('FOREMAN_HOST', '127.0.0.1')
    foreman_port = os.environ.get('FOREMAN_PORT', '443')
    foreman_username = os.environ.get('FOREMAN_USERNAME', 'foreman')
    foreman_password = os.environ.get('FOREMAN_PASSWORD', 'changme')

    try:
        opts, args = getopt.getopt(argv,
                                   "f:hu:p:s:",
                                   ["foreman=", "username=", "port=", "secret="])
    except getopt.GetoptError:
        show_help()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-f', '--foreman'):
            foreman_host = arg
        elif opt == '-h':
            show_help()
            sys.exit()
        elif opt in ('-u', '--username'):
            foreman_username = arg
        elif opt in ('-p', '--port'):
            foreman_port = arg
        elif opt in ('-s', '--secret'):
            foreman_password = arg

    theforeman = Foreman(foreman_host, foreman_port, foreman_username, foreman_password)
    backup_root = '.'

    backup(backup_dir=backup_root, resource='architectures',
           resource_function=theforeman.get_architectures())
    backup(backup_dir=backup_root, resource='compute_resources',
           resource_function=theforeman.get_compute_resources())
    backup(backup_dir=backup_root, resource='compute_profiles',
           resource_function=theforeman.get_compute_profiles())
    backup(backup_dir=backup_root, resource='domains',
           resource_function=theforeman.get_domains())
    backup(backup_dir=backup_root, resource='environments',
           resource_function=theforeman.get_environments())
    backup(backup_dir=backup_root, resource='hosts',
           resource_function=theforeman.get_hosts())
    backup(backup_dir=backup_root, resource='hostgroups',
           resource_function=theforeman.get_hostgroups())
    backup(backup_dir=backup_root, resource='locations',
           resource_function=theforeman.get_locations())
    backup(backup_dir=backup_root, resource='media',
           resource_function=theforeman.get_media())
    backup(backup_dir=backup_root, resource='organizations',
           resource_function=theforeman.get_organizations())
    backup(backup_dir=backup_root, resource='operatingsystems',
           resource_function=theforeman.get_operatingsystems())
    backup(backup_dir=backup_root, resource='smart_proxies',
           resource_function=theforeman.get_smart_proxies())
    backup(backup_dir=backup_root, resource='subnets',
           resource_function=theforeman.get_subnets())

if __name__ == '__main__':
    main(sys.argv[1:])
