Skip to content

Commit

Permalink
Merge d7cb69e into backport/liamcervante/convert-before-defaults/comm…
Browse files Browse the repository at this point in the history
…only-improved-bobcat
  • Loading branch information
teamterraform committed Nov 2, 2022
2 parents 50db8e2 + d7cb69e commit 6e93746
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/terraform/context_apply_test.go
Expand Up @@ -12027,7 +12027,7 @@ output "out" {
Mode: plans.NormalMode,
SetVariables: InputValues{
"in": &InputValue{
Value: cty.MapVal(map[string]cty.Value{
Value: cty.ObjectVal(map[string]cty.Value{
"required": cty.StringVal("boop"),
}),
SourceType: ValueFromCaller,
Expand Down
16 changes: 8 additions & 8 deletions internal/terraform/eval_variable.go
Expand Up @@ -90,14 +90,6 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In
given = defaultVal // must be set, because we checked above that the variable isn't required
}

// Apply defaults from the variable's type constraint to the given value,
// unless the given value is null. We do not apply defaults to top-level
// null values, as doing so could prevent assigning null to a nullable
// variable.
if cfg.TypeDefaults != nil && !given.IsNull() {
given = cfg.TypeDefaults.Apply(given)
}

val, err := convert.Convert(given, convertTy)
if err != nil {
log.Printf("[ERROR] prepareFinalInputVariableValue: %s has unsuitable type\n got: %s\n want: %s", addr, given.Type(), convertTy)
Expand Down Expand Up @@ -140,6 +132,14 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In
return cty.UnknownVal(cfg.Type), diags
}

// Apply defaults from the variable's type constraint to the given value,
// unless the given value is null. We do not apply defaults to top-level
// null values, as doing so could prevent assigning null to a nullable
// variable.
if cfg.TypeDefaults != nil && !val.IsNull() {
val = cfg.TypeDefaults.Apply(val)
}

// By the time we get here, we know:
// - val matches the variable's type constraint
// - val is definitely not cty.NilVal, but might be a null value if the given was already null.
Expand Down
62 changes: 62 additions & 0 deletions internal/terraform/eval_variable_test.go
Expand Up @@ -68,6 +68,15 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
nullable = false
type = string
}
variable "complex_type_with_nested_default_optional" {
type = set(object({
name = string
schedules = set(object({
name = string
cold_storage_after = optional(number, 10)
}))
}))
}
`
cfg := testModuleInline(t, map[string]string{
"main.tf": cfgSrc,
Expand Down Expand Up @@ -399,6 +408,59 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
``,
},

// complex types

{
"complex_type_with_nested_default_optional",
cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("test1"),
"schedules": cty.SetVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"name": cty.StringVal("daily"),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("test2"),
"schedules": cty.SetVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"name": cty.StringVal("daily"),
}),
cty.MapVal(map[string]cty.Value{
"name": cty.StringVal("weekly"),
"cold_storage_after": cty.StringVal("0"),
}),
}),
}),
}),
cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("test1"),
"schedules": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("daily"),
"cold_storage_after": cty.NumberIntVal(10),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("test2"),
"schedules": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("daily"),
"cold_storage_after": cty.NumberIntVal(10),
}),
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("weekly"),
"cold_storage_after": cty.NumberIntVal(0),
}),
}),
}),
}),
``,
},

// sensitive
{
"constrained_string_sensitive_required",
Expand Down

0 comments on commit 6e93746

Please sign in to comment.