def test_importvalue_with_ref_to_string(self): mediary = ConvertToMediary({ 'Fn::Join': [ '_', [ 'one', { 'Fn::ImportValue': { 'Fn::Join': [':', [{ 'Ref': 'Environment' }, 'vpc_id']] } }, 'three' ] ] }).convert() matching_pattern = re.escape( textwrap.dedent(""" !Sub - one_${fnimportvalue_SUBSTITUTIONPLACEHOLDER}_three - fnimportvalue_SUBSTITUTIONPLACEHOLDER: Fn::ImportValue: !Sub "${Environment}:vpc_id" """)).replace("SUBSTITUTIONPLACEHOLDER", "[\d\w]{8}") self.assertRegexpMatches(yaml.dump(mediary), matching_pattern)
def test_complextransform(self): mediary = ConvertToMediary({ 'Resources': { 'MyYamlResource': { 'Type': 'AWS::S3::Bucket', 'Properties': { 'RefProp': { "Fn::Join": [":", ["NameSpace", { "Ref": "MyVariable" }]] }, 'FindInMapProp': { "Fn::FindInMap": ["MapName", "TopLevelKey", "SecondLevelKey"] }, 'GetAttrProp': { "Fn::GetAtt": ["MyResourceName", "Attribute"] }, } } } }).convert() self.assertEqual( "Resources:\n" " MyYamlResource:\n" " Properties:\n" " FindInMapProp: !FindInMap [MapName, TopLevelKey, SecondLevelKey]\n" " GetAttrProp: !GetAtt \"MyResourceName.Attribute\"\n" " RefProp: !Sub \"NameSpace:${MyVariable}\"\n" " Type: AWS::S3::Bucket\n", yaml.dump(mediary))
def test_simple_join(self): mediary = ConvertToMediary({ 'Outputs': { 'MyValue1': 'one', 'AnotherValue': 'two' }, 'AWildCard': 'Surprise!', 'Resources': { 'FirstResource': { 'Properties': { 'Property': 'this' }, 'Type': 'AWS::S3::Bucket', 'DependsOn': 'Other' } }, 'Description': 'This description is in the wrong place' }).convert(order_template=True) self.assertEqual( textwrap.dedent(""" --- Description: This description is in the wrong place Resources: FirstResource: Type: AWS::S3::Bucket Properties: Property: this DependsOn: Other Outputs: AnotherValue: two MyValue1: one AWildCard: Surprise!\n"""), print_to_string(mediary))
def test_not(self): mediary = ConvertToMediary({ 'Hallo': { 'Fn::Not': ['ConditionName'] } }).convert() self.assertEqual(print_to_string(mediary), """--- Hallo: !Not [ConditionName] """)
def test_fragments(self, name, json_source, yaml_target): mediary = ConvertToMediary(json_source).convert(order_template=True) if 'SUBSTITUTIONPLACEHOLDER' in yaml_target: matching_pattern = re.escape(yaml_target).replace( 'SUBSTITUTIONPLACEHOLDER', "[\d\w]{8}") self.assertRegexpMatches( print_to_string(mediary), matching_pattern, "Regexp didn't match in fragment '{name}'".format(name=name)) else: self.assertEqual(yaml_target, print_to_string(mediary))
def test_complextransform_userdata(self): mediary = ConvertToMediary({ 'Resources': { 'MyYamlResource': { 'Type': 'AWS::S3::Bucket', 'Properties': { 'UserData': { "Fn::Base64": { "Fn::Join": [ "", [ "#! /bin/bash -xe\n", "yum install -y aws-cfn-bootstrap\n", "# Install the files and packages from the metadata\n", "/opt/aws/bin/cfn-init -v ", "--stack ", { "Ref": "AWS::Region" }, " --resource ", { "Fn::FindInMap": [ "MapName", "TopLevelKey", "SecondLevelKey" ] }, " --configsets ", { "Fn::Select": [0, ["one", "two"]] }, " ", "--region ", { "Ref": "AWS::Region" } ] ] } } } } } }).convert() matching_pattern = re.escape( textwrap.dedent(""" Resources: MyYamlResource: Properties: UserData: Fn::Base64: !Sub - |- #! /bin/bash -xe yum install -y aws-cfn-bootstrap # Install the files and packages from the metadata /opt/aws/bin/cfn-init -v --stack ${AWS::Region} --resource ${findinmap_SUBSTITUTIONPLACEHOLDER} --configsets ${select_SUBSTITUTIONPLACEHOLDER} --region ${AWS::Region} - findinmap_SUBSTITUTIONPLACEHOLDER: !FindInMap [MapName, TopLevelKey, SecondLevelKey] select_SUBSTITUTIONPLACEHOLDER: !Select [0, [one, two]] Type: AWS::S3::Bucket """)).replace('SUBSTITUTIONPLACEHOLDER', "[\d\w]{8}") self.assertRegexpMatches(yaml.dump(mediary), matching_pattern)
def test_simple_join(self): mediary = ConvertToMediary({ "Fn::Join": [ ":", ["NameSpace", { "Ref": "MyVariable" }, { "Ref": "AWS::Region" }] ] }).convert() self.assertEqual(yaml.dump(mediary), "!Sub \"NameSpace:${MyVariable}:${AWS::Region}\"\n")
def test_nodeconvertor(self): mediary = ConvertToMediary({ 'Resources': { 'MyResources': { 'Type': 'AWS::S3::Bucket', 'Properties': { 'Name': { 'Ref': 'MyBucketName' }, 'Tag': { 'Fn::ImportValue': { 'Fn::Sub': [ '${Environment}:alpha:beta:gamma', { 'Environment': { 'Fn::Select': [0, 'MappingName'] } } ] } }, 'JoinToSub': { 'Fn::Join': [ '-', [ 'one', 'two', { 'Ref': 'MyRef' }, { 'Fn::GetAtt': ['MyRsc', 'Att'] } ] ] } } } } }).convert() self.assertEqual( """Resources: MyResources: Properties: JoinToSub: !Sub "one-two-${MyRef}-${MyRsc.Att}" Name: !Ref "MyBucketName" Tag: Fn::ImportValue: !Sub - ${Environment}:alpha:beta:gamma - Environment: !Select [0, MappingName] Type: AWS::S3::Bucket """, yaml.dump(mediary))
def convert(args=sys.argv[1:]): if args: with open(args[0]) as fp: json_document = fp.read() else: json_document = [] for line in sys.stdin: json_document.append(line) json_document = "\n".join(json_document) document = json.loads(json_document) print print_to_string( ConvertToMediary(document).convert(order_template=True))
def test_nested_complex_join(self): mediary = ConvertToMediary({ "Fn::Join": [ ":", [ "NameSpace", { "Ref": "MyVariable" }, { "Fn::FindInMap": ["one", "two", "three"] } ] ] }).convert() matching_pattern = re.escape( textwrap.dedent(""" !Sub - NameSpace:${MyVariable}:${findinmap_SUBSTITUTIONPLACEHOLDER} - findinmap_SUBSTITUTIONPLACEHOLDER: !FindInMap [one, two, three] """)).replace("SUBSTITUTIONPLACEHOLDER", "[\d\w]{8}") self.assertRegexpMatches(yaml.dump(mediary), matching_pattern)
def test_to_yaml_ref(self): mediary = ConvertToMediary({ 'Resources': { 'MyYamlResource': { 'Type': 'AWS::S3::Bucket', 'Properties': { 'Name': { 'Ref': 'MyVariable' } } } } }).convert() self.assertEquals( ("Resources:\n" " MyYamlResource:\n" " Properties:\n" " Name: !Ref \"MyVariable\"\n" " Type: AWS::S3::Bucket\n"), yaml.dump(mediary) )
def test_to_yaml_join_to_sub(self): median = ConvertToMediary({ 'Resources': { 'MyYamlResource': { 'Type': 'AWS::S3::Bucket', 'Properties': { 'Name': { "Fn::Join": [":", ["NameSpace", {"Ref": "MyVariable"}]] } } } } }).convert() self.assertEquals( ("Resources:\n" " MyYamlResource:\n" " Properties:\n" " Name: !Sub \"NameSpace:${MyVariable}\"\n" " Type: AWS::S3::Bucket\n"), yaml.dump(median) )
def test_complexjoin(self): mediary = ConvertToMediary({ 'Resources': { 'MyResources': { 'Type': 'AWS::EC2::Instance', 'Properties': { 'UserData': { "Fn::Base64": { "Fn::Join": [ "", [ u"#!/bin/bash\n", u"# bootstrap/default-cloud-init.sh\n", u"sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz;\n", u"/usr/local/bin/cfn-signal -e $? --stack \"", { "Ref": "AWS::StackName" }, "\" --resource ", { "Fn::ImportValue": "my_external_resource_name" }, " --region \"", { "Ref": "AWS::Region" }, "\";\n", "\n", "yum -y remove sendmail\n", "yum -v clean all && yum -v makecache\n", "yum -v install -y jq yum-cron\n", "yum -y update\n", "\n", "\n", "aws s3 cp s3://myscripts/bootstrap.sh /root/bootstrap.sh\n", "chmod a+x /root/bootstrap.sh\n", "echo \"/root/bootstrap.sh\" >> /etc/rc.local\n", "reboot\n", "\n" ] ] } } } } } }).convert() matching_pattern = re.escape( textwrap.dedent(""" Resources: MyResources: Properties: UserData: Fn::Base64: !Sub - |+ #!/bin/bash # bootstrap/default-cloud-init.sh sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz; /usr/local/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource ${importvalue_SUBSTITUTIONPLACEHOLDER} --region "${AWS::Region}"; yum -y remove sendmail yum -v clean all && yum -v makecache yum -v install -y jq yum-cron yum -y update aws s3 cp s3://myscripts/bootstrap.sh /root/bootstrap.sh chmod a+x /root/bootstrap.sh echo "/root/bootstrap.sh" >> /etc/rc.local reboot - importvalue_SUBSTITUTIONPLACEHOLDER: !ImportValue "my_external_resource_name" Type: AWS::EC2::Instance """)).replace("SUBSTITUTIONPLACEHOLDER", "[\d\w]{8}") self.assertRegexpMatches(yaml.dump(mediary), matching_pattern)
def test_complexjoin(self): mediary = ConvertToMediary({ 'Resources': { 'MyResources': { 'Type': 'AWS::EC2::Instance', 'Properties': { 'UserData': { "Fn::Base64": { "Fn::Join": [ "", [ "#!/bin/bash\n", "# bootstrap/default-cloud-init.sh\n", "sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz;\n", "/usr/local/bin/cfn-signal -e $? --stack \"", { "Ref": "AWS::StackName" }, "\" --resource ", { "Fn::ImportValue": "my_external_resource_name" }, " --region \"", { "Ref": "AWS::Region" }, "\";\n", "\n", "yum -y remove sendmail\n", "yum -v clean all && yum -v makecache\n", "yum -v install -y jq yum-cron\n", "yum -y update\n", "\n", "\n", "aws s3 cp s3://myscripts/bootstrap.sh /root/bootstrap.sh\n", "chmod a+x /root/bootstrap.sh\n", "echo \"/root/bootstrap.sh\" >> /etc/rc.local\n", "reboot\n", "\n" ] ] } } } } } }).convert() matching_pattern = re.escape( textwrap.dedent(""" Resources: MyResources: Properties: UserData: Fn::Base64: !Sub - |+ #!/bin/bash # bootstrap/default-cloud-init.sh sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz; /usr/local/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource ${importvalue_SUBSTITUTIONPLACEHOLDER} --region "${AWS::Region}"; yum -y remove sendmail yum -v clean all && yum -v makecache yum -v install -y jq yum-cron yum -y update aws s3 cp s3://myscripts/bootstrap.sh /root/bootstrap.sh chmod a+x /root/bootstrap.sh echo "/root/bootstrap.sh" >> /etc/rc.local reboot - importvalue_SUBSTITUTIONPLACEHOLDER: !ImportValue "my_external_resource_name" Type: AWS::EC2::Instance """)).replace("SUBSTITUTIONPLACEHOLDER", "[\d\w]{8}") self.assertRegexpMatches(yaml.dump(mediary), matching_pattern) def test_longjoin(self): mediary = ConvertToMediary({ 'Resources': { 'MyResources': { 'Type': 'AWS::EC2::Instance', 'Properties': { 'UserData': { "Fn::Base64": { "Fn::Join": [ "", [ "#!/bin/bash\n", "# bootstrap/default-cloud-init.sh\n", "sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz;\n", "/usr/local/bin/cfn-signal -e $? --stack \"", { "Ref": "AWS::StackName" }, "\" --resource ASG --region \"", { "Ref": "AWS::Region" }, "\";\n", "\n", "yum -y remove sendmail\n", "yum -v clean all && yum -v makecache\n", "yum -v install -y jq yum-cron\n", "yum -y update\n", "\n", "\n", "aws s3 cp s3://pure360-ansible-prod/sysops_ansible_prod.sh /root/sysops_ansible.sh\n", "chmod a+x /root/sysops_ansible.sh\n", "echo \"/root/sysops_ansible.sh\" >> /etc/rc.local\n", "reboot\n", "\n" ] ] } } } } } }).convert() self.assertEqual( """Resources: MyResources: Properties: UserData: Fn::Base64: !Sub |+ #!/bin/bash # bootstrap/default-cloud-init.sh sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz; /usr/local/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource ASG --region "${AWS::Region}"; yum -y remove sendmail yum -v clean all && yum -v makecache yum -v install -y jq yum-cron yum -y update aws s3 cp s3://myscripts/bootstrap.sh /root/bootstrap.sh chmod a+x /root/bootstrap.sh echo "/root/bootstrap.sh" >> /etc/rc.local reboot Type: AWS::EC2::Instance """, yaml.dump(mediary)) def test_nodeconvertor(self): mediary = ConvertToMediary({ 'Resources': { 'MyResources': { 'Type': 'AWS::S3::Bucket', 'Properties': { 'Name': { 'Ref': 'MyBucketName' }, 'Tag': { 'Fn::ImportValue': { 'Fn::Sub': [ '${Environment}:alpha:beta:gamma', { 'Environment': { 'Fn::Select': [0, 'MappingName'] } } ] } }, 'JoinToSub': { 'Fn::Join': [ '-', [ 'one', 'two', { 'Ref': 'MyRef' }, { 'Fn::GetAtt': ['MyRsc', 'Att'] } ] ] } } } } }).convert() self.assertEqual( """Resources: MyResources: Properties: JoinToSub: !Sub "one-two-${MyRef}-${MyRsc.Att}" Name: !Ref "MyBucketName" Tag: Fn::ImportValue: !Sub - ${Environment}:alpha:beta:gamma - Environment: !Select [0, MappingName] Type: AWS::S3::Bucket """, yaml.dump(mediary))
def test_longjoin(self): mediary = ConvertToMediary({ 'Resources': { 'MyResources': { 'Type': 'AWS::EC2::Instance', 'Properties': { 'UserData': { "Fn::Base64": { "Fn::Join": [ "", [ "#!/bin/bash\n", "# bootstrap/default-cloud-init.sh\n", "sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz;\n", "/usr/local/bin/cfn-signal -e $? --stack \"", { "Ref": "AWS::StackName" }, "\" --resource ASG --region \"", { "Ref": "AWS::Region" }, "\";\n", "\n", "yum -y remove sendmail\n", "yum -v clean all && yum -v makecache\n", "yum -v install -y jq yum-cron\n", "yum -y update\n", "\n", "\n", "aws s3 cp s3://pure360-ansible-prod/sysops_ansible_prod.sh /root/sysops_ansible.sh\n", "chmod a+x /root/sysops_ansible.sh\n", "echo \"/root/sysops_ansible.sh\" >> /etc/rc.local\n", "reboot\n", "\n" ] ] } } } } } }).convert() self.assertEqual( """Resources: MyResources: Properties: UserData: Fn::Base64: !Sub |+ #!/bin/bash # bootstrap/default-cloud-init.sh sudo easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz; /usr/local/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource ASG --region "${AWS::Region}"; yum -y remove sendmail yum -v clean all && yum -v makecache yum -v install -y jq yum-cron yum -y update aws s3 cp s3://myscripts/bootstrap.sh /root/bootstrap.sh chmod a+x /root/bootstrap.sh echo "/root/bootstrap.sh" >> /etc/rc.local reboot Type: AWS::EC2::Instance """, yaml.dump(mediary))
def test_simple_ref(self): mediary = ConvertToMediary({"Ref": "MyVariable"}).convert() self.assertEqual(yaml.dump(mediary), "!Ref \"MyVariable\"\n")