From e9aa8d8dc5c43eac9316ec43745834eefed4e52a Mon Sep 17 00:00:00 2001 From: Matt Weagle Date: Sat, 10 Mar 2018 14:46:50 -0800 Subject: [PATCH] Add a bit more resiliency for unmarshalling Ref: https://s3-us-west-2.amazonaws.com/cloudformation-templates-us-west-2/WordPress_Multi_AZ.template --- func_select.go | 24 ++++++++++++++++++++++-- string_list.go | 7 +++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/func_select.go b/func_select.go index 6708aa2..acbe350 100644 --- a/func_select.go +++ b/func_select.go @@ -1,6 +1,9 @@ package cloudformation -import "encoding/json" +import ( + "encoding/json" + "fmt" +) type selectArg interface{} @@ -47,9 +50,26 @@ func (f *SelectFunc) UnmarshalJSON(data []byte) error { if len(v.FnSelect) != 2 { return &json.UnsupportedValueError{Str: string(data)} } - if err := json.Unmarshal(v.FnSelect[0], &f.Selector); err != nil { + // Possible that the second arg is another template, in which case + // we need to check for that...Example: + /* + "Fn::Select": [ + 0, + { + "Fn::GetAtt": [ + "ApplicationLoadBalancer", + "SecurityGroups" + ] + } + ] + */ + var positionSelector interface{} + if err := json.Unmarshal(v.FnSelect[0], &positionSelector); err != nil { return err } + f.Selector = fmt.Sprintf("%v", positionSelector) + + // What about the second one? if err := json.Unmarshal(v.FnSelect[1], &f.Items); err != nil { return err } diff --git a/string_list.go b/string_list.go index f4c608a..5f05eb1 100644 --- a/string_list.go +++ b/string_list.go @@ -62,6 +62,13 @@ func (x *StringListExpr) UnmarshalJSON(data []byte) error { x.Func = stringFunc return nil } + + // Possible that it's a Fn::GetAtt instance? + getAttrFunc, ok := funcCall.(GetAttFunc) + if ok { + x.Func = StringList(getAttrFunc) + return nil + } return fmt.Errorf("%#v is not a StringListFunc", funcCall) }