Skip to content

Commit

Permalink
Specify traversal schema (addresses) for core & providers (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored May 14, 2021
1 parent cf1deae commit b947185
Show file tree
Hide file tree
Showing 26 changed files with 2,561 additions and 415 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/google/go-cmp v0.5.5
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl-lang v0.0.0-20210419185146-8556dd730bc7
github.com/hashicorp/hcl-lang v0.0.0-20210514214009-15d3afdead68
github.com/hashicorp/hcl/v2 v2.10.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20210318070130-9a80970d6b34
github.com/hashicorp/terraform-json v0.11.0
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hashicorp/hcl-lang v0.0.0-20210419185146-8556dd730bc7 h1:6ajS9kFs/7UnRcy3dHThUnmicqU7VjGUn0RLOT6Ewq0=
github.com/hashicorp/hcl-lang v0.0.0-20210419185146-8556dd730bc7/go.mod h1:VRVfqufUmJSpWsoWDtYV/BejqCV+NNyS9V9vR0dcivs=
github.com/hashicorp/hcl-lang v0.0.0-20210514214009-15d3afdead68 h1:Y5HxVHg9bKiS8Q4Q3dHOdETtF4DEmKNVdBh93o5K1L0=
github.com/hashicorp/hcl-lang v0.0.0-20210514214009-15d3afdead68/go.mod h1:7BtIzYAy75UR501SFkNjL98xMZbvn5Vc7bj+dOgcH70=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.9.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.10.0 h1:1S1UnuhDGlv3gRFV4+0EdwB+znNP5HmcGbIqwnSCByg=
github.com/hashicorp/hcl/v2 v2.10.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/terraform-config-inspect v0.0.0-20210318070130-9a80970d6b34 h1:y4kOB9aYVSsJWVqewwUZmHxPSNjkDbOeW7eb9yFIc3Q=
Expand All @@ -55,13 +54,11 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5 h1:shw+DWUaHIyW64Tv30ASCbC6QO6fLy+M5SJb5pJVEI4=
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5/go.mod h1:nHPoxaBUc5CDAMIv0MNmn5PBjWbTs9BI/eh30/n0U6g=
github.com/mitchellh/copystructure v1.1.2/go.mod h1:EBArHfARyrSWO/+Wyr9zwEkc6XMFB9XyNgFNmRkZZU4=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -82,7 +79,6 @@ github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLE
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.8.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.8.3 h1:48gwZXrdSADU2UW9eZKHprxAI7APZGW9XmExpJpSjT0=
github.com/zclconf/go-cty v1.8.3/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI=
Expand Down
25 changes: 24 additions & 1 deletion internal/schema/0.12/data_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
bs := &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.StaticStep{Name: "data"},
schema.LabelStep{Index: 0},
schema.LabelStep{Index: 1},
},
FriendlyName: "datasource",
ScopeId: refscope.DataScope,
AsReference: true,
DependentBodyAsData: true,
InferDependentBody: true,
},
Labels: []*schema.LabelSchema{
{
Name: "type",
Expand All @@ -26,13 +39,16 @@ func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"provider": {
Expr: schema.ExprConstraints{},
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfScopeId: refscope.ProviderScope},
},
IsOptional: true,
Description: lang.Markdown("Reference to a `provider` configuration block, e.g. `mycloud.west` or `mycloud`"),
IsDepKey: true,
},
"count": {
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Number},
schema.LiteralTypeExpr{Type: cty.Number},
},
IsOptional: true,
Expand All @@ -42,6 +58,11 @@ func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Name: "set of references",
AnyElem: schema.ExprConstraints{
schema.TraversalExpr{OfScopeId: refscope.DataScope},
schema.TraversalExpr{OfScopeId: refscope.ModuleScope},
schema.TraversalExpr{OfScopeId: refscope.ResourceScope},
},
},
},
IsOptional: true,
Expand All @@ -54,6 +75,8 @@ 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)},
},
Expand Down
16 changes: 16 additions & 0 deletions internal/schema/0.12/locals_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@ package schema
import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/zclconf/go-cty/cty"
)

var localsBlockSchema = &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.StaticStep{Name: "local"},
},
FriendlyName: "local",
BodyAsData: true,
},
Description: lang.Markdown("Local values assigning names to expressions, so you can use these multiple times without repetition\n" +
"e.g. `service_name = \"forum\"`"),
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.DynamicPseudoType},
schema.LiteralTypeExpr{Type: cty.DynamicPseudoType},
},
},
},
}
15 changes: 15 additions & 0 deletions internal/schema/0.12/module_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ package schema
import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

var moduleBlockSchema = &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.StaticStep{Name: "module"},
schema.LabelStep{Index: 0},
},
FriendlyName: "module",
ScopeId: refscope.ModuleScope,
AsReference: true,
DependentBodyAsData: true,
InferDependentBody: true,
},
Labels: []*schema.LabelSchema{
{
Name: "name",
Expand Down Expand Up @@ -35,6 +47,9 @@ var moduleBlockSchema = &schema.BlockSchema{
Expr: schema.ExprConstraints{
schema.MapExpr{
Name: "map of provider references",
Elem: schema.ExprConstraints{
schema.TraversalExpr{OfScopeId: refscope.ProviderScope},
},
},
},
IsOptional: true,
Expand Down
20 changes: 19 additions & 1 deletion internal/schema/0.12/output_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ package schema
import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

var outputBlockSchema = &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.StaticStep{Name: "output"},
schema.LabelStep{Index: 0},
},
FriendlyName: "output",
ScopeId: refscope.OutputScope,
AsReference: true,
},
Labels: []*schema.LabelSchema{
{
Name: "name",
Expand All @@ -22,7 +32,10 @@ var outputBlockSchema = &schema.BlockSchema{
Description: lang.PlainText("Human-readable description of the output (for documentation and UI)"),
},
"value": {
Expr: schema.ExprConstraints{},
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.DynamicPseudoType},
schema.LiteralTypeExpr{Type: cty.DynamicPseudoType},
},
IsRequired: true,
Description: lang.PlainText("Value, typically a reference to an attribute of a resource or a data source"),
},
Expand All @@ -35,6 +48,11 @@ var outputBlockSchema = &schema.BlockSchema{
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Name: "set of references",
AnyElem: schema.ExprConstraints{
schema.TraversalExpr{OfScopeId: refscope.DataScope},
schema.TraversalExpr{OfScopeId: refscope.ModuleScope},
schema.TraversalExpr{OfScopeId: refscope.ResourceScope},
},
},
},
IsOptional: true,
Expand Down
10 changes: 10 additions & 0 deletions internal/schema/0.12/provider_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

func providerBlockSchema(v *version.Version) *schema.BlockSchema {
return &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.LabelStep{Index: 0},
schema.AttrValueStep{Name: "alias", IsOptional: true},
},
FriendlyName: "provider",
ScopeId: refscope.ProviderScope,
AsReference: true,
},
Labels: []*schema.LabelSchema{
{
Name: "name",
Expand Down
28 changes: 26 additions & 2 deletions internal/schema/0.12/resource_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

func resourceBlockSchema(v *version.Version) *schema.BlockSchema {
bs := &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.LabelStep{Index: 0},
schema.LabelStep{Index: 1},
},
FriendlyName: "resource",
ScopeId: refscope.ResourceScope,
AsReference: true,
DependentBodyAsData: true,
InferDependentBody: true,
},
Labels: []*schema.LabelSchema{
{
Name: "type",
Expand All @@ -26,20 +38,30 @@ func resourceBlockSchema(v *version.Version) *schema.BlockSchema {
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"provider": {
Expr: schema.ExprConstraints{},
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfScopeId: refscope.ProviderScope},
},
IsOptional: true,
Description: lang.Markdown("Reference to a `provider` configuration block, e.g. `mycloud.west` or `mycloud`"),
IsDepKey: true,
},
"count": {
Expr: schema.LiteralTypeOnly(cty.Number),
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Number},
schema.LiteralTypeExpr{Type: cty.Number},
},
IsOptional: true,
Description: lang.Markdown("Number of instances of this resource, e.g. `3`"),
},
"depends_on": {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Name: "set of references",
AnyElem: schema.ExprConstraints{
schema.TraversalExpr{OfScopeId: refscope.DataScope},
schema.TraversalExpr{OfScopeId: refscope.ModuleScope},
schema.TraversalExpr{OfScopeId: refscope.ResourceScope},
},
},
},
IsOptional: true,
Expand All @@ -56,6 +78,8 @@ 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)},
},
Expand Down
9 changes: 9 additions & 0 deletions internal/schema/0.12/terraform_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

Expand Down Expand Up @@ -38,6 +39,14 @@ func terraformBlockSchema(v *version.Version) *schema.BlockSchema {
AnyAttribute: &schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("Version constraint"),
Address: &schema.AttributeAddrSchema{
Steps: []schema.AddrStep{
schema.AttrNameStep{},
},
AsReference: true,
FriendlyName: "provider",
ScopeId: refscope.ProviderScope,
},
},
},
MaxItems: 1,
Expand Down
10 changes: 10 additions & 0 deletions internal/schema/0.12/variable_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

func variableBlockSchema(v *version.Version) *schema.BlockSchema {
bs := &schema.BlockSchema{
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.StaticStep{Name: "var"},
schema.LabelStep{Index: 0},
},
FriendlyName: "variable",
ScopeId: refscope.VariableScope,
AsReference: true,
},
Labels: []*schema.LabelSchema{
{
Name: "name",
Expand Down
Loading

0 comments on commit b947185

Please sign in to comment.