Skip to content

Commit

Permalink
Add schema for state backends (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jun 10, 2021
1 parent 8f28989 commit db0f98d
Show file tree
Hide file tree
Showing 29 changed files with 2,299 additions and 115 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.6
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl-lang v0.0.0-20210609120759-c626a5e211fc
github.com/hashicorp/hcl-lang v0.0.0-20210609161122-bc4299c29bd1
github.com/hashicorp/hcl/v2 v2.10.0
github.com/hashicorp/terraform-json v0.11.0
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896
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-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl-lang v0.0.0-20210609120759-c626a5e211fc h1:Kuz00XoVdIcsZ4ak4xI1Tc/1waKqAISzgqgzWn0DE2Y=
github.com/hashicorp/hcl-lang v0.0.0-20210609120759-c626a5e211fc/go.mod h1:uMsq6wV8ZXEH8qndov4tncXlHDYnZ8aHsGmS4T74e2o=
github.com/hashicorp/hcl-lang v0.0.0-20210609161122-bc4299c29bd1 h1:GC7rz0G81RRnb8RHB6TH4s0UrOs8oEgZ6UqoaQ0lzSU=
github.com/hashicorp/hcl-lang v0.0.0-20210609161122-bc4299c29bd1/go.mod h1:uMsq6wV8ZXEH8qndov4tncXlHDYnZ8aHsGmS4T74e2o=
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-json v0.11.0 h1:4zDqqW2F3kOysORIaYKFGgWDYIRA3hwqx3XHeHkbBQ0=
Expand Down
8 changes: 5 additions & 3 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/backends"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)
Expand All @@ -26,12 +27,13 @@ func terraformBlockSchema(v *version.Version) *schema.BlockSchema {
"operations are performed, where state snapshots are stored, etc."),
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.Markdown("Backend Type"),
Name: "backend type",
Description: lang.Markdown("Backend type"),
IsDepKey: true,
},
},
MaxItems: 1,
MaxItems: 1,
DependentBody: backends.ConfigsAsDependentBodies(v),
},
"required_providers": {
Description: lang.Markdown("What provider version to use within this configuration"),
Expand Down
2 changes: 1 addition & 1 deletion internal/schema/0.13/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ModuleSchema(v *version.Version) *schema.BodySchema {

bs.Blocks["module"] = moduleBlockSchema
bs.Blocks["provider"] = providerBlockSchema
bs.Blocks["terraform"] = terraformBlockSchema
bs.Blocks["terraform"] = terraformBlockSchema(v)

return bs
}
129 changes: 67 additions & 62 deletions internal/schema/0.13/terraform_block.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,93 @@
package schema

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/backends"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)

var terraformBlockSchema = &schema.BlockSchema{
Description: lang.Markdown("Terraform block used to configure some high-level behaviors of Terraform"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"required_version": {
Expr: schema.LiteralTypeOnly(cty.String),
IsOptional: true,
Description: lang.Markdown("Constraint to specify which versions of Terraform can be used " +
"with this configuration, e.g. `~> 0.12`"),
},
"experiments": {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Name: "set of features",
func terraformBlockSchema(v *version.Version) *schema.BlockSchema {
return &schema.BlockSchema{
Description: lang.Markdown("Terraform block used to configure some high-level behaviors of Terraform"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"required_version": {
Expr: schema.LiteralTypeOnly(cty.String),
IsOptional: true,
Description: lang.Markdown("Constraint to specify which versions of Terraform can be used " +
"with this configuration, e.g. `~> 0.12`"),
},
"experiments": {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{
Name: "set of features",
},
},
IsOptional: true,
Description: lang.Markdown("A set of experimental language features to enable"),
},
IsOptional: true,
Description: lang.Markdown("A set of experimental language features to enable"),
},
},
Blocks: map[string]*schema.BlockSchema{
"backend": {
Description: lang.Markdown("Backend configuration which defines exactly where and how " +
"operations are performed, where state snapshots are stored, etc."),
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.Markdown("Backend Type"),
IsDepKey: true,
Blocks: map[string]*schema.BlockSchema{
"backend": {
Description: lang.Markdown("Backend configuration which defines exactly where and how " +
"operations are performed, where state snapshots are stored, etc."),
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.Markdown("Backend Type"),
IsDepKey: true,
},
},
DependentBody: backends.ConfigsAsDependentBodies(v),
},
},
"provider_meta": {
Description: lang.Markdown("Metadata to pass into a provider which supports this"),
Labels: []*schema.LabelSchema{
{
Name: "name",
Description: lang.Markdown("Provider Name"),
IsDepKey: true,
"provider_meta": {
Description: lang.Markdown("Metadata to pass into a provider which supports this"),
Labels: []*schema.LabelSchema{
{
Name: "name",
Description: lang.Markdown("Provider Name"),
IsDepKey: true,
},
},
},
},
"required_providers": {
Description: lang.Markdown("What provider version to use within this configuration " +
"and where to source it from"),
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.ObjectExpr{
Attributes: schema.ObjectExprAttributes{
"source": &schema.AttributeSchema{
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.AttributeSchema{
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`"),
"required_providers": {
Description: lang.Markdown("What provider version to use within this configuration " +
"and where to source it from"),
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Expr: schema.ExprConstraints{
schema.ObjectExpr{
Attributes: schema.ObjectExprAttributes{
"source": &schema.AttributeSchema{
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.AttributeSchema{
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},
},
schema.LiteralTypeExpr{Type: cty.String},
},
Address: &schema.AttributeAddrSchema{
Steps: []schema.AddrStep{
schema.AttrNameStep{},
Address: &schema.AttributeAddrSchema{
Steps: []schema.AddrStep{
schema.AttrNameStep{},
},
AsReference: true,
FriendlyName: "provider",
ScopeId: refscope.ProviderScope,
},
AsReference: true,
FriendlyName: "provider",
ScopeId: refscope.ProviderScope,
Description: lang.Markdown("Provider source and version constraint"),
},
Description: lang.Markdown("Provider source and version constraint"),
},
},
},
},
},
}
}
2 changes: 2 additions & 0 deletions internal/schema/0.14/terraform.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/backends"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -50,6 +51,7 @@ func terraformBlockSchema(v *version.Version) *schema.BlockSchema {
IsDepKey: true,
},
},
DependentBody: backends.ConfigsAsDependentBodies(v),
},
"provider_meta": {
Description: lang.Markdown("Metadata to pass into a provider which supports this"),
Expand Down
50 changes: 50 additions & 0 deletions internal/schema/backends/artifactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package backends

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 artifactoryBackend(v *version.Version) *schema.BodySchema {
// https://github.com/hashicorp/terraform/blob/v0.12.0/backend/remote-state/artifactory/backend.go
// https://github.com/hashicorp/terraform/blob/v1.0.0/internal/backend/remote-state/artifactory/backend.go
// Docs:
// https://github.com/hashicorp/terraform/blob/v1.0.0/website/docs/language/settings/backends/artifactory.html.md
docsUrl := "https://www.terraform.io/docs/language/settings/backends/artifactory.html"
return &schema.BodySchema{
Description: lang.Markdown("Artifactory"),
HoverURL: docsUrl,
DocsLink: &schema.DocsLink{
URL: docsUrl,
},
Attributes: map[string]*schema.AttributeSchema{
"username": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
Description: lang.Markdown("Username"),
},
"password": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
Description: lang.Markdown("Password"),
},
"url": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
Description: lang.Markdown("Artfactory base URL (i.e. URL without repo and subpath)"),
},
"repo": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
Description: lang.Markdown("The repository name"),
},
"subpath": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
Description: lang.Markdown("Path within the repository"),
},
},
}
}
Loading

0 comments on commit db0f98d

Please sign in to comment.