Files
opentf/internal/terraform/testdata/apply-nullable-variables/mod/main.tf
James Bardin f0a64eb456 configs: explicitly nullable variable values
The current behavior of module input variables is to allow users to
override a default by assigning `null`, which works contrary to the
behavior of resource attributes, and prevents explicitly accepting a
default when the input must be defined in the configuration.

Add a new variable attribute called `nullable` will allow explicitly
defining when a variable can be set to null or not. The current default
behavior is that of `nullable=true`.

Setting `nullable=false` in a variable block indicates that the variable
value can never be null. This either requires a non-null input value, or
a non-null default value. In the case of the latter, we also opt-in to
the new behavior of a `null` input value taking the default rather than
overriding it.

In a future language edition where we make `nullable=false` the default,
setting `nullable=true` will allow the legacy behavior of `null`
overriding a default value. The only future configuration in which this
would be required even if the legacy behavior were not desired is when
setting an optional+nullable value. In that case `default=null` would
also be needed and we could therefor imply `nullable=true` without
requiring it in the configuration.
2021-10-29 13:59:46 -04:00

60 lines
1.2 KiB
HCL

// optional, and this can take null as an input
variable "nullable_null_default" {
// This is implied now as the default, and probably should be implied even
// when nullable=false is the default, so we're leaving this unset for the test.
// nullable = true
default = null
}
// assigning null can still override the default.
variable "nullable_non_null_default" {
nullable = true
default = "ok"
}
// required, and assigning null is valid.
variable "nullable_no_default" {
nullable = true
}
// this combination is invalid
//variable "non_nullable_null_default" {
// nullable = false
// default = null
//}
// assigning null will take the default
variable "non_nullable_default" {
nullable = false
default = "ok"
}
// required, but null is not a valid value
variable "non_nullable_no_default" {
nullable = false
}
output "nullable_null_default" {
value = var.nullable_null_default
}
output "nullable_non_null_default" {
value = var.nullable_non_null_default
}
output "nullable_no_default" {
value = var.nullable_no_default
}
output "non_nullable_default" {
value = var.non_nullable_default
}
output "non_nullable_no_default" {
value = var.non_nullable_no_default
}