Skip to content

Commit d7cb69e

Browse files
author
Liam Cervante
committed
Convert variable types before applying defaults
1 parent 4bc1696 commit d7cb69e

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

internal/terraform/context_apply_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12027,7 +12027,7 @@ output "out" {
1202712027
Mode: plans.NormalMode,
1202812028
SetVariables: InputValues{
1202912029
"in": &InputValue{
12030-
Value: cty.MapVal(map[string]cty.Value{
12030+
Value: cty.ObjectVal(map[string]cty.Value{
1203112031
"required": cty.StringVal("boop"),
1203212032
}),
1203312033
SourceType: ValueFromCaller,

internal/terraform/eval_variable.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In
9090
given = defaultVal // must be set, because we checked above that the variable isn't required
9191
}
9292

93-
// Apply defaults from the variable's type constraint to the given value,
94-
// unless the given value is null. We do not apply defaults to top-level
95-
// null values, as doing so could prevent assigning null to a nullable
96-
// variable.
97-
if cfg.TypeDefaults != nil && !given.IsNull() {
98-
given = cfg.TypeDefaults.Apply(given)
99-
}
100-
10193
val, err := convert.Convert(given, convertTy)
10294
if err != nil {
10395
log.Printf("[ERROR] prepareFinalInputVariableValue: %s has unsuitable type\n got: %s\n want: %s", addr, given.Type(), convertTy)
@@ -140,6 +132,14 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In
140132
return cty.UnknownVal(cfg.Type), diags
141133
}
142134

135+
// Apply defaults from the variable's type constraint to the given value,
136+
// unless the given value is null. We do not apply defaults to top-level
137+
// null values, as doing so could prevent assigning null to a nullable
138+
// variable.
139+
if cfg.TypeDefaults != nil && !val.IsNull() {
140+
val = cfg.TypeDefaults.Apply(val)
141+
}
142+
143143
// By the time we get here, we know:
144144
// - val matches the variable's type constraint
145145
// - val is definitely not cty.NilVal, but might be a null value if the given was already null.

internal/terraform/eval_variable_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
6868
nullable = false
6969
type = string
7070
}
71+
variable "complex_type_with_nested_default_optional" {
72+
type = set(object({
73+
name = string
74+
schedules = set(object({
75+
name = string
76+
cold_storage_after = optional(number, 10)
77+
}))
78+
}))
79+
}
7180
`
7281
cfg := testModuleInline(t, map[string]string{
7382
"main.tf": cfgSrc,
@@ -399,6 +408,59 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
399408
``,
400409
},
401410

411+
// complex types
412+
413+
{
414+
"complex_type_with_nested_default_optional",
415+
cty.SetVal([]cty.Value{
416+
cty.ObjectVal(map[string]cty.Value{
417+
"name": cty.StringVal("test1"),
418+
"schedules": cty.SetVal([]cty.Value{
419+
cty.MapVal(map[string]cty.Value{
420+
"name": cty.StringVal("daily"),
421+
}),
422+
}),
423+
}),
424+
cty.ObjectVal(map[string]cty.Value{
425+
"name": cty.StringVal("test2"),
426+
"schedules": cty.SetVal([]cty.Value{
427+
cty.MapVal(map[string]cty.Value{
428+
"name": cty.StringVal("daily"),
429+
}),
430+
cty.MapVal(map[string]cty.Value{
431+
"name": cty.StringVal("weekly"),
432+
"cold_storage_after": cty.StringVal("0"),
433+
}),
434+
}),
435+
}),
436+
}),
437+
cty.SetVal([]cty.Value{
438+
cty.ObjectVal(map[string]cty.Value{
439+
"name": cty.StringVal("test1"),
440+
"schedules": cty.SetVal([]cty.Value{
441+
cty.ObjectVal(map[string]cty.Value{
442+
"name": cty.StringVal("daily"),
443+
"cold_storage_after": cty.NumberIntVal(10),
444+
}),
445+
}),
446+
}),
447+
cty.ObjectVal(map[string]cty.Value{
448+
"name": cty.StringVal("test2"),
449+
"schedules": cty.SetVal([]cty.Value{
450+
cty.ObjectVal(map[string]cty.Value{
451+
"name": cty.StringVal("daily"),
452+
"cold_storage_after": cty.NumberIntVal(10),
453+
}),
454+
cty.ObjectVal(map[string]cty.Value{
455+
"name": cty.StringVal("weekly"),
456+
"cold_storage_after": cty.NumberIntVal(0),
457+
}),
458+
}),
459+
}),
460+
}),
461+
``,
462+
},
463+
402464
// sensitive
403465
{
404466
"constrained_string_sensitive_required",

0 commit comments

Comments
 (0)