Skip to content

Commit

Permalink
Support for_each schema extensions (#155)
Browse files Browse the repository at this point in the history
Add support for for_each references within resource, data or module blocks.
  • Loading branch information
jpogran authored Oct 31, 2022
1 parent ef7f11b commit 619c765
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl-lang v0.0.0-20221014125844-7eceda07a779
github.com/hashicorp/hcl-lang v0.0.0-20221031081328-b20d6288c7f3
github.com/hashicorp/hcl/v2 v2.14.1
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go-version v1.5.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl-lang v0.0.0-20221014125844-7eceda07a779 h1:xzrUeFJXnrQ3U7sNrPLdtY8d2kmYr1BqY8X2fCagsl0=
github.com/hashicorp/hcl-lang v0.0.0-20221014125844-7eceda07a779/go.mod h1:olv+pM633vw0rXvyHIPjw62ur/FNqqNV7b5TfSxLY6E=
github.com/hashicorp/hcl-lang v0.0.0-20221031081328-b20d6288c7f3 h1:vGYvIZ5vznlnvJp4lmTz0yvkE6P7by+myhKPXJhC61c=
github.com/hashicorp/hcl-lang v0.0.0-20221031081328-b20d6288c7f3/go.mod h1:rn5V4b1/658M3g1o+MtdIUl/ZNoCVasB9T31j8XllMM=
github.com/hashicorp/hcl/v2 v2.14.1 h1:x0BpjfZ+CYdbiz+8yZTQ+gdLO7IXvOut7Da+XJayx34=
github.com/hashicorp/hcl/v2 v2.14.1/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
Expand Down
17 changes: 2 additions & 15 deletions internal/schema/0.12/data_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/hashicorp/terraform-schema/internal/schema/tokmod"
"github.com/zclconf/go-cty/cty"
)

func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
Expand Down Expand Up @@ -43,7 +42,8 @@ func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
"Terraform module, but has no significance outside of the scope of a module."),
Body: &schema.BodySchema{
Extensions: &schema.BodyExtensions{
Count: true,
Count: true,
ForEach: true, // for_each was introduced in 0.12.6, but for simplicity we report it for all 0.12+
},
Attributes: map[string]*schema.AttributeSchema{
"provider": {
Expand Down Expand Up @@ -73,18 +73,5 @@ func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
},
}

if v.GreaterThanOrEqual(v0_12_6) {
bs.Body.Attributes["for_each"] = &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Set(cty.DynamicPseudoType)},
schema.TraversalExpr{OfType: cty.Map(cty.DynamicPseudoType)},
schema.LiteralTypeExpr{Type: cty.Set(cty.DynamicPseudoType)},
schema.LiteralTypeExpr{Type: cty.Map(cty.DynamicPseudoType)},
},
IsOptional: true,
Description: lang.Markdown("A set or a map where each item represents an instance of this data source"),
}
}

return bs
}
16 changes: 2 additions & 14 deletions internal/schema/0.12/resource_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func resourceBlockSchema(v *version.Version) *schema.BlockSchema {
"outside of the scope of a module."),
Body: &schema.BodySchema{
Extensions: &schema.BodyExtensions{
Count: true,
Count: true,
ForEach: true, // for_each was introduced in 0.12.6, but for simplicity we report it for all 0.12+
},
Attributes: map[string]*schema.AttributeSchema{
"provider": {
Expand Down Expand Up @@ -77,19 +78,6 @@ func resourceBlockSchema(v *version.Version) *schema.BlockSchema {
},
}

if v.GreaterThanOrEqual(v0_12_6) {
bs.Body.Attributes["for_each"] = &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Set(cty.DynamicPseudoType)},
schema.TraversalExpr{OfType: cty.Map(cty.DynamicPseudoType)},
schema.LiteralTypeExpr{Type: cty.Set(cty.DynamicPseudoType)},
schema.LiteralTypeExpr{Type: cty.Map(cty.DynamicPseudoType)},
},
IsOptional: true,
Description: lang.Markdown("A set or a map where each item represents an instance of this resource"),
}
}

return bs
}

Expand Down
13 changes: 2 additions & 11 deletions internal/schema/0.13/module_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func moduleBlockSchema() *schema.BlockSchema {
Description: lang.PlainText("Module block to call a locally or remotely stored module"),
Body: &schema.BodySchema{
Extensions: &schema.BodyExtensions{
Count: true,
Count: true,
ForEach: true,
},
Attributes: map[string]*schema.AttributeSchema{
"source": {
Expand Down Expand Up @@ -75,16 +76,6 @@ func moduleBlockSchema() *schema.BlockSchema {
IsOptional: true,
Description: lang.Markdown("Explicit mapping of providers which the module uses"),
},
"for_each": {
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Set(cty.DynamicPseudoType)},
schema.TraversalExpr{OfType: cty.Map(cty.DynamicPseudoType)},
schema.LiteralTypeExpr{Type: cty.Set(cty.DynamicPseudoType)},
schema.LiteralTypeExpr{Type: cty.Map(cty.DynamicPseudoType)},
},
IsOptional: true,
Description: lang.Markdown("A set or a map where each item represents an instance of this module"),
},
"depends_on": {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Expand Down

0 comments on commit 619c765

Please sign in to comment.