def test_yaml(): template = yaml.load('AMI: !Ref Old') replace_references(template, 'Old', 'New') result = StringIO() yaml.dump(template, result) assert result.getvalue() == 'AMI: !Ref New\n'
def update_template(self): resources = self.find_matching_resources() if not resources: return name = resources[-1] rest_api_gateway = self.template['Resources'][name] current_body = rest_api_gateway.get('Properties', {}).get('Body', {}) current_body_as_string = self.yaml_dump_to_str( current_body) if current_body else '' if self.body_as_string != current_body_as_string: if self.verbose: for text in difflib.unified_diff( current_body_as_string.split("\n"), self.body_as_string.split("\n")): if text[:3] not in ['---', '+++']: sys.stderr.write('{}\n'.format(text)) rest_api_gateway = self.copy_resource(rest_api_gateway) if not 'Properties' in rest_api_gateway: rest_api_gateway['Properties'] = {} rest_api_gateway['Properties']['Body'] = self.body if self.add_new_version: new_name = self.new_resource_name(name) sys.stderr.write( 'INFO: adding resource {} with new swagger body in template {}\n' .format(new_name, self.filename)) else: new_name = name sys.stderr.write( 'INFO: updating resource {} with swagger body in template {}\n' .format(new_name, self.filename)) if self.add_new_version: replace_references(self.template, name, new_name) for i in range(0, len(resources) - (self.keep - 1)): sys.stderr.write( 'INFO: removing resource {} from template {}\n'.format( resources[i], self.filename)) del self.template['Resources'][resources[i]] self.template['Resources'][new_name] = rest_api_gateway self.dirty = True else: sys.stderr.write( 'INFO: no changes of swagger body of {} in template {}\n'. format(self.resource_name, self.filename))
def test_simple(): template = {'Ref': 'Old'} assert replace_references(template, 'Old', 'New') assert template['Ref'] == 'New' template = {'DependsOn': [{'Ref': 'Old'}]} assert replace_references(template, 'Old', 'New') assert template['DependsOn'][0]['Ref'] == 'New' template = 'Old' assert not replace_references(template, 'Old', 'New') assert template == 'Old' template = {'DependsOn': [{'Ref': 'Old'}]} assert not replace_references(template, 'Oud', 'New') assert template['DependsOn'][0]['Ref'] == 'Old'