Commit 922de89b authored by Alisdair McDiarmid's avatar Alisdair McDiarmid
Browse files

Conclude module variable optional attrs experiment

parent 29435b46
Showing with 2 additions and 68 deletions
+2 -68
......@@ -6,7 +6,6 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/experiments"
"github.com/hashicorp/terraform/version"
"github.com/zclconf/go-cty/cty"
)
// When developing UI for experimental features, you can temporarily disable
......@@ -196,51 +195,5 @@ func checkModuleExperiments(m *Module) hcl.Diagnostics {
}
*/
if !m.ActiveExperiments.Has(experiments.ModuleVariableOptionalAttrs) {
for _, v := range m.Variables {
if typeConstraintHasOptionalAttrs(v.ConstraintType) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Optional object type attributes are experimental",
Detail: "This feature is currently an opt-in experiment, subject to change in future releases based on feedback.\n\nActivate the feature for this module by adding module_variable_optional_attrs to the list of active experiments.",
Subject: v.DeclRange.Ptr(),
})
}
}
}
return diags
}
func typeConstraintHasOptionalAttrs(ty cty.Type) bool {
if ty == cty.NilType {
// Weird, but we'll just ignore it to avoid crashing.
return false
}
switch {
case ty.IsPrimitiveType():
return false
case ty.IsCollectionType():
return typeConstraintHasOptionalAttrs(ty.ElementType())
case ty.IsObjectType():
if len(ty.OptionalAttributes()) != 0 {
return true
}
for _, aty := range ty.AttributeTypes() {
if typeConstraintHasOptionalAttrs(aty) {
return true
}
}
return false
case ty.IsTupleType():
for _, ety := range ty.TupleElementTypes() {
if typeConstraintHasOptionalAttrs(ety) {
return true
}
}
return false
default:
return false
}
}
variable "a" {
type = object({
# The optional attributes experiment isn't enabled, so this isn't allowed.
a = optional(string)
})
}
terraform {
experiments = [
module_variable_optional_attrs, # WARNING: Experimental feature "module_variable_optional_attrs" is active
]
}
variable "a" {
type = object({
foo = optional(string)
......
......@@ -27,7 +27,7 @@ func init() {
registerConcludedExperiment(SuppressProviderSensitiveAttrs, "Provider-defined sensitive attributes are now redacted by default, without enabling an experiment.")
registerConcludedExperiment(ConfigDrivenMove, "Declarations of moved resource instances using \"moved\" blocks can now be used by default, without enabling an experiment.")
registerConcludedExperiment(PreconditionsPostconditions, "Condition blocks can now be used by default, without enabling an experiment.")
registerCurrentExperiment(ModuleVariableOptionalAttrs)
registerConcludedExperiment(ModuleVariableOptionalAttrs, "Optional object attributes in module variable type constraints can now be used by default, without enabling an experiment.")
}
// GetCurrent takes an experiment name and returns the experiment value
......@@ -92,6 +92,7 @@ var currentExperiments = make(Set)
// Members of this map are registered in the init function above.
var concludedExperiments = make(map[Experiment]string)
//lint:ignore U1000 No experiments are active
func registerCurrentExperiment(exp Experiment) {
currentExperiments.Add(exp)
}
......
......@@ -11978,10 +11978,6 @@ resource "test_resource" "foo" {
func TestContext2Apply_moduleVariableOptionalAttributes(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
terraform {
experiments = [module_variable_optional_attrs]
}
variable "in" {
type = object({
required = string
......@@ -12054,10 +12050,6 @@ output "out" {
func TestContext2Apply_moduleVariableOptionalAttributesDefault(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
terraform {
experiments = [module_variable_optional_attrs]
}
variable "in" {
type = object({
required = string
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment