Debian Codegraph @

tfconfig/provider_ref.go

raw
1 // Copyright (c) HashiCorp, Inc.
2 // SPDX-License-Identifier: MPL-2.0
3
4 package tfconfig
5
6 import (
7 "fmt"
8
9 "github.com/hashicorp/hcl/v2"
10 "github.com/hashicorp/hcl/v2/gohcl"
11 "github.com/zclconf/go-cty/cty"
12 )
13
14 // ProviderRef is a reference to a provider configuration within a module.
15 // It represents the contents of a "provider" argument in a resource, or
16 // a value in the "providers" map for a module call.
17 type ProviderRef struct {
18 Name string `json:"name"`
19 Alias string `json:"alias,omitempty"` // Empty if the default provider configuration is referenced
20 }
21
22 type ProviderRequirement struct {
23 Source string `json:"source,omitempty"`
24 VersionConstraints []string `json:"version_constraints,omitempty"`
25 ConfigurationAliases []ProviderRef `json:"aliases,omitempty"`
26 }
27
28 func decodeRequiredProvidersBlock(block *hcl.Block) (map[string]*ProviderRequirement, hcl.Diagnostics) {
29 attrs, diags := block.Body.JustAttributes()
30 reqs := make(map[string]*ProviderRequirement)
31 for name, attr := range attrs {
32 // Look for a legacy version in the attribute first
33 if expr, err := attr.Expr.Value(nil); err == nil && expr.Type().IsPrimitiveType() {
34 var version string
35 valDiags := gohcl.DecodeExpression(attr.Expr, nil, &version)
36 diags = append(diags, valDiags...)
37 if !valDiags.HasErrors() {
38 reqs[name] = &ProviderRequirement{
39 VersionConstraints: []string{version},
40 }
41 }
42 continue
43 }
44
45 kvs, mapDiags := hcl.ExprMap(attr.Expr)
46 if mapDiags.HasErrors() {
47 diags = append(diags, &hcl.Diagnostic{
48 Severity: hcl.DiagError,
49 Summary: "Invalid required_providers object",
50 Detail: "Required providers entries must be strings or objects.",
51 Subject: attr.Expr.Range().Ptr(),
52 })
53 continue
54 }
55
56 var pr ProviderRequirement
57
58 for _, kv := range kvs {
59 key, keyDiags := kv.Key.Value(nil)
60 if keyDiags.HasErrors() {
61 diags = append(diags, keyDiags...)
62 continue
63 }
64
65 if key.Type() != cty.String {
66 diags = append(diags, &hcl.Diagnostic{
67 Severity: hcl.DiagError,
68 Summary: "Invalid Attribute",
69 Detail: fmt.Sprintf("Invalid attribute value for provider requirement: %#v", key),
70 Subject: kv.Key.Range().Ptr(),
71 })
72 continue
73 }
74
75 switch key.AsString() {
76 case "version":
77 version, valDiags := kv.Value.Value(nil)
78 if valDiags.HasErrors() || !version.Type().Equals(cty.String) {
79 diags = append(diags, &hcl.Diagnostic{
80 Severity: hcl.DiagError,
81 Summary: "Unsuitable value type",
82 Detail: "Unsuitable value: string required",
83 Subject: attr.Expr.Range().Ptr(),
84 })
85 continue
86 }
87 if !version.IsNull() {
88 pr.VersionConstraints = append(pr.VersionConstraints, version.AsString())
89 }
90
91 case "source":
92 source, valDiags := kv.Value.Value(nil)
93 if valDiags.HasErrors() || !source.Type().Equals(cty.String) {
94 diags = append(diags, &hcl.Diagnostic{
95 Severity: hcl.DiagError,
96 Summary: "Unsuitable value type",
97 Detail: "Unsuitable value: string required",
98 Subject: attr.Expr.Range().Ptr(),
99 })
100 continue
101 }
102
103 if !source.IsNull() {
104 pr.Source = source.AsString()
105 }
106 case "configuration_aliases":
107 aliases, valDiags := decodeConfigurationAliases(name, kv.Value)
108 if valDiags.HasErrors() {
109 diags = append(diags, valDiags...)
110 continue
111 }
112 pr.ConfigurationAliases = append(pr.ConfigurationAliases, aliases...)
113 }
114
115 reqs[name] = &pr
116 }
117 }
118
119 return reqs, diags
120 }
121
122 func decodeConfigurationAliases(localName string, value hcl.Expression) ([]ProviderRef, hcl.Diagnostics) {
123 aliases := make([]ProviderRef, 0)
124 var diags hcl.Diagnostics
125
126 exprs, listDiags := hcl.ExprList(value)
127 if listDiags.HasErrors() {
128 diags = append(diags, listDiags...)
129 return aliases, diags
130 }
131
132 for _, expr := range exprs {
133 traversal, travDiags := hcl.AbsTraversalForExpr(expr)
134 if travDiags.HasErrors() {
135 diags = append(diags, travDiags...)
136 continue
137 }
138
139 ref, cfgDiags := parseProviderRef(traversal)
140 if cfgDiags.HasErrors() {
141 diags = append(diags, &hcl.Diagnostic{
142 Severity: hcl.DiagError,
143 Summary: "Invalid configuration_aliases value",
144 Detail: `Configuration aliases can only contain references to local provider configuration names in the format of provider.alias`,
145 Subject: value.Range().Ptr(),
146 })
147 continue
148 }
149
150 if ref.Name != localName {
151 diags = append(diags, &hcl.Diagnostic{
152 Severity: hcl.DiagError,
153 Summary: "Invalid configuration_aliases value",
154 Detail: fmt.Sprintf(`Configuration aliases must be prefixed with the provider name. Expected %q, but found %q.`, localName, ref.Name),
155 Subject: value.Range().Ptr(),
156 })
157 continue
158 }
159
160 aliases = append(aliases, ref)
161 }
162
163 return aliases, diags
164 }
165
166 func parseProviderRef(traversal hcl.Traversal) (ProviderRef, hcl.Diagnostics) {
167 var diags hcl.Diagnostics
168 ret := ProviderRef{
169 Name: traversal.RootName(),
170 }
171
172 if len(traversal) < 2 {
173 // Just a local name, then.
174 return ret, diags
175 }
176
177 aliasStep := traversal[1]
178 switch ts := aliasStep.(type) {
179 case hcl.TraverseAttr:
180 ret.Alias = ts.Name
181 return ret, diags
182 default:
183 diags = diags.Append(&hcl.Diagnostic{
184 Severity: hcl.DiagError,
185 Summary: "Invalid provider configuration address",
186 Detail: "The provider type name must either stand alone or be followed by an alias name separated with a dot.",
187 Subject: aliasStep.SourceRange().Ptr(),
188 })
189 }
190
191 if len(traversal) > 2 {
192 diags = diags.Append(&hcl.Diagnostic{
193 Severity: hcl.DiagError,
194 Summary: "Invalid provider configuration address",
195 Detail: "Extraneous extra operators after provider configuration address.",
196 Subject: traversal[2:].SourceRange().Ptr(),
197 })
198 }
199
200 return ret, diags
201 }