diff --git a/internal/schema/0.12/variable_block.go b/internal/schema/0.12/variable_block.go index ec88dd1d..cc98cc08 100644 --- a/internal/schema/0.12/variable_block.go +++ b/internal/schema/0.12/variable_block.go @@ -39,11 +39,6 @@ func variableBlockSchema(v *version.Version) *schema.BlockSchema { IsOptional: true, Description: lang.Markdown("Type constraint restricting the type of value to accept, e.g. `string` or `list(string)`"), }, - "default": { - Expr: schema.ExprConstraints{}, - IsOptional: true, - Description: lang.Markdown("Default value to use when variable is not explicitly set"), - }, }, Blocks: make(map[string]*schema.BlockSchema, 0), }, diff --git a/internal/schema/0.14/variable_block.go b/internal/schema/0.14/variable_block.go index ab5fbd62..a78e4442 100644 --- a/internal/schema/0.14/variable_block.go +++ b/internal/schema/0.14/variable_block.go @@ -38,11 +38,6 @@ var variableBlockSchema = &schema.BlockSchema{ IsOptional: true, Description: lang.Markdown("Type constraint restricting the type of value to accept, e.g. `string` or `list(string)`"), }, - "default": { - Expr: schema.ExprConstraints{}, - IsOptional: true, - Description: lang.Markdown("Default value to use when variable is not explicitly set"), - }, "sensitive": { Expr: schema.LiteralTypeOnly(cty.Bool), IsOptional: true, diff --git a/schema/schema_merge.go b/schema/schema_merge.go index 65943f23..3ed0e009 100644 --- a/schema/schema_merge.go +++ b/schema/schema_merge.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/hcl-lang/lang" "github.com/hashicorp/hcl-lang/schema" - "github.com/hashicorp/terraform-registry-address" + tfaddr "github.com/hashicorp/terraform-registry-address" "github.com/hashicorp/terraform-schema/module" ) @@ -166,9 +166,42 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e } } + if _, ok := mergedSchema.Blocks["variable"]; ok { + mergedSchema.Blocks["variable"].Labels = []*schema.LabelSchema{ + { + Name: "name", + IsDepKey: true, + Description: lang.PlainText("Variable name"), + }, + } + mergedSchema.Blocks["variable"].DependentBody = variableDependentBody(meta.Variables) + } return mergedSchema, nil } +func variableDependentBody(vars map[string]module.Variable) map[schema.SchemaKey]*schema.BodySchema { + depBodies := make(map[schema.SchemaKey]*schema.BodySchema) + + for name, mVar := range vars { + depKeys := schema.DependencyKeys{ + Labels: []schema.LabelDependent{ + {Index: 0, Value: name}, + }, + } + depBodies[schema.NewSchemaKey(depKeys)] = &schema.BodySchema{ + Attributes: map[string]*schema.AttributeSchema{ + "default": { + Expr: schema.ExprConstraints{schema.LiteralTypeExpr{Type: mVar.Type}}, + IsOptional: true, + Description: lang.Markdown("Default value to use when variable is not explicitly set"), + }, + }, + } + } + + return depBodies +} + type ProviderReferences map[module.ProviderRef]tfaddr.Provider func (pr ProviderReferences) ReferencesOfProvider(addr tfaddr.Provider) []module.ProviderRef {