Skip to content

Commit

Permalink
feat(intrinsics): add support for base64 encoded string in instrinsic…
Browse files Browse the repository at this point in the history
… if function (awslabs#414)

Fix awslabs#412

Co-authored-by: grzegorz_skupiewski <grzegorz.skupiewski@smartcommerce.net>
  • Loading branch information
Nitjsefni7 and grzegorz_skupiewski committed Nov 22, 2021
1 parent c5ccd62 commit 652501b
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions cloudformation/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,44 @@ func FindInMap(mapName, topLevelKey, secondLevelKey interface{}) string {

// If returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false. Currently, AWS CloudFormation supports the Fn::If intrinsic function in the metadata attribute, update policy attribute, and property values in the Resources section and Outputs sections of a template. You can use the AWS::NoValue pseudo parameter as a return value to remove the corresponding property.
func If(value, ifEqual interface{}, ifNotEqual interface{}) string {
var equal string

equal, err := json.Marshal(ifEqual)
if err != nil {
panic(err)
switch v := ifEqual.(type) {
case map[string]string, []string, string:
out, err := json.Marshal(ifEqual)
if err != nil {
panic(err)
}
equal = string(out)
default:
fmt.Printf("Unsupported type for ifEqual: %T\n", v)
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %q, %q ] }`, value, ifEqual, ifNotEqual))
}

var notEqual string
switch v := ifNotEqual.(type) {
case map[string]string, []string, string:
out, err := json.Marshal(ifNotEqual)
if err != nil {
panic(err)
}
notEqual = string(out)
default:
fmt.Printf("Unsupported type for ifNotEqual: %T\n", v)
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %q, %q ] }`, value, ifEqual, ifNotEqual))
}

notEqual, err := json.Marshal(ifNotEqual)
if err != nil {
panic(err)
if isBase64(equal) {
if isBase64(notEqual) {
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %q, %q ] }`, value, equal, notEqual))
}
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %q, %v ] }`, value, equal, notEqual))
}
if isBase64(notEqual) {
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %v, %q ] }`, value, equal, notEqual))
}

return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %v, %v ] }`, value, string(equal), string(notEqual)))
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %v, %v ] }`, value, equal, notEqual))
}

// (str, []str) -> str
Expand Down Expand Up @@ -270,3 +296,8 @@ func interfaceAtostrA(values []interface{}) []string {
}
return converted
}

func isBase64(s string) bool {
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}

0 comments on commit 652501b

Please sign in to comment.