@@ -85,7 +85,7 @@ func getSliceItems(context tfContext) ([]interface{}, error) {
85
85
for _ , pathRaw := range paths {
86
86
path := pathRaw
87
87
if strings .Contains (pathRaw , "${" ) {
88
- path , err = resolvePlaceholders (path , context .ParentContext )
88
+ path , err = resolvePlaceholders (pathRaw , context .ParentContext )
89
89
if err != nil {
90
90
return nil , err
91
91
}
@@ -98,7 +98,7 @@ func getSliceItems(context tfContext) ([]interface{}, error) {
98
98
if len (jsonResults ) == 0 && TfPlan != nil {
99
99
jsonResults , err = utils .GetJSON (path , * TfPlan )
100
100
if err != nil {
101
- return nil , errors .Wrapf (err , "Cannot get item: %v" , path )
101
+ return nil , errors .Wrapf (err , "Cannot get item in full plan : %v" , path )
102
102
}
103
103
}
104
104
for _ , jsonResultsI := range jsonResults {
@@ -108,7 +108,9 @@ func getSliceItems(context tfContext) ([]interface{}, error) {
108
108
if err != nil {
109
109
return nil , err
110
110
}
111
- results = append (results , result )
111
+ if result != nil {
112
+ results = append (results , result )
113
+ }
112
114
case []interface {}:
113
115
for _ , jsonResultI := range jsonResults {
114
116
jsonResultI , ok := jsonResultI .(map [string ]interface {})
@@ -119,7 +121,9 @@ func getSliceItems(context tfContext) ([]interface{}, error) {
119
121
if err != nil {
120
122
return nil , err
121
123
}
122
- results = append (results , result )
124
+ if result != nil {
125
+ results = append (results , result )
126
+ }
123
127
}
124
128
default :
125
129
return nil , errors .Errorf ("Not an map or an array of maps: %T" , jsonResultsI )
@@ -146,7 +150,9 @@ func getItem(context tfContext, itemMappingProperties *ResourceMapping, jsonResu
146
150
if err != nil {
147
151
return nil , err
148
152
}
149
- result [key ] = property
153
+ if property != nil {
154
+ result [key ] = property
155
+ }
150
156
}
151
157
return result , nil
152
158
}
@@ -198,7 +204,7 @@ func getValue(key string, context *tfContext) (*valueWithUnit, error) {
198
204
for _ , propertyMapping := range propertiesMappings {
199
205
paths , err := readPaths (propertyMapping .Paths )
200
206
if err != nil {
201
- return nil , err
207
+ return nil , errors . Wrapf ( err , "Cannot get paths for %v" , context . ResourceAddress )
202
208
}
203
209
unit := propertyMapping .Unit
204
210
@@ -208,25 +214,27 @@ func getValue(key string, context *tfContext) (*valueWithUnit, error) {
208
214
}
209
215
path := pathRaw
210
216
if strings .Contains (pathRaw , "${" ) {
217
+ fmt .Println ("pathRaw: " , pathRaw )
211
218
path , err = resolvePlaceholders (path , context )
212
219
if err != nil {
213
- return nil , err
220
+ return nil , errors . Wrapf ( err , "Cannot resolve placeholders for %v" , path )
214
221
}
222
+ fmt .Println ("path: " , path )
215
223
}
216
224
valueFounds , err := utils .GetJSON (path , context .Resource )
217
225
if err != nil {
218
- return nil , err
226
+ return nil , errors . Wrapf ( err , "Cannot get value for %v" , path )
219
227
}
220
228
if len (valueFounds ) == 0 && TfPlan != nil {
221
229
// Try to resolve it against the whole plan
222
230
valueFounds , err = utils .GetJSON (path , * TfPlan )
223
231
if err != nil {
224
- return nil , err
232
+ return nil , errors . Wrapf ( err , "Cannot get value in the whole plan for %v" , path )
225
233
}
226
234
}
227
235
if len (valueFounds ) > 0 {
228
236
if len (valueFounds ) > 1 {
229
- return nil , fmt .Errorf ("Found more than one value for property %v of resource type %v" , key , context .ResourceAddress )
237
+ return nil , errors .Errorf ("Found more than one value for property %v of resource type %v" , key , context .ResourceAddress )
230
238
}
231
239
if valueFounds [0 ] == nil {
232
240
continue
@@ -240,14 +248,14 @@ func getValue(key string, context *tfContext) (*valueWithUnit, error) {
240
248
if ok {
241
249
valueFound , err = applyRegex (valueFoundStr , & propertyMapping , context )
242
250
if err != nil {
243
- return nil , err
251
+ return nil , errors . Wrapf ( err , "Cannot apply regex for %v" , valueFoundStr )
244
252
}
245
253
}
246
254
valueFoundStr , ok = valueFound .(string )
247
255
if ok {
248
256
valueFound , err = applyReference (valueFoundStr , & propertyMapping , context )
249
257
if err != nil {
250
- return nil , err
258
+ return nil , errors . Wrapf ( err , "Cannot apply reference for %v" , valueFoundStr )
251
259
}
252
260
}
253
261
}
@@ -257,7 +265,7 @@ func getValue(key string, context *tfContext) (*valueWithUnit, error) {
257
265
if ok {
258
266
valueFound , err = getValueOfExpression (valueFoundMap , context )
259
267
if err != nil {
260
- return nil , err
268
+ return nil , errors . Wrapf ( err , "Cannot get value of expression for %v" , valueFoundMap )
261
269
}
262
270
}
263
271
@@ -272,7 +280,7 @@ func getValue(key string, context *tfContext) (*valueWithUnit, error) {
272
280
if valueFound == nil {
273
281
defaultValue , err := getDefaultValue (key , context )
274
282
if err != nil {
275
- return nil , err
283
+ return nil , errors . Wrapf ( err , "Cannot get default value for %v" , key )
276
284
}
277
285
278
286
if defaultValue != nil {
@@ -303,7 +311,13 @@ func resolvePlaceholders(input string, context *tfContext) (string, error) {
303
311
if err != nil {
304
312
return input , err
305
313
}
306
- resolvedExpressions [placeholder ] = resolved
314
+ if resolved == nil {
315
+ // It's ok to not find a value for a placeholder
316
+ resolvedExpressions [placeholder ] = ".not_found"
317
+ } else {
318
+ resolvedExpressions [placeholder ] = * resolved
319
+ }
320
+
307
321
}
308
322
309
323
// Replace placeholders in the input string with resolved expressions
@@ -317,32 +331,37 @@ func resolvePlaceholders(input string, context *tfContext) (string, error) {
317
331
return resolvedString , nil
318
332
}
319
333
320
- func resolvePlaceholder (expression string , context * tfContext ) (string , error ) {
321
- result := ""
334
+ func resolvePlaceholder (expression string , context * tfContext ) (* string , error ) {
322
335
if strings .HasPrefix (expression , "this." ) {
323
336
thisProperty := strings .TrimPrefix (expression , "this" )
324
337
resource := context .Resource
325
338
value , err := utils .GetJSON (thisProperty , resource )
326
339
if err != nil {
327
- return "" , errors .Wrapf (err , "Cannot get value for variable %s" , expression )
340
+ return nil , errors .Wrapf (err , "Cannot get value for variable %s" , expression )
328
341
}
329
- if value == nil {
330
- return "" , errors .Errorf ("No value found for variable %s" , expression )
342
+ if len (value ) == 0 {
343
+ return nil , nil
344
+ }
345
+ if value [0 ] == nil {
346
+ return nil , nil
331
347
}
332
- return fmt .Sprintf ("%v" , value [0 ]), err
348
+ valueStr := fmt .Sprintf ("%v" , value [0 ])
349
+ return & valueStr , err
333
350
} else if strings .HasPrefix (expression , "config." ) {
334
351
configProperty := strings .TrimPrefix (expression , "config." )
335
352
value := viper .GetFloat64 (configProperty )
336
- return fmt .Sprintf ("%v" , value ), nil
353
+ valueStr := fmt .Sprintf ("%v" , value )
354
+ return & valueStr , nil
337
355
}
338
356
variable , err := getVariable (expression , context )
339
357
if err != nil {
340
- return "" , err
358
+ return nil , err
341
359
}
342
360
if variable != nil {
343
- result = fmt .Sprintf ("%v" , variable )
361
+ valueStr := fmt .Sprintf ("%v" , variable )
362
+ return & valueStr , nil
344
363
}
345
- return result , nil
364
+ return nil , nil
346
365
}
347
366
348
367
func getDefaultValue (key string , context * tfContext ) (* valueWithUnit , error ) {
@@ -400,10 +419,10 @@ func getVariable(name string, context *tfContext) (interface{}, error) {
400
419
}
401
420
value , err := getValue (name , & variableContext )
402
421
if err != nil {
403
- return nil , err
422
+ return nil , errors . Wrapf ( err , "Cannot get variable %v" , name )
404
423
}
405
424
if value == nil {
406
- return nil , fmt . Errorf ( "Cannot get variable : %v" , name )
425
+ return nil , nil
407
426
}
408
427
return value .Value , nil
409
428
0 commit comments