Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to create schema for variables #49

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions schema/variable_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package schema

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

func SchemaForVariables(vars map[string]module.Variable) (*schema.BodySchema, error) {
varSchemas := make(map[string]*schema.AttributeSchema)

for name, v := range vars {
varSchemas[name] = &schema.AttributeSchema{
Description: lang.MarkupContent{
Value: v.Description,
Kind: lang.PlainTextKind,
},
Expr: schema.ExprConstraints{schema.LiteralTypeExpr{Type: v.Type}},
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting for support for sensitive attributes (hashicorp/hcl-lang#42)

}

return &schema.BodySchema{
Attributes: varSchemas,
}, nil
}
89 changes: 89 additions & 0 deletions schema/variable_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package schema

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/module"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)

func TestSchemaForVariables(t *testing.T) {
testCases := []struct {
name string
variables map[string]module.Variable
expectedSchema *schema.BodySchema
}{
{
"empty schema",
make(map[string]module.Variable),
&schema.BodySchema{Attributes: make(map[string]*schema.AttributeSchema)},
},
{
"one attribute schema",
map[string]module.Variable{
"name": module.Variable{
Description: "name of the module",
Type: cty.String,
},
},
&schema.BodySchema{Attributes: map[string]*schema.AttributeSchema{
"name": &schema.AttributeSchema{
Description: lang.MarkupContent{
Value: "name of the module",
Kind: lang.PlainTextKind,
},
Expr: schema.ExprConstraints{schema.LiteralTypeExpr{cty.String}},
},
}},
},
{
"two attribute schema",
map[string]module.Variable{
"name": module.Variable{
Description: "name of the module",
Type: cty.String,
},
"id": module.Variable{
Description: "id of the module",
Type: cty.Number,
},
},
&schema.BodySchema{Attributes: map[string]*schema.AttributeSchema{
"name": &schema.AttributeSchema{
Description: lang.MarkupContent{
Value: "name of the module",
Kind: lang.PlainTextKind,
},
Expr: schema.ExprConstraints{schema.LiteralTypeExpr{cty.String}},
},
"id": &schema.AttributeSchema{
Description: lang.MarkupContent{
Value: "id of the module",
Kind: lang.PlainTextKind,
},
Expr: schema.ExprConstraints{schema.LiteralTypeExpr{cty.Number}},
},
}},
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
actualSchema, err := SchemaForVariables(tc.variables)

if err != nil {
t.Fatal(err)
}

diff := cmp.Diff(tc.expectedSchema, actualSchema, ctydebug.CmpOptions)
if diff != "" {
t.Fatalf("unexpected schema %s", diff)
}
})
}
}