-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
config_as_cty.go
526 lines (459 loc) · 15.7 KB
/
config_as_cty.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package config
import (
"encoding/json"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terragrunt/remote"
)
// Serialize TerragruntConfig struct to a cty Value that can be used to reference the attributes in other config. Note
// that we can't straight up convert the struct using cty tags due to differences in the desired representation.
// Specifically, we want to reference blocks by named attributes, but blocks are rendered to lists in the
// TerragruntConfig struct, so we need to do some massaging of the data to convert the list of blocks in to a map going
// from the block name label to the block value.
func TerragruntConfigAsCty(config *TerragruntConfig) (cty.Value, error) {
output := map[string]cty.Value{}
// Convert attributes that are primitive types
output[MetadataTerraformBinary] = gostringToCty(config.TerraformBinary)
output[MetadataTerraformVersionConstraint] = gostringToCty(config.TerraformVersionConstraint)
output[MetadataTerragruntVersionConstraint] = gostringToCty(config.TerragruntVersionConstraint)
output[MetadataDownloadDir] = gostringToCty(config.DownloadDir)
output[MetadataIamRole] = gostringToCty(config.IamRole)
output[MetadataSkip] = goboolToCty(config.Skip)
output[MetadataIamAssumeRoleSessionName] = gostringToCty(config.IamAssumeRoleSessionName)
catalogConfigCty, err := catalogConfigAsCty(config.Catalog)
if err != nil {
return cty.NilVal, err
}
if catalogConfigCty != cty.NilVal {
output[MetadataCatalog] = catalogConfigCty
}
terraformConfigCty, err := terraformConfigAsCty(config.Terraform)
if err != nil {
return cty.NilVal, err
}
if terraformConfigCty != cty.NilVal {
output[MetadataTerraform] = terraformConfigCty
}
remoteStateCty, err := remoteStateAsCty(config.RemoteState)
if err != nil {
return cty.NilVal, err
}
if remoteStateCty != cty.NilVal {
output[MetadataRemoteState] = remoteStateCty
}
dependenciesCty, err := goTypeToCty(config.Dependencies)
if err != nil {
return cty.NilVal, err
}
if dependenciesCty != cty.NilVal {
output[MetadataDependencies] = dependenciesCty
}
if config.PreventDestroy != nil {
output[MetadataPreventDestroy] = goboolToCty(*config.PreventDestroy)
}
dependencyCty, err := dependencyBlocksAsCty(config.TerragruntDependencies)
if err != nil {
return cty.NilVal, err
}
if dependencyCty != cty.NilVal {
output[MetadataDependency] = dependencyCty
}
generateCty, err := goTypeToCty(config.GenerateConfigs)
if err != nil {
return cty.NilVal, err
}
if generateCty != cty.NilVal {
output[MetadataGenerateConfigs] = generateCty
}
retryableCty, err := goTypeToCty(config.RetryableErrors)
if err != nil {
return cty.NilVal, err
}
if retryableCty != cty.NilVal {
output[MetadataRetryableErrors] = retryableCty
}
iamAssumeRoleDurationCty, err := goTypeToCty(config.IamAssumeRoleDuration)
if err != nil {
return cty.NilVal, err
}
if iamAssumeRoleDurationCty != cty.NilVal {
output[MetadataIamAssumeRoleDuration] = iamAssumeRoleDurationCty
}
retryMaxAttemptsCty, err := goTypeToCty(config.RetryMaxAttempts)
if err != nil {
return cty.NilVal, err
}
if retryMaxAttemptsCty != cty.NilVal {
output[MetadataRetryMaxAttempts] = retryMaxAttemptsCty
}
retrySleepIntervalSecCty, err := goTypeToCty(config.RetrySleepIntervalSec)
if err != nil {
return cty.NilVal, err
}
if retrySleepIntervalSecCty != cty.NilVal {
output[MetadataRetrySleepIntervalSec] = retrySleepIntervalSecCty
}
inputsCty, err := convertToCtyWithJson(config.Inputs)
if err != nil {
return cty.NilVal, err
}
if inputsCty != cty.NilVal {
output[MetadataInputs] = inputsCty
}
localsCty, err := convertToCtyWithJson(config.Locals)
if err != nil {
return cty.NilVal, err
}
if localsCty != cty.NilVal {
output[MetadataLocals] = localsCty
}
if len(config.DependentModulesPath) > 0 {
dependentModulesCty, err := convertToCtyWithJson(config.DependentModulesPath)
if err != nil {
return cty.NilVal, err
}
if dependentModulesCty != cty.NilVal {
output[MetadataDependentModules] = dependentModulesCty
}
}
return convertValuesMapToCtyVal(output)
}
func TerragruntConfigAsCtyWithMetadata(config *TerragruntConfig) (cty.Value, error) {
output := map[string]cty.Value{}
// Convert attributes that are primitive types
if err := wrapWithMetadata(config, config.TerraformBinary, MetadataTerraformBinary, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.TerraformVersionConstraint, MetadataTerraformVersionConstraint, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.TerragruntVersionConstraint, MetadataTerragruntVersionConstraint, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.DownloadDir, MetadataDownloadDir, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.IamRole, MetadataIamRole, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.Skip, MetadataSkip, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.IamAssumeRoleSessionName, MetadataIamAssumeRoleSessionName, &output); err != nil {
return cty.NilVal, err
}
if config.PreventDestroy != nil {
if err := wrapWithMetadata(config, *config.PreventDestroy, MetadataPreventDestroy, &output); err != nil {
return cty.NilVal, err
}
}
if err := wrapWithMetadata(config, config.RetryableErrors, MetadataRetryableErrors, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.IamAssumeRoleDuration, MetadataIamAssumeRoleDuration, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.RetryMaxAttempts, MetadataRetryMaxAttempts, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.RetrySleepIntervalSec, MetadataRetrySleepIntervalSec, &output); err != nil {
return cty.NilVal, err
}
if err := wrapWithMetadata(config, config.DependentModulesPath, MetadataDependentModules, &output); err != nil {
return cty.NilVal, err
}
// Terraform
terraformConfigCty, err := terraformConfigAsCty(config.Terraform)
if err != nil {
return cty.NilVal, err
}
if terraformConfigCty != cty.NilVal {
if err := wrapWithMetadata(config, terraformConfigCty, MetadataTerraform, &output); err != nil {
return cty.NilVal, err
}
}
// Remote state
remoteStateCty, err := remoteStateAsCty(config.RemoteState)
if err != nil {
return cty.NilVal, err
}
if remoteStateCty != cty.NilVal {
if err := wrapWithMetadata(config, remoteStateCty, MetadataRemoteState, &output); err != nil {
return cty.NilVal, err
}
}
if err := wrapCtyMapWithMetadata(config, &config.Inputs, MetadataInputs, &output); err != nil {
return cty.NilVal, err
}
if err := wrapCtyMapWithMetadata(config, &config.Locals, MetadataLocals, &output); err != nil {
return cty.NilVal, err
}
// remder dependencies as list of maps with "value" and "metadata"
if config.Dependencies != nil {
var dependencyWithMetadata = make([]ValueWithMetadata, 0, len(config.Dependencies.Paths))
for _, dependency := range config.Dependencies.Paths {
var content = ValueWithMetadata{}
content.Value = gostringToCty(dependency)
metadata, found := config.GetMapFieldMetadata(MetadataDependencies, dependency)
if found {
content.Metadata = metadata
}
dependencyWithMetadata = append(dependencyWithMetadata, content)
}
dependenciesCty, err := goTypeToCty(dependencyWithMetadata)
if err != nil {
return cty.NilVal, err
}
output[MetadataDependencies] = dependenciesCty
}
if config.TerragruntDependencies != nil {
var dependenciesMap = map[string]cty.Value{}
for _, block := range config.TerragruntDependencies {
ctyValue, err := goTypeToCty(block)
if err != nil {
continue
}
if ctyValue == cty.NilVal {
continue
}
var content = ValueWithMetadata{}
content.Value = ctyValue
metadata, found := config.GetMapFieldMetadata(MetadataDependency, block.Name)
if found {
content.Metadata = metadata
}
value, err := goTypeToCty(content)
if err != nil {
continue
}
dependenciesMap[block.Name] = value
}
if len(dependenciesMap) > 0 {
dependenciesCty, err := convertValuesMapToCtyVal(dependenciesMap)
if err != nil {
return cty.NilVal, err
}
output[MetadataDependency] = dependenciesCty
}
}
if config.GenerateConfigs != nil {
var generateConfigsWithMetadata = map[string]cty.Value{}
for key, value := range config.GenerateConfigs {
ctyValue, err := goTypeToCty(value)
if err != nil {
continue
}
if ctyValue == cty.NilVal {
continue
}
var content = ValueWithMetadata{}
content.Value = ctyValue
metadata, found := config.GetMapFieldMetadata(MetadataGenerateConfigs, key)
if found {
content.Metadata = metadata
}
v, err := goTypeToCty(content)
if err != nil {
continue
}
generateConfigsWithMetadata[key] = v
}
if len(generateConfigsWithMetadata) > 0 {
dependenciesCty, err := convertValuesMapToCtyVal(generateConfigsWithMetadata)
if err != nil {
return cty.NilVal, err
}
output[MetadataGenerateConfigs] = dependenciesCty
}
}
return convertValuesMapToCtyVal(output)
}
func wrapCtyMapWithMetadata(config *TerragruntConfig, data *map[string]interface{}, fieldType string, output *map[string]cty.Value) error {
var valueWithMetadata = map[string]cty.Value{}
for key, value := range *data {
var content = ValueWithMetadata{}
ctyValue, err := convertToCtyWithJson(value)
if err != nil {
return err
}
content.Value = ctyValue
metadata, found := config.GetMapFieldMetadata(fieldType, key)
if found {
content.Metadata = metadata
}
v, err := goTypeToCty(content)
if err != nil {
continue
}
valueWithMetadata[key] = v
}
if len(valueWithMetadata) > 0 {
localsCty, err := convertValuesMapToCtyVal(valueWithMetadata)
if err != nil {
return err
}
(*output)[fieldType] = localsCty
}
return nil
}
func wrapWithMetadata(config *TerragruntConfig, value interface{}, metadataName string, output *map[string]cty.Value) error {
if value == nil {
return nil
}
var valueWithMetadata = ValueWithMetadata{}
ctyValue, err := goTypeToCty(value)
if err != nil {
return err
}
valueWithMetadata.Value = ctyValue
metadata, found := config.GetFieldMetadata(metadataName)
if found {
valueWithMetadata.Metadata = metadata
}
ctyJson, err := goTypeToCty(valueWithMetadata)
if err != nil {
return err
}
if ctyJson != cty.NilVal {
(*output)[metadataName] = ctyJson
}
return nil
}
// ValueWithMetadata stores value and metadata used in render-json with metadata.
type ValueWithMetadata struct {
Value cty.Value `json:"value" cty:"value"`
Metadata map[string]string `json:"metadata" cty:"metadata"`
}
// ctyCatalogConfig is an alternate representation of CatalogConfig that converts internal blocks into a map that
// maps the name to the underlying struct, as opposed to a list representation.
type ctyCatalogConfig struct {
URLs []string `cty:"urls"`
}
// Serialize CatalogConfig to a cty Value, but with maps instead of lists for the blocks.
func catalogConfigAsCty(config *CatalogConfig) (cty.Value, error) {
if config == nil {
return cty.NilVal, nil
}
configCty := ctyCatalogConfig{
URLs: config.URLs,
}
return goTypeToCty(configCty)
}
// ctyTerraformConfig is an alternate representation of TerraformConfig that converts internal blocks into a map that
// maps the name to the underlying struct, as opposed to a list representation.
type ctyTerraformConfig struct {
ExtraArgs map[string]TerraformExtraArguments `cty:"extra_arguments"`
Source *string `cty:"source"`
IncludeInCopy *[]string `cty:"include_in_copy"`
BeforeHooks map[string]Hook `cty:"before_hook"`
AfterHooks map[string]Hook `cty:"after_hook"`
ErrorHooks map[string]ErrorHook `cty:"error_hook"`
}
// Serialize TerraformConfig to a cty Value, but with maps instead of lists for the blocks.
func terraformConfigAsCty(config *TerraformConfig) (cty.Value, error) {
if config == nil {
return cty.NilVal, nil
}
configCty := ctyTerraformConfig{
Source: config.Source,
IncludeInCopy: config.IncludeInCopy,
ExtraArgs: map[string]TerraformExtraArguments{},
BeforeHooks: map[string]Hook{},
AfterHooks: map[string]Hook{},
ErrorHooks: map[string]ErrorHook{},
}
for _, arg := range config.ExtraArgs {
configCty.ExtraArgs[arg.Name] = arg
}
for _, hook := range config.BeforeHooks {
configCty.BeforeHooks[hook.Name] = hook
}
for _, hook := range config.AfterHooks {
configCty.AfterHooks[hook.Name] = hook
}
for _, errorHook := range config.ErrorHooks {
configCty.ErrorHooks[errorHook.Name] = errorHook
}
return goTypeToCty(configCty)
}
// Serialize RemoteState to a cty Value. We can't directly serialize the struct because `config` is an arbitrary
// interface whose type we do not know, so we have to do a hack to go through json.
func remoteStateAsCty(remoteState *remote.RemoteState) (cty.Value, error) {
if remoteState == nil {
return cty.NilVal, nil
}
output := map[string]cty.Value{}
output["backend"] = gostringToCty(remoteState.Backend)
output["disable_init"] = goboolToCty(remoteState.DisableInit)
output["disable_dependency_optimization"] = goboolToCty(remoteState.DisableDependencyOptimization)
generateCty, err := goTypeToCty(remoteState.Generate)
if err != nil {
return cty.NilVal, err
}
output["generate"] = generateCty
ctyJsonVal, err := convertToCtyWithJson(remoteState.Config)
if err != nil {
return cty.NilVal, err
}
output["config"] = ctyJsonVal
return convertValuesMapToCtyVal(output)
}
// Serialize the list of dependency blocks to a cty Value as a map that maps the block names to the cty representation.
func dependencyBlocksAsCty(dependencyBlocks []Dependency) (cty.Value, error) {
out := map[string]cty.Value{}
for _, block := range dependencyBlocks {
blockCty, err := goTypeToCty(block)
if err != nil {
return cty.NilVal, err
}
out[block.Name] = blockCty
}
return convertValuesMapToCtyVal(out)
}
// Converts arbitrary go types that are json serializable to a cty Value by using json as an intermediary
// representation. This avoids the strict type nature of cty, where you need to know the output type beforehand to
// serialize to cty.
func convertToCtyWithJson(val interface{}) (cty.Value, error) {
jsonBytes, err := json.Marshal(val)
if err != nil {
return cty.NilVal, errors.WithStackTrace(err)
}
var ctyJsonVal ctyjson.SimpleJSONValue
if err := ctyJsonVal.UnmarshalJSON(jsonBytes); err != nil {
return cty.NilVal, errors.WithStackTrace(err)
}
return ctyJsonVal.Value, nil
}
// Converts arbitrary go type (struct that has cty tags, slice, map with string keys, string, bool, int
// uint, float, cty.Value) to a cty Value
func goTypeToCty(val interface{}) (cty.Value, error) {
ctyType, err := gocty.ImpliedType(val)
if err != nil {
return cty.NilVal, errors.WithStackTrace(err)
}
ctyOut, err := gocty.ToCtyValue(val, ctyType)
if err != nil {
return cty.NilVal, errors.WithStackTrace(err)
}
return ctyOut, nil
}
// Converts primitive go strings to a cty Value.
func gostringToCty(val string) cty.Value {
ctyOut, err := gocty.ToCtyValue(val, cty.String)
if err != nil {
// Since we are converting primitive strings, we should never get an error in this conversion.
panic(err)
}
return ctyOut
}
// Converts primitive go bools to a cty Value.
func goboolToCty(val bool) cty.Value {
ctyOut, err := gocty.ToCtyValue(val, cty.Bool)
if err != nil {
// Since we are converting primitive bools, we should never get an error in this conversion.
panic(err)
}
return ctyOut
}