55 "slices"
66
77 "github.com/aws-cloudformation/rain/cft"
8- "github.com/aws-cloudformation/rain/internal/config"
98 "github.com/aws-cloudformation/rain/internal/node"
109 "github.com/aws-cloudformation/rain/internal/s11n"
1110 "gopkg.in/yaml.v3"
@@ -102,21 +101,15 @@ func (module *Module) ProcessConditions() error {
102101 condName := conditionNode .Value
103102 condResult , ok := module .ConditionValues [condName ]
104103 if ! ok {
105- msg := "%s Condition not found in %s"
106-
107104 // Is this a condition that we could not
108105 // fully resolve, or a condition that doesn't exist?
109106
110- config .Debugf (msg , condName , module .Config .Source )
107+ if slices .Contains (unResolved , condName ) {
108+ // Prepend the module name to the condition
109+ newName := module .Config .Name + conditionNode .Value
110+ conditionNode .Value = newName
111111
112- if ! slices .Contains (unResolved , condName ) {
113- // If it's not in that list, it's likely a mistake
114- return fmt .Errorf (msg , condName , module .Config .Source )
115112 }
116-
117- // Prepend the module name to the condition
118- newName := module .Config .Name + conditionNode .Value
119- conditionNode .Value = newName
120113 } else {
121114 if ! condResult {
122115 itemsToRemove = append (itemsToRemove , itemName )
@@ -144,23 +137,27 @@ func (module *Module) ProcessConditions() error {
144137 }
145138
146139 if len (unResolved ) == 0 {
147- config .Debugf ("No unresolved conditions in %s" , module .Config .Name )
148140 node .RemoveFromMap (module .Node , string (cft .Conditions ))
149141 } else {
150142 // Only remove those that we fully resolved.
151143 // Emit the rest into the parent template.
152144 for _ , name := range resolved {
153145 node .RemoveFromMap (module .ConditionsNode , name )
154146 }
147+
148+ // Ensure that the parent template has a Conditions section
155149 t := module .ParentTemplate
156150 tc , err := t .GetSection (cft .Conditions )
157151 if err != nil {
158152 tc , _ = t .AddMapSection (cft .Conditions )
159153 }
154+
155+ // Record the existing conditions in the parent
160156 tcNames := make (map [string ]* yaml.Node )
161157 for i := 0 ; i < len (tc .Content ); i += 2 {
162158 tcNames [tc .Content [i ].Value ] = tc .Content [i + 1 ]
163159 }
160+
164161 for i , condNode := range module .ConditionsNode .Content {
165162 if i % 2 == 0 {
166163 // Prepend the module name to the condition
@@ -172,12 +169,12 @@ func (module *Module) ProcessConditions() error {
172169 "%s that conflicts with a Condition in the parent"
173170 return fmt .Errorf (msg , module .Config .Source , name )
174171 }
172+ } else {
173+ nameNode := node .MakeScalar (name )
174+ node .Append (tc , nameNode )
175+ cloned := node .Clone (val )
176+ node .Append (tc , cloned )
175177 }
176- nameNode := node .MakeScalar (name )
177- node .Append (tc , nameNode )
178- cloned := node .Clone (module .ConditionsNode .Content [i + 1 ])
179- node .Append (tc , cloned )
180- break
181178 }
182179 }
183180
@@ -190,9 +187,6 @@ func (module *Module) ProcessConditions() error {
190187func (module * Module ) EvalCond (
191188 name string , val * yaml.Node ) (EvalResult , error ) {
192189
193- config .Debugf ("module.EvalCond %s %s:\n %s" ,
194- module .Config .Name , name , node .YamlStr (val ))
195-
196190 // Handle mapping node (most condition functions)
197191 if val .Kind == yaml .MappingNode && len (val .Content ) >= 2 {
198192 key := val .Content [0 ].Value
@@ -343,10 +337,6 @@ func (module *Module) EvalEquals(n *yaml.Node) (EvalResult, error) {
343337 }
344338 }
345339
346- // This looks like an unresolved reference
347- config .Debugf ("EvalEquals unresolved val1: %s, val2: %s" ,
348- node .YamlStr (val1 ), node .YamlStr (val2 ))
349-
350340 return UnResolved , nil
351341}
352342
0 commit comments