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 5, 2020
1 parent 3b7b040 commit fdf63a2
Show file tree
Hide file tree
Showing 23 changed files with 1,039 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ require (
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
)
53 changes: 53 additions & 0 deletions internal/schema/0.12/data_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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{
Description: lang.Markdown("A set or a map where each item represents an instance of this data source"),
ValueType: cty.DynamicPseudoType,
}
}

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`"),
},
},
},
}
}
121 changes: 121 additions & 0 deletions internal/schema/0.12/resource_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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{
Description: lang.Markdown("A set or a map where each item represents an instance of this resource"),
ValueType: cty.DynamicPseudoType,
}
}

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),
},
}
}
64 changes: 64 additions & 0 deletions internal/schema/0.12/terraform_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 terraformBlockSchema(v *version.Version) *schema.BlockSchema {
bs := &schema.BlockSchema{
Description: lang.Markdown("TODO"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"required_version": {
ValueType: cty.String,
Description: lang.Markdown("TODO"),
},
},
Blocks: map[string]*schema.BlockSchema{
"backend": {
Labels: []*schema.LabelSchema{
{
Name: "type",
Description: lang.Markdown("TODO"),
IsDepKey: true,
},
},
},
"required_providers": {
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
ValueType: cty.String,
Description: lang.Markdown("Provider version constraint"),
},
},
},
},
},
}

if v.GreaterThanOrEqual(v0_12_18) {
bs.Body.Attributes["experiments"] = &schema.AttributeSchema{
ValueType: cty.Set(cty.DynamicPseudoType),
Description: lang.Markdown("TODO"),
}
}

if v.GreaterThanOrEqual(v0_12_20) {
bs.Body.Blocks["required_providers"] = &schema.BlockSchema{
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
ValueType: cty.Object(map[string]cty.Type{
"version": cty.String,
}),
// TODO: ValueTypes []cty.Type -> still allow plain cty.String
Description: lang.Markdown("Provider version constraint"),
},
},
}
}

return bs
}
Loading

0 comments on commit fdf63a2

Please sign in to comment.