#!/usr/bin/python

# Owner: Steve Ovens <steve D0T ovens <AT> redhat -DOT- com>
# Date Created: May 2016
# Primary Function:
# This will export a config map from a project

from template_shared_code import TemplateParsing
import os

template_name = TemplateParsing.options.source_project_name + "_" + TemplateParsing.options.config_map_name + \
                "_configmap_template"
template_output_path = "/tmp/"
template_output = template_output_path + template_name + ".yaml"
export_command = "oc get configmap %s -n %s -o yaml" % (TemplateParsing.options.config_map_name,
                                                        TemplateParsing.options.source_project_name)
resource_dict = {"configmap": "import"}

TemplateParsing.substitute_values_in_template(export_command, template_output, resource_dict)
print("Config map generated: %s" % template_output_path)
    if TemplateParsing.options.ose_registry is not None and TemplateParsing.options.copy_build_config.lower() == "no" \
       and TemplateParsing.options.ose_token is not None and TemplateParsing.options.docker_username is not None:
        ose_resources_to_export = ['imagestream', 'deploymentconfig', 'service', 'route']
        resource_dictionary['image_deployment'] = TemplateParsing.options.ose_registry
        promote_image = True
    for resource in ose_resources_to_export:
        resource_with_apps.append("%s/%s" % (resource, TemplateParsing.options.app_name))
    export_command = "/usr/bin/oc export %s --as-template=%s" % (" ".join(resource_with_apps), template_name)
    # If the optional url flag was passed into the script, search the text for a route spec
    # At the time of writing this is denoted by "host: <url>" in the spec section of a route
    if TemplateParsing.options.url is not None or TemplateParsing.options.env_variables is not None:
        resource_dictionary['source_project'] = TemplateParsing.options.source_project_name
        resource_dictionary['destination_project'] = TemplateParsing.options.destination_project_name
        if TemplateParsing.options.url is not None:
            resource_dictionary["url"] = TemplateParsing.options.url
        if TemplateParsing.options.env_variables is not None:
            resource_dictionary["environment_vars"] = TemplateParsing.options.env_variables
        TemplateParsing.substitute_values_in_template(export_command, template_output, resource_dictionary)
    else:
        TemplateParsing.export_as_template(export_command, template_output)
else:
    print("%s was not found in project %s" % (TemplateParsing.options.app_name,
                                              TemplateParsing.options.source_project_name))
    sys.exit(2)

TemplateParsing.create_objects(TemplateParsing.options.destination_project_name, template_output)

if promote_image:
    TemplateParsing.docker_promote_image(TemplateParsing.options.ose_registry, TemplateParsing.options.docker_username,
                                     TemplateParsing.options.ose_token, TemplateParsing.options.source_project_name,
                                     TemplateParsing.options.destination_project_name, TemplateParsing.options.app_name)