-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmodule_schema.go
126 lines (106 loc) · 3.11 KB
/
module_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package schema
import (
"sort"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/hashicorp/terraform-schema/module"
"github.com/zclconf/go-cty/cty"
)
func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*schema.BodySchema, error) {
attributes := make(map[string]*schema.AttributeSchema, 0)
for name, modVar := range modMeta.Variables {
aSchema := moduleVarToAttribute(modVar)
varType := typeOfModuleVar(modVar)
aSchema.Expr = convertAttributeTypeToExprConstraints(varType)
aSchema.OriginForTarget = &schema.PathTarget{
Address: schema.Address{
schema.StaticStep{Name: "var"},
schema.AttrNameStep{},
},
Path: lang.Path{
Path: modMeta.Path,
LanguageID: ModuleLanguageID,
},
Constraints: schema.Constraints{
ScopeId: refscope.VariableScope,
Type: varType,
},
}
attributes[name] = aSchema
}
bodySchema := &schema.BodySchema{
Attributes: attributes,
}
if localName == "" {
// avoid creating output refs if we don't have reference name
return bodySchema, nil
}
modOutputTypes := make(map[string]cty.Type, 0)
modOutputVals := make(map[string]cty.Value, 0)
targetableOutputs := make(schema.Targetables, 0)
for name, output := range modMeta.Outputs {
addr := lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: localName},
lang.AttrStep{Name: name},
}
typ := cty.DynamicPseudoType
if !output.Value.IsNull() {
typ = output.Value.Type()
}
targetable := &schema.Targetable{
Address: addr,
ScopeId: refscope.ModuleScope,
AsType: typ,
IsSensitive: output.IsSensitive,
NestedTargetables: schema.NestedTargetablesForValue(addr, refscope.ModuleScope, output.Value),
}
if output.Description != "" {
targetable.Description = lang.PlainText(output.Description)
}
targetableOutputs = append(targetableOutputs, targetable)
modOutputTypes[name] = typ
modOutputVals[name] = output.Value
}
sort.Sort(targetableOutputs)
addr := lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: localName},
}
bodySchema.TargetableAs = append(bodySchema.TargetableAs, &schema.Targetable{
Address: addr,
ScopeId: refscope.ModuleScope,
AsType: cty.Object(modOutputTypes),
NestedTargetables: targetableOutputs,
})
if len(modMeta.Filenames) > 0 {
filename := modMeta.Filenames[0]
// Prioritize main.tf based on best practices as documented at
// https://learn.hashicorp.com/tutorials/terraform/module-create
if sliceContains(modMeta.Filenames, "main.tf") {
filename = "main.tf"
}
bodySchema.Targets = &schema.Target{
Path: lang.Path{
Path: modMeta.Path,
LanguageID: "terraform",
},
Range: hcl.Range{
Filename: filename,
Start: hcl.InitialPos,
End: hcl.InitialPos,
},
}
}
return bodySchema, nil
}
func sliceContains(slice []string, value string) bool {
for _, val := range slice {
if val == value {
return true
}
}
return false
}