def create_sc_product_template()

in lambda/source/create_sc_template.py [0:0]


def create_sc_product_template(file_path, file_name, sc_product_template_target):
    # Unzip the ALZ Add-On package
    zip_call = zipfile.ZipFile(file_path + file_name)
    zip_call.extractall(file_path)
    zip_call.close()

    # Use Service Catalog product template
    shutil.copyfile(sc_product_template_src, file_path + sc_product_template_target)

    # Get the content of Service Catalog product template
    with open(file_path + sc_product_template_target, 'r') as yaml_file:
        sc_product_template_content = yaml.load(yaml_file)

    # Get ALZ Add-On product template
    for root, dirs, files in os.walk(file_path):
        for name in files:
            if (root == file_path + "templates/core_accounts") and (".template" in name):
                alz_add_on_template = "templates/core_accounts/" + name
                break

    # Convert the product template in json to template in yaml
    file = file_path + alz_add_on_template
    if (utils.is_json(file) is True) and (utils.is_yaml(file) is False):
        utils.json_2_yaml(file, file_path + 'temp_yaml.template')
        shutil.move(file_path + 'temp_yaml.template', file_path + alz_add_on_template)
    elif (utils.is_json(file) is False) and (utils.is_yaml(file) is True):
        pass
    else:
        raise ValueError('The file is neither json nor yaml! exit()')
        exit()

    # Get the content of ALZ Add-On product template
    parameter_dict = dict()
    with open(file_path + alz_add_on_template, 'r') as yaml_file:
        alz_add_on_template_content = yaml.load(yaml_file)
        alz_add_on_template_parameters = alz_add_on_template_content["Parameters"]
        for key in alz_add_on_template_parameters:
            parameter_dict[key] = ("!Ref " + key)

    # Populate Service Catalog product template from ALZ Add-On template
    sc_product_template_content["Parameters"].update(alz_add_on_template_content["Parameters"])
    sc_product_template_content["Metadata"]["AWS::CloudFormation::Interface"]["ParameterGroups"].extend\
        (alz_add_on_template_content["Metadata"]["AWS::CloudFormation::Interface"]["ParameterGroups"])
    sc_product_template_content["Resources"]["LandingZoneAddOnConfigDeployer"]["Properties"]["find_replace"][0]["parameters"].update(parameter_dict)

    if sc_product_template_content:
        with open(file_path + sc_product_template_target, 'w') as yaml_file:
            yaml.dump(sc_product_template_content, yaml_file)

    # Clean up extra quotes
    with open(file_path + sc_product_template_target, 'r+') as text_file:
        sc_product_template_content = text_file.readlines()
        text_file.seek(0)
        for line in sc_product_template_content:
            if "'!Ref" in line:
                text_file.write(line.replace("'", ""))
            else:
                text_file.write(line)
        text_file.truncate()
    return