Beispiel #1
0
def __compare(template,
              stack,
              vars=None,
              parameters={},
              tags={},
              main_account_parameter=False):
    current_parameters = {
        p["ParameterKey"]: p["ParameterValue"]
        for p in (stack.get("Parameters", []))
    }
    parameters = {key: str(value) for key, value in parameters.items()}
    tags = {key: str(value) for key, value in tags.items()}
    current_tags = {p["Key"]: p["Value"] for p in (stack.get("Tags", []))}

    loader = Loader(variables=vars,
                    main_account_parameter=main_account_parameter)
    loader.load()
    deployed_template = convert(template)
    template_parameters = {
        key: str(value["Default"]).lower()
        if type(value["Default"]) == bool else str(value["Default"])
        for key, value in (
            loader.template_dictionary().get("Parameters", {})).items()
        if "Default" in value
    }

    template_parameters.update(parameters)
    if isinstance(deployed_template, str):
        deployed_template = yaml.full_load(deployed_template)

    __generate_table("Parameters", current_parameters, template_parameters)
    __generate_table("Tags", current_tags, tags)
    __generate_table("Template", deployed_template,
                     convert(loader.template_dictionary()))
Beispiel #2
0
def __compare(template,
              stack,
              vars=None,
              parameters={},
              tags={},
              main_account_parameter=False):
    current_parameters = {
        p['ParameterKey']: p['ParameterValue']
        for p in (stack.get('Parameters', []))
    }
    parameters = {key: str(value) for key, value in parameters.items()}
    tags = {key: str(value) for key, value in tags.items()}
    current_tags = {p['Key']: p['Value'] for p in (stack.get('Tags', []))}

    loader = Loader(variables=vars,
                    main_account_parameter=main_account_parameter)
    loader.load()
    deployed_template = convert(template)
    template_parameters = {
        key: str(value['Default']).lower()
        if type(value['Default']) == bool else str(value['Default'])
        for key, value in (
            loader.template_dictionary().get('Parameters', {})).items()
        if 'Default' in value
    }

    template_parameters.update(parameters)
    if isinstance(deployed_template, str):
        deployed_template = yaml.safe_load(deployed_template)

    __generate_table('Parameters', current_parameters, template_parameters)
    __generate_table('Tags', current_tags, tags)
    __generate_table('Template', deployed_template,
                     convert(loader.template_dictionary()))
Beispiel #3
0
def test_supports_extra_jinja_vars(tmpdir):
    load = Loader(variables={'test': 'bar'})
    example = '{"Description": "{{ test | title }}"}'
    with Path(tmpdir):
        with open('test.template.json', 'w') as f:
            f.write(example)
        load.load()
        actual = json.loads(load.template())
    assert actual == {"Description": "Bar"}
Beispiel #4
0
def test_module_vars_have_precedence_over_global(tmpdir):
    load = Loader(variables={'test': 'bar'})
    example = '{"Description": "{{ test }}"}'
    with Path(tmpdir):
        os.mkdir('moduledir')
        with open('moduledir/test.template.json', 'w') as f:
            f.write(example)
        with open('test.template.json', 'w') as f:
            f.write('{"Resources": {"TestResource": {"From": "Moduledir", "Properties": {"test": "baz" } }}}')
        load.load()
        actual = json.loads(load.template())
    assert actual == {"Description": "baz"}
Beispiel #5
0
def test_code_passes_variables(tmpdir):
    load = Loader(variables={'SomeVariable': 'SomeValue'})
    template = '{"Resources": {"{{code("testfile")}}": "Test"}}'
    code = '{{SomeVariable}}'
    with Path(tmpdir):
        with open('test.template.json', 'w') as f:
            f.write(template)
        with open('testfile', 'w') as f:
            f.write(code)
        load.load()

        actual = json.loads(load.template())
    assert actual == {"Resources": {"SomeValue": "Test"}}
Beispiel #6
0
    def run(self, stack, variables=None):
        client = self.cf_client()

        result = client.get_template(StackName=stack, )

        loader = Loader(variables=variables)
        loader.load()
        deployed_template = convert(result['TemplateBody'])
        if isinstance(deployed_template, str):
            deployed_template = yaml.load(deployed_template)

        changes = DeepDiff(deployed_template,
                           convert(loader.template_dictionary()),
                           ignore_order=False,
                           report_repetition=True,
                           verbose_level=2,
                           view='tree')

        table = Texttable(max_width=200)
        table.add_rows([['Path', 'From', 'To', 'Change Type']])
        print_diff = False

        processed_changes = self.__collect_changes(changes)

        for change in processed_changes:
            print_diff = True
            path = re.findall("\['?(\w+)'?\]", change.path)
            table.add_row([
                ' > '.join(path), change.before, change.after,
                change.type.title().replace('_', ' ')
            ])

        if print_diff:
            logger.info(table.draw() + "\n")
        else:
            logger.info('No Changes found')