Conclude module variable optional attrs experiment

This commit is contained in:
Alisdair McDiarmid 2022-06-13 11:53:36 -04:00
parent 29435b46ac
commit 922de89be1
5 changed files with 2 additions and 68 deletions

View File

@ -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
}
}

View File

@ -1,6 +0,0 @@
variable "a" {
type = object({
# The optional attributes experiment isn't enabled, so this isn't allowed.
a = optional(string)
})
}

View File

@ -1,9 +1,3 @@
terraform {
experiments = [
module_variable_optional_attrs, # WARNING: Experimental feature "module_variable_optional_attrs" is active
]
}
variable "a" {
type = object({
foo = optional(string)

View File

@ -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)
}

View File

@ -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