Skip to content

Commit

Permalink
[Fix] Properly Encode !GetAtt yaml tag (awslabs#256)
Browse files Browse the repository at this point in the history
fix(parser): Properly Encode !GetAtt yaml tag
  • Loading branch information
Graham Jenson committed Jan 29, 2020
1 parent 54fe964 commit a7d5c92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions cloudformation/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ func strWrap(fn func(interface{}) string) intrinsics.IntrinsicHandler {
}
}

// Splits the string base on a . delimiter
func strSplit2Wrap(fn func(string, string) string) intrinsics.IntrinsicHandler {
delim := "."
return func(name string, input interface{}, template interface{}) interface{} {
if str, ok := input.(string); ok {
arr := strings.SplitN(str, delim, 2)
if len(arr) != 2 {
return nil
}
return fn(arr[0], arr[1])
}
return nil
}
}

func str2Wrap(fn func(interface{}, interface{}) string) intrinsics.IntrinsicHandler {
return func(name string, input interface{}, template interface{}) interface{} {
// Check that the input is an array
Expand Down Expand Up @@ -75,7 +90,7 @@ var EncoderIntrinsics = map[string]intrinsics.IntrinsicHandler{
"Fn::Not": strAWrap(Not),
"Fn::Or": strAWrap(Or),
"Fn::FindInMap": str3Wrap(FindInMap),
"Fn::GetAtt": str2Wrap(GetAtt),
"Fn::GetAtt": strSplit2Wrap(GetAtt),
"Fn::GetAZs": strWrap(GetAZs),
"Fn::ImportValue": strWrap(ImportValue),
"Fn::Join": str2AWrap(Join),
Expand Down Expand Up @@ -116,7 +131,7 @@ func Sub(value interface{}) string {
// (str, str) -> str

// GetAtt returns the value of an attribute from a resource in the template.
func GetAtt(logicalName interface{}, attribute interface{}) string {
func GetAtt(logicalName string, attribute string) string {
return encode(fmt.Sprintf(`{ "Fn::GetAtt" : [ "%v", "%v" ] }`, logicalName, attribute))
}

Expand Down
2 changes: 1 addition & 1 deletion cloudformation/intrinsics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = Describe("Goformation", func() {
},
{
Name: "GetAtt",
Input: `Description: !GetAtt [object, property]`,
Input: `Description: !GetAtt object.property`,
Expected: `{"Description":{"Fn::GetAtt":["object","property"]}}`,
},
{
Expand Down

0 comments on commit a7d5c92

Please sign in to comment.