-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdecoder.go
176 lines (154 loc) · 4.52 KB
/
decoder.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package earlydecoder
import (
"fmt"
"sort"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/module"
)
func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diagnostics) {
var diags hcl.Diagnostics
filenames := make([]string, 0)
mod := newDecodedModule()
for filename, f := range files {
filenames = append(filenames, filename)
fDiags := loadModuleFromFile(f, mod)
diags = append(diags, fDiags...)
}
sort.Strings(filenames)
var coreRequirements version.Constraints
for _, rc := range mod.RequiredCore {
c, err := version.NewConstraint(rc)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unable to parse terraform requirements",
Detail: fmt.Sprintf("Constraint %q is not a valid constraint: %s", rc, err),
})
continue
}
coreRequirements = append(coreRequirements, c...)
}
var backend *module.Backend
if len(mod.Backends) == 1 {
for bType, data := range mod.Backends {
backend = &module.Backend{
Type: bType,
Data: data,
}
}
} else if len(mod.Backends) > 1 {
backendTypes := make([]string, len(mod.Backends))
for bType := range mod.Backends {
backendTypes = append(backendTypes, bType)
}
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unable to parse backend configuration",
Detail: fmt.Sprintf("Multiple backend definitions: %q", backendTypes),
})
}
var (
providerRequirements = make(map[tfaddr.Provider]version.Constraints, 0)
refs = make(map[module.ProviderRef]tfaddr.Provider, 0)
)
for name, req := range mod.ProviderRequirements {
var src tfaddr.Provider
if req.Source == "" {
if name == "" {
continue
}
src = tfaddr.NewLegacyProvider(name)
} else {
var err error
src, err = tfaddr.ParseRawProviderSourceString(req.Source)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Unable to parse provider source for %q", name),
Detail: fmt.Sprintf("%q provider source (%q) is not a valid source string", name, req.Source),
})
continue
}
}
constraints := make(version.Constraints, 0)
for _, vc := range req.VersionConstraints {
c, err := version.NewConstraint(vc)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Unable to parse %q provider requirements", name),
Detail: fmt.Sprintf("Constraint %q is not a valid constraint: %s", vc, err),
})
continue
}
constraints = append(constraints, c...)
}
providerRequirements[src] = constraints
refs[module.ProviderRef{
LocalName: name,
}] = src
for _, alias := range req.ConfigurationAliases {
refs[module.ProviderRef{
LocalName: alias.LocalName,
Alias: alias.Alias,
}] = src
}
}
for _, cfg := range mod.ProviderConfigs {
src := refs[module.ProviderRef{
LocalName: cfg.Name,
}]
if cfg.Alias != "" {
refs[module.ProviderRef{
LocalName: cfg.Name,
Alias: cfg.Alias,
}] = src
}
}
for _, resource := range mod.Resources {
providerName := resource.Provider.LocalName
localRef := module.ProviderRef{
LocalName: providerName,
}
if _, exists := refs[localRef]; !exists && providerName != "" {
src := tfaddr.NewLegacyProvider(providerName)
if _, exists := providerRequirements[src]; !exists {
providerRequirements[src] = version.Constraints{}
}
refs[localRef] = src
}
}
for _, dataSource := range mod.DataSources {
providerName := dataSource.Provider.LocalName
localRef := module.ProviderRef{
LocalName: providerName,
}
if _, exists := refs[localRef]; !exists && providerName != "" {
src := tfaddr.NewLegacyProvider(providerName)
if _, exists := providerRequirements[src]; !exists {
providerRequirements[src] = version.Constraints{}
}
refs[localRef] = src
}
}
variables := make(map[string]module.Variable)
for key, variable := range mod.Variables {
variables[key] = *variable
}
outputs := make(map[string]module.Output)
for key, output := range mod.Outputs {
outputs[key] = *output
}
return &module.Meta{
Path: path,
Backend: backend,
ProviderReferences: refs,
ProviderRequirements: providerRequirements,
CoreRequirements: coreRequirements,
Variables: variables,
Outputs: outputs,
Filenames: filenames,
}, diags
}