Skip to content

Commit

Permalink
schema: add versioned schemas for some blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 6, 2020
1 parent 3b7b040 commit 2a2e42c
Show file tree
Hide file tree
Showing 24 changed files with 1,068 additions and 6 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ require (
github.com/google/go-cmp v0.3.1
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.0
github.com/hashicorp/hcl-lang v0.0.0-20201030165913-78d715cf8480
github.com/hashicorp/hcl-lang v0.0.0-20201106164120-ed150d21ec05
github.com/hashicorp/hcl/v2 v2.6.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20201021083235-edf96fe612be
github.com/hashicorp/terraform-json v0.6.0
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/zclconf/go-cty v1.6.1
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+d
github.com/hashicorp/go-version v1.2.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-20201030165913-78d715cf8480 h1:cT1ikuCDvhBXhnWehL/Abl6JVGvQPdob4D0GoQrVZ9o=
github.com/hashicorp/hcl-lang v0.0.0-20201030165913-78d715cf8480/go.mod h1:A0mcesqbT4EQkWw/CMBBp8pM5kl8wEekshClBJRGeUw=
github.com/hashicorp/hcl-lang v0.0.0-20201106164120-ed150d21ec05 h1:ww+H+xN5sPYDje6Dur2ChUSlHhGI6fBiZs/PIWmdplU=
github.com/hashicorp/hcl-lang v0.0.0-20201106164120-ed150d21ec05/go.mod h1:A0mcesqbT4EQkWw/CMBBp8pM5kl8wEekshClBJRGeUw=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.6.0 h1:3krZOfGY6SziUXa6H9PJU6TyohHn7I+ARYnhbeNBz+o=
github.com/hashicorp/hcl/v2 v2.6.0/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
Expand Down
56 changes: 56 additions & 0 deletions internal/schema/0.12/data_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
bs := &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.PlainText("Data Source Type"),
IsDepKey: true,
},
{
Name: "name",
Description: lang.PlainText("Reference Name"),
},
},
Description: lang.PlainText("A data block requests that Terraform read from a given data source and export the result " +
"under the given local name. The name is used to refer to this resource from elsewhere in the same " +
"Terraform module, but has no significance outside of the scope of a module."),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"provider": {
ValueType: cty.DynamicPseudoType,
Description: lang.Markdown("Reference to a `provider` configuration block, e.g. `mycloud.west` or `mycloud`"),
IsDepKey: true,
},
"count": {
ValueType: cty.Number,
Description: lang.Markdown("Number of instances of this data source, e.g. `3`"),
},
"depends_on": {
ValueType: cty.Set(cty.DynamicPseudoType),
Description: lang.Markdown("Set of references to hidden dependencies, e.g. other resources or data sources"),
},
},
},
}

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

return bs
}
11 changes: 11 additions & 0 deletions internal/schema/0.12/locals_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package schema

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

var localsBlockSchema = &schema.BlockSchema{
Description: lang.Markdown("Local values assigning names to expressions, so you can use these multiple times without repetition\n" +
"e.g. `service_name = \"forum\"`"),
}
35 changes: 35 additions & 0 deletions internal/schema/0.12/module_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package schema

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

var moduleBlockSchema = &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{
Name: "name",
Description: lang.PlainText("Reference Name"),
},
},
Description: lang.PlainText("TODO"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"source": {
ValueType: cty.String,
Description: lang.Markdown("TODO"),
IsRequired: true,
IsDepKey: true,
},
"version": {
ValueType: cty.String,
Description: lang.Markdown("TODO"),
},
"providers": {
ValueType: cty.Map(cty.DynamicPseudoType),
Description: lang.Markdown("TODO"),
},
},
},
}
38 changes: 38 additions & 0 deletions internal/schema/0.12/output_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package schema

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

var outputBlockSchema = &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{
Name: "name",
Description: lang.PlainText("Output Name"),
},
},
Description: lang.PlainText("Output value for consumption by another module or a human interacting via the UI"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"description": {
ValueType: cty.String,
Description: lang.PlainText("Human-readable description of the output (for documentation and UI)"),
},
"value": {
ValueType: cty.DynamicPseudoType,
IsRequired: true,
Description: lang.PlainText("Value, typically a reference to an attribute of a resource or a data source"),
},
"sensitive": {
ValueType: cty.Bool,
Description: lang.PlainText("Whether the output contains sensitive material and should be hidden in the UI"),
},
"depends_on": {
ValueType: cty.Set(cty.DynamicPseudoType),
Description: lang.PlainText("Set of references to hidden dependencies (e.g. resources or data sources)"),
},
},
},
}
33 changes: 33 additions & 0 deletions internal/schema/0.12/provider_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 providerBlockSchema(v *version.Version) *schema.BlockSchema {
return &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{
Name: "name",
Description: lang.PlainText("Provider Name"),
IsDepKey: true,
},
},
Description: lang.PlainText("A provider block is used to specify a provider configuration"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"alias": {
ValueType: cty.String,
Description: lang.Markdown("Alias for using the same provider with different configurations for different resources, e.g. `eu-west`"),
},
"version": {
ValueType: cty.String,
Description: lang.Markdown("Specifies a version constraint for the provider, e.g. `~> 1.0`"),
},
},
},
}
}
124 changes: 124 additions & 0 deletions internal/schema/0.12/resource_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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 resourceBlockSchema(v *version.Version) *schema.BlockSchema {
bs := &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.PlainText("Resource Type"),
IsDepKey: true,
},
{
Name: "name",
Description: lang.PlainText("Reference Name"),
},
},
Description: lang.PlainText("A resource block declares a resource of a given type with a given local name. The name is " +
"used to refer to this resource from elsewhere in the same Terraform module, but has no significance " +
"outside of the scope of a module."),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"provider": {
ValueType: cty.DynamicPseudoType,
Description: lang.Markdown("Reference to a `provider` configuration block, e.g. `mycloud.west` or `mycloud`"),
IsDepKey: true,
},
"count": {
ValueType: cty.Number,
Description: lang.Markdown("Number of instances of this resource, e.g. `3`"),
},
"depends_on": {
ValueType: cty.Set(cty.DynamicPseudoType),
Description: lang.Markdown("Set of references to hidden dependencies, e.g. other resources or data sources"),
},
},
Blocks: map[string]*schema.BlockSchema{
"lifecycle": lifecycleBlock,
"connection": connectionBlock,
},
},
}

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

return bs
}

var lifecycleBlock = &schema.BlockSchema{
Description: lang.Markdown("Lifecycle customizations to change default resource behaviours during apply"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"create_before_destroy": {
ValueType: cty.Bool,
Description: lang.Markdown("Whether to reverse the default order of operations (destroy -> create) during apply " +
"when the resource requires replacement (cannot be updated in-place)"),
},
"prevent_destroy": {
ValueType: cty.Bool,
Description: lang.Markdown("Whether to prevent accidental destruction of the resource and cause Terraform " +
"to reject with an error any plan that would destroy the resource"),
},
"ignore_changes": {
ValueType: cty.Set(cty.DynamicPseudoType),
Description: lang.Markdown("A set of fields (references) of which to ignore changes to, e.g. `tags`"),
},
},
},
}

var provisionerBlock = &schema.BlockSchema{
Description: lang.Markdown("Provisioner to model specific actions on the local machine or on a remote machine " +
"in order to prepare servers or other infrastructure objects for service"),
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.PlainText("Type of provisioner to use, e.g. `remote-exec` or `file`"),
IsDepKey: true,
},
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"when": {
ValueType: cty.DynamicPseudoType,
Description: lang.Markdown("When to run the provisioner - `create` or `destroy`, defaults to `create` " +
"(i.e. after creation of the resource)"),
},
"on_failure": {
ValueType: cty.DynamicPseudoType,
Description: lang.Markdown("What to do when the provisioner run fails to finish - `fail` (default), " +
"or `continue` (ignore the error)"),
},
},
Blocks: map[string]*schema.BlockSchema{
"connection": connectionBlock,
},
},
}

var connectionBlock = &schema.BlockSchema{
Description: lang.Markdown("Connection block describing how the provisioner connects to the given instance"),
MaxItems: 1,
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"type": {
ValueType: cty.String,
Description: lang.Markdown("Connection type to use - `ssh` (default) or `winrm`"),
},
},
},
}
27 changes: 27 additions & 0 deletions internal/schema/0.12/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package schema

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

var (
v0_12_6 = version.Must(version.NewVersion("0.12.6"))
v0_12_18 = version.Must(version.NewVersion("0.12.18"))
v0_12_20 = version.Must(version.NewVersion("0.12.20"))
)

func ModuleSchema(v *version.Version) *schema.BodySchema {
return &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"data": datasourceBlockSchema(v),
"locals": localsBlockSchema,
"module": moduleBlockSchema,
"output": outputBlockSchema,
"provider": providerBlockSchema(v),
"resource": resourceBlockSchema(v),
"variable": variableBlockSchema(v),
"terraform": terraformBlockSchema(v),
},
}
}
Loading

0 comments on commit 2a2e42c

Please sign in to comment.