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()))
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()))
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')