Skip to content

Commit

Permalink
Add schema for Terraform 0.15 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 11, 2021
1 parent 451e1bc commit 58cdd74
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 16 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.5
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl-lang v0.0.0-20210305083102-791d1fc99bad
github.com/hashicorp/hcl-lang v0.0.0-20210311200725-da0537032422
github.com/hashicorp/hcl/v2 v2.9.1
github.com/hashicorp/terraform-config-inspect v0.0.0-20201102131242-0c45ba392e51
github.com/hashicorp/terraform-json v0.8.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pB
github.com/hashicorp/go-version v1.2.1/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-20210305083102-791d1fc99bad h1:ynQREphl/QqetQnYIWergBwcuxunJYwhaGxGALgwjzU=
github.com/hashicorp/hcl-lang v0.0.0-20210305083102-791d1fc99bad/go.mod h1:Ga5F2eYh9HyR2eHBYBa49VdoTkkC/7ob1cV8Mpvhg5M=
github.com/hashicorp/hcl-lang v0.0.0-20210311200725-da0537032422 h1:hDjba3BV66uIFJN07x2xcubtueRoZMVPpSScv0fQtjI=
github.com/hashicorp/hcl-lang v0.0.0-20210311200725-da0537032422/go.mod h1:TUGbLnxaJsOmfcJ+hwWTQxLmZX2lZSd+uMFNNphz6hc=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.9.0 h1:7kJiMiKBqGHASbDJuFAMlpRMJLyhuLg/IsU/3EzwniA=
github.com/hashicorp/hcl/v2 v2.9.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.9.1 h1:eOy4gREY0/ZQHNItlfuEZqtcQbXIxzojlP301hDpnac=
github.com/hashicorp/hcl/v2 v2.9.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/terraform-config-inspect v0.0.0-20201102131242-0c45ba392e51 h1:SEGO1vz/pFLfKy4QpABIMCe7wffmtsOiWO4yc1E87cU=
Expand Down
5 changes: 5 additions & 0 deletions internal/schema/0.12/resource_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ var lifecycleBlock = &schema.BlockSchema{
"ignore_changes": {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{},
schema.KeywordExpr{
Keyword: "all",
Description: lang.Markdown("Ignore all attributes, which means that Terraform can create" +
" and destroy the remote object but will never propose updates to it"),
},
},
IsOptional: true,
Description: lang.Markdown("A set of fields (references) of which to ignore changes to, e.g. `tags`"),
Expand Down
12 changes: 9 additions & 3 deletions internal/schema/0.12/terraform_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ func terraformBlockSchema(v *version.Version) *schema.BlockSchema {
bs.Body.Blocks["required_providers"].Body = &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.LiteralTypeExpr{Type: cty.Object(map[string]cty.Type{
"version": cty.String,
})},
schema.ObjectExpr{
Attributes: schema.ObjectExprAttributes{
"version": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("Version constraint specifying which subset of " +
"available provider versions the module is compatible with, e.g. `~> 1.0`"),
},
},
},
schema.LiteralTypeExpr{Type: cty.String},
},
Description: lang.Markdown("Version constraint"),
Expand Down
18 changes: 14 additions & 4 deletions internal/schema/0.13/terraform_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,20 @@ var terraformBlockSchema = &schema.BlockSchema{
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.LiteralTypeExpr{Type: cty.Object(map[string]cty.Type{
"source": cty.String,
"version": cty.String,
})},
schema.ObjectExpr{
Attributes: schema.ObjectExprAttributes{
"source": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("The global source address for the provider " +
"you intend to use, such as `hashicorp/aws`"),
},
"version": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("Version constraint specifying which subset of " +
"available provider versions the module is compatible with, e.g. `~> 1.0`"),
},
},
},
schema.LiteralTypeExpr{Type: cty.String},
},
Description: lang.Markdown("Provider source and version constraint"),
Expand Down
18 changes: 14 additions & 4 deletions internal/schema/0.14/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@ func terraformBlockSchema(v *version.Version) *schema.BlockSchema {
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.LiteralTypeExpr{Type: cty.Object(map[string]cty.Type{
"source": cty.String,
"version": cty.String,
})},
schema.ObjectExpr{
Attributes: schema.ObjectExprAttributes{
"source": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("The global source address for the provider " +
"you intend to use, such as `hashicorp/aws`"),
},
"version": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("Version constraint specifying which subset of " +
"available provider versions the module is compatible with, e.g. `~> 1.0`"),
},
},
},
schema.LiteralTypeExpr{Type: cty.String},
},
Description: lang.Markdown("Provider source and version constraint"),
Expand Down
14 changes: 14 additions & 0 deletions internal/schema/0.15/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package schema

import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/schema"

v014_mod "github.com/hashicorp/terraform-schema/internal/schema/0.14"
)

func ModuleSchema(v *version.Version) *schema.BodySchema {
bs := v014_mod.ModuleSchema(v)
bs.Blocks["terraform"] = patchTerraformBlockSchema(bs.Blocks["terraform"], v)
return bs
}
43 changes: 43 additions & 0 deletions internal/schema/0.15/terraform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package schema

import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/zclconf/go-cty/cty"
)

func patchTerraformBlockSchema(bs *schema.BlockSchema, v *version.Version) *schema.BlockSchema {
bs.Body.Blocks["required_providers"].Body = &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.ObjectExpr{
Attributes: schema.ObjectExprAttributes{
"source": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("The global source address for the provider " +
"you intend to use, such as `hashicorp/aws`"),
},
"version": schema.ObjectAttribute{
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.Markdown("Version constraint specifying which subset of " +
"available provider versions the module is compatible with, e.g. `~> 1.0`"),
},
"configuration_aliases": schema.ObjectAttribute{
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Name: "set of aliases",
},
},
Description: lang.Markdown("Aliases under which to make the provider available, " +
"such as `[ aws.eu-west, aws.us-east ]`"),
},
},
},
schema.LiteralTypeExpr{Type: cty.String},
},
Description: lang.Markdown("Provider source, version constraint and its aliases"),
},
}
return bs
}
5 changes: 5 additions & 0 deletions schema/core_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
mod_v0_12 "github.com/hashicorp/terraform-schema/internal/schema/0.12"
mod_v0_13 "github.com/hashicorp/terraform-schema/internal/schema/0.13"
mod_v0_14 "github.com/hashicorp/terraform-schema/internal/schema/0.14"
mod_v0_15 "github.com/hashicorp/terraform-schema/internal/schema/0.15"
universal "github.com/hashicorp/terraform-schema/internal/schema/universal"
)

var (
v0_12 = version.Must(version.NewVersion("0.12"))
v0_13 = version.Must(version.NewVersion("0.13"))
v0_14 = version.Must(version.NewVersion("0.14"))
v0_15 = version.Must(version.NewVersion("0.15"))
)

// CoreModuleSchemaForVersion finds a module schema which is relevant
Expand All @@ -26,6 +28,9 @@ func CoreModuleSchemaForVersion(v *version.Version) (*schema.BodySchema, error)
return nil, fmt.Errorf("invalid version: %w", err)
}

if ver.GreaterThanOrEqual(v0_15) {
return mod_v0_15.ModuleSchema(ver), nil
}
if ver.GreaterThanOrEqual(v0_14) {
return mod_v0_14.ModuleSchema(ver), nil
}
Expand Down

0 comments on commit 58cdd74

Please sign in to comment.