def test_load_yaml(): """ Should map to an ordered dict """ source = """z: first m: !Sub - The cake is a ${CakeType} - CakeType: lie a: !Ref last """ actual = load_yaml(source) assert type(actual) == ODict assert list(actual.keys()) == ["z", "m", "a"] assert actual["z"] == "first" assert actual["m"] == { "Fn::Sub": [ "The cake is a ${CakeType}", { "CakeType": "lie", }, ], } assert actual["a"] == {"Ref": "last"}
def _load_fragment(fragment_file): try: with open(fragment_file, "r", encoding="utf-8") as f: return load_yaml(__first_pass_syntax_check(f.read())) except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e: raise FragmentValidationError( "Fragment file '{}' is invalid: {}".format(fragment_file, str(e)) ) from e
def _validate_parameter_data(template, parameter_data): '''Drops keys not in CFN template''' text = open(template).read() template_data = load_yaml(text) template_parameters = list(template_data['Parameters'].keys()) for param in parameter_data: if param['ParameterKey'] not in template_parameters: parameter_data.remove(param) print(f"Removed {param['ParameterKey']} from params") return parameter_data
def yaml_to_json(p_input_file): json_out = ROOT_DIR+'/../../../tacocat_cf.json' with open( p_input_file,'r') as yaml_in: yaml_data = load_yaml(yaml_in) yaml_data = clean(yaml_data) with open(json_out,'w') as out_file: json_data = dump_json(yaml_data) json_data = load_json(json_data) json.dump(json_data ,out_file, sort_keys=True, indent=4, separators=(',', ': ')) print('*--*--*--*--*--*--*--*--*--*--*--*--**--*--*--*--*--*--*--*--*--*--*--*') print('The new JSON file has been created in the root folder of the project with the name tacocat_cf.json') print('*--*--*--*--*--*--*--*--*--*--*--*--**--*--*--*--*--*--*--*--*--*--*--*')
def flip(template, in_format=None, out_format=None, clean_up=False, no_flip=False, long_form=False): """ Figure out the input format and convert the data to the opposing output format """ # Do we need to figure out the input format? if not in_format: # Load the template as JSON? if (out_format == "json" and no_flip) or (out_format == "yaml" and not no_flip): in_format = "json" elif (out_format == "yaml" and no_flip) or (out_format == "json" and not no_flip): in_format = "yaml" # Load the data if in_format == "json": data = load_json(template) elif in_format == "yaml": data = load_yaml(template) else: data, in_format = load(template) # Clean up? if clean_up: data = clean(data) # Figure out the output format if not out_format: if (in_format == "json" and no_flip) or (in_format == "yaml" and not no_flip): out_format = "json" else: out_format = "yaml" # Finished! if out_format == "json": if sys.version[0] == "3": return dump_json(data) else: return dump_json(data).encode('utf-8') return dump_yaml(data, clean_up, long_form)
def generate(self, template_path): with open(template_path, 'r') as template: cfn_yaml = load_yaml(template.read()) parameter_groups = cfn_yaml['Metadata'][ 'AWS::CloudFormation::Interface']['ParameterGroups'] parameter_labels = cfn_yaml['Metadata'][ 'AWS::CloudFormation::Interface']['ParameterLabels'] parameters = cfn_yaml['Parameters'] print( dump_yaml({ "ParameterGroups": parameter_groups, "ParameterLabels": parameter_labels, "Parameters": parameters }))
def synth(context: "Context", top_level: str = "orbit") -> str: outdir = os.path.join(os.getcwd(), ".orbit.out", context.name, "toolkit") try: shutil.rmtree(outdir) except FileNotFoundError: pass os.makedirs(outdir, exist_ok=True) output_filename = os.path.join(outdir, FILENAME) _logger.debug("Reading %s", MODEL_FILENAME) with open(MODEL_FILENAME, "r") as file: template = load_yaml(file) _logger.debug( "manifest.name: %s | manifest.account_id: %s | manifest.region: %s | manifest.deploy_id: %s", context.name, context.account_id, context.region, context.toolkit.deploy_id, ) if context.policies: template["Resources"]["AdminRole"]["Properties"][ "ManagedPolicyArns"].extend([ f"arn:aws:iam::{context.account_id}:policy/{policy}" for policy in context.policies ]) if context.toolkit.codeseeder_policy: template["Resources"]["AdminRole"]["Properties"][ "ManagedPolicyArns"].extend([context.toolkit.codeseeder_policy]) content: str = yaml.dump(template, Dumper=yaml_dumper.get_dumper()) content = content.replace("$", "").format( top_level=top_level, role_prefix=f"/{context.role_prefix}/" if context.role_prefix else "/", env_name=context.name, account_id=context.account_id, region=context.region, deploy_id=context.toolkit.deploy_id, ) _logger.debug("Writing %s", output_filename) os.makedirs(outdir, exist_ok=True) with open(output_filename, "w") as file: file.write(content) return output_filename
def convert_yaml_to_json(yaml_file): converted_file = ROOT_DIR + '/../../../tacocat_cf.json' with open(yaml_file, 'r') as file_in: yaml_data = load_yaml(file_in) yaml_data = clean(yaml_data) with open(converted_file, 'w') as file_out: json_data = dump_json(yaml_data) json_data = load_json(json_data) json.dump(json_data, file_out, sort_keys=True, indent=4, separators=(',', ': ')) print( '#####--Converted successfully, please check the root folder with the name tacocat_cf.json--#####' )
def run(self): template_contents = self.input().open("r").read() template = cfn_tools.load_yaml(template_contents) friendly_uid = template.get("Description").split("\n")[0] self.info(f"creating the stack: {friendly_uid}") tags = [] for tag in self.tags: tags.append({ "Key": tag.get("Key"), "Value": tag.get("Value"), }) with self.client("cloudformation") as cloudformation: if self.template.get("Name"): response = cloudformation.create_or_update( StackName=friendly_uid, TemplateBody=template_contents, Tags=tags, ) elif self.provisioner.get("Type") == "CloudFormation": response = cloudformation.create_or_update( StackName=friendly_uid, TemplateBody=template_contents, Tags=tags, ) elif self.provisioner.get("Type") == "Terraform": response = cloudformation.create_or_update( StackName=friendly_uid, TemplateBody=template_contents, Parameters=[ { "ParameterKey": "Version", "ParameterValue": self.factory_version, "UsePreviousValue": False, }, ], Tags=tags, ) else: raise Exception("did not do anything") self.write_output(response) self.info(f"Finished")