Skip to content

Commit

Permalink
export Default to custom schema to help eliminate Optional attribut…
Browse files Browse the repository at this point in the history
…e holding the default value
  • Loading branch information
magodo committed Aug 21, 2021
1 parent ef1e526 commit ddc91d6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
install:
@go install
gen:
@./tools/generate-provider-schema/run.sh $(PROVIDER_DIR) $(PROVIDER_VERSION)
53 changes: 48 additions & 5 deletions internal/hcl_schema.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
package internal

import (
"fmt"
"github.com/hashicorp/hcl/v2"
"strings"

"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/magodo/aztfy/schema"
"github.com/zclconf/go-cty/cty"
)

// Currently, some special attributes are output by `terraform add`, while should be excluded.
// This is tracked in: https://github.com/hashicorp/terraform/issues/29219
// We are manually excluding these special properties here by modifying the hcl.
func tuneHCLSchemaForResource(rb *hclwrite.Body, sch *schema.Schema) {
func tuneHCLSchemaForResource(rb *hclwrite.Body, sch *schema.Schema) error {
rb.RemoveAttribute("id")
rb.RemoveBlock(rb.FirstMatchingBlock("timeouts", nil))
removeComputedForBody(rb, sch.Block)
return removeComputedForBody(rb, sch.Block)
}
func removeComputedForBody(rb *hclwrite.Body, sch *schema.SchemaBlock) {
for attrName := range rb.Attributes() {
if sch.Attributes[attrName].Computed {
func removeComputedForBody(rb *hclwrite.Body, sch *schema.SchemaBlock) error {
for attrName, attrVal := range rb.Attributes() {
schAttr := sch.Attributes[attrName]
if schAttr.Required {
continue
}

if schAttr.Computed {
rb.RemoveAttribute(attrName)
continue
}

// For optional only attributes, remove it from the output config if it holds the default value
var dstr string
switch schAttr.AttributeType {
case cty.Number:
dstr = "0"
case cty.Bool:
dstr = "false"
case cty.String:
dstr = `""`
default:
if schAttr.AttributeType.IsListType() || schAttr.AttributeType.IsSetType() {
dstr = "[]"
break
}
if schAttr.AttributeType.IsMapType() {
dstr = "{}"
break
}
}
if schAttr.Default != nil {
dstr = fmt.Sprintf("%#v", schAttr.Default)
}
attrExpr, diags := hclwrite.ParseConfig(attrVal.BuildTokens(nil).Bytes(), "generate_attr", hcl.InitialPos)
if diags.HasErrors() {
return fmt.Errorf(`building attribute %q attribute: %s`, attrName, diags.Error())
}
attrValLit := strings.TrimSpace(string(attrExpr.Body().GetAttribute(attrName).Expr().BuildTokens(nil).Bytes()))
if attrValLit == dstr {
rb.RemoveAttribute(attrName)
continue
}
Expand All @@ -28,4 +70,5 @@ func removeComputedForBody(rb *hclwrite.Body, sch *schema.SchemaBlock) {
}
removeComputedForBody(blkVal.Body(), sch.NestedBlocks[blkVal.Type()].Block)
}
return nil
}
1 change: 1 addition & 0 deletions schema/core_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func fromProviderSchemaAttribute(ps *schema.Schema) *SchemaAttribute {
Optional: opt,
Required: reqd,
Computed: ps.Computed,
Default: ps.Default,
}
}

Expand Down
2 changes: 1 addition & 1 deletion schema/provider_gen.go

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/zclconf/go-cty/cty"
// This is a simplified and modified version of the hashicorp/terraform-json.
// The motivation for this is to add more information that is lost during the conversion from plugin sdk (v2) to the terraform core schema.
// (github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema/core_schema.go)
// Specifically, we are adding the Required, Optional, Computed for the SchemaBlockType, and removing any other attributes.
// Specifically, we are adding the Required, Optional, Computed for the SchemaBlockType, adding Defualt for the SchemaAttribute, and removing any other attributes.

type ProviderSchema struct {
ResourceSchemas map[string]*Schema `json:"resource_schemas,omitempty"`
Expand All @@ -32,7 +32,8 @@ type SchemaBlockType struct {
type SchemaAttribute struct {
AttributeType cty.Type `json:"type,omitempty"`

Required bool `json:"required,omitempty"`
Optional bool `json:"optional,omitempty"`
Computed bool `json:"computed,omitempty"`
Required bool `json:"required,omitempty"`
Optional bool `json:"optional,omitempty"`
Computed bool `json:"computed,omitempty"`
Default interface{} `json:"default,omitempty"`
}

0 comments on commit ddc91d6

Please sign in to comment.