-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvariable_schema.go
48 lines (39 loc) · 1.23 KB
/
variable_schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package schema
import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-schema/module"
"github.com/zclconf/go-cty/cty"
)
func SchemaForVariables(vars map[string]module.Variable) (*schema.BodySchema, error) {
return &schema.BodySchema{
Attributes: variablesToAttrSchemas(vars, schema.LiteralTypeOnly),
}, nil
}
type exprFunc func(cty.Type) schema.ExprConstraints
func variablesToAttrSchemas(vars map[string]module.Variable, exprFunc exprFunc) map[string]*schema.AttributeSchema {
varSchemas := make(map[string]*schema.AttributeSchema)
for name, v := range vars {
varType := v.Type
if (varType == cty.DynamicPseudoType || varType == cty.NilType) &&
v.DefaultValue != cty.NilVal {
// infer type from default value if one is not specified
// or when it's "any"
varType = v.DefaultValue.Type()
}
aSchema := &schema.AttributeSchema{
Expr: exprFunc(varType),
IsSensitive: v.IsSensitive,
}
if v.Description != "" {
aSchema.Description = lang.PlainText(v.Description)
}
varSchemas[name] = aSchema
if v.DefaultValue == cty.NilVal {
varSchemas[name].IsRequired = true
} else {
varSchemas[name].IsOptional = true
}
}
return varSchemas
}