-
Notifications
You must be signed in to change notification settings - Fork 21
/
provider_requirements.go
194 lines (168 loc) · 5.28 KB
/
provider_requirements.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package earlydecoder
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/terraform-schema/module"
"github.com/zclconf/go-cty/cty"
)
type providerRequirement struct {
Source string
VersionConstraints []string
ConfigurationAliases []module.ProviderRef
}
func decodeRequiredProvidersBlock(block *hcl.Block) (map[string]*providerRequirement, hcl.Diagnostics) {
attrs, diags := block.Body.JustAttributes()
reqs := make(map[string]*providerRequirement)
for name, attr := range attrs {
// Look for a legacy version in the attribute first
if expr, err := attr.Expr.Value(nil); err == nil && expr.Type().IsPrimitiveType() {
var version string
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &version)
diags = append(diags, valDiags...)
if !valDiags.HasErrors() {
reqs[name] = &providerRequirement{
VersionConstraints: []string{version},
}
}
continue
}
kvs, mapDiags := hcl.ExprMap(attr.Expr)
if mapDiags.HasErrors() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid required_providers object",
Detail: "Required providers entries must be strings or objects.",
Subject: attr.Expr.Range().Ptr(),
})
continue
}
var pr providerRequirement
for _, kv := range kvs {
key, keyDiags := kv.Key.Value(nil)
if keyDiags.HasErrors() {
diags = append(diags, keyDiags...)
continue
}
if key.Type() != cty.String {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid Attribute",
Detail: fmt.Sprintf("Invalid attribute value for provider requirement: %#v", key),
Subject: kv.Key.Range().Ptr(),
})
continue
}
switch key.AsString() {
case "version":
version, valDiags := kv.Value.Value(nil)
if valDiags.HasErrors() || !version.Type().Equals(cty.String) {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable value type",
Detail: "Unsuitable value: string required",
Subject: attr.Expr.Range().Ptr(),
})
continue
}
if !version.IsNull() {
pr.VersionConstraints = append(pr.VersionConstraints, version.AsString())
}
case "source":
source, valDiags := kv.Value.Value(nil)
if valDiags.HasErrors() || !source.Type().Equals(cty.String) {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable value type",
Detail: "Unsuitable value: string required",
Subject: attr.Expr.Range().Ptr(),
})
continue
}
if !source.IsNull() {
pr.Source = source.AsString()
}
case "configuration_aliases":
aliases, valDiags := decodeConfigurationAliases(name, kv.Value)
if valDiags.HasErrors() {
diags = append(diags, valDiags...)
continue
}
pr.ConfigurationAliases = append(pr.ConfigurationAliases, aliases...)
}
reqs[name] = &pr
}
}
return reqs, diags
}
func decodeConfigurationAliases(localName string, value hcl.Expression) ([]module.ProviderRef, hcl.Diagnostics) {
aliases := make([]module.ProviderRef, 0)
var diags hcl.Diagnostics
exprs, listDiags := hcl.ExprList(value)
if listDiags.HasErrors() {
diags = append(diags, listDiags...)
return aliases, diags
}
for _, expr := range exprs {
traversal, travDiags := hcl.AbsTraversalForExpr(expr)
if travDiags.HasErrors() {
diags = append(diags, travDiags...)
continue
}
ref, cfgDiags := parseProviderRef(traversal)
if cfgDiags.HasErrors() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid configuration_aliases value",
Detail: `Configuration aliases can only contain references to local provider configuration names in the format of provider.alias`,
Subject: value.Range().Ptr(),
})
continue
}
if ref.LocalName != localName {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid configuration_aliases value",
Detail: fmt.Sprintf(`Configuration aliases must be prefixed with the provider name. Expected %q, but found %q.`, localName, ref.LocalName),
Subject: value.Range().Ptr(),
})
continue
}
aliases = append(aliases, ref)
}
return aliases, diags
}
func parseProviderRef(traversal hcl.Traversal) (module.ProviderRef, hcl.Diagnostics) {
var diags hcl.Diagnostics
ret := module.ProviderRef{
LocalName: traversal.RootName(),
}
if len(traversal) < 2 {
// Just a local name, then.
return ret, diags
}
aliasStep := traversal[1]
switch ts := aliasStep.(type) {
case hcl.TraverseAttr:
ret.Alias = ts.Name
return ret, diags
default:
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid provider configuration address",
Detail: "The provider type name must either stand alone or be followed by an alias name separated with a dot.",
Subject: aliasStep.SourceRange().Ptr(),
})
}
if len(traversal) > 2 {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid provider configuration address",
Detail: "Extraneous extra operators after provider configuration address.",
Subject: traversal[2:].SourceRange().Ptr(),
})
}
return ret, diags
}