/
attribute_validation.go
302 lines (252 loc) · 10.7 KB
/
attribute_validation.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package fwserver
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// AttributeValidate performs all Attribute validation.
//
// TODO: Clean up this abstraction back into an internal Attribute type method.
// The extra Attribute parameter is a carry-over of creating the proto6server
// package from the tfsdk package and not wanting to export the method.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/365
func AttributeValidate(ctx context.Context, a fwschema.Attribute, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {
ctx = logging.FrameworkWithAttributePath(ctx, req.AttributePath.String())
if (a.GetAttributes() == nil || len(a.GetAttributes().GetAttributes()) == 0) && a.GetType() == nil {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid Attribute Definition",
"Attribute must define either Attributes or Type. This is always a problem with the provider and should be reported to the provider developer.",
)
return
}
if a.GetAttributes() != nil && len(a.GetAttributes().GetAttributes()) > 0 && a.GetType() != nil {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid Attribute Definition",
"Attribute cannot define both Attributes and Type. This is always a problem with the provider and should be reported to the provider developer.",
)
return
}
if !a.IsRequired() && !a.IsOptional() && !a.IsComputed() {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid Attribute Definition",
"Attribute missing Required, Optional, or Computed definition. This is always a problem with the provider and should be reported to the provider developer.",
)
return
}
configData := &fwschemadata.Data{
Description: fwschemadata.DataDescriptionConfiguration,
Schema: req.Config.Schema,
TerraformValue: req.Config.Raw,
}
attributeConfig, diags := configData.ValueAtPath(ctx, req.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
// Terraform CLI does not automatically perform certain configuration
// checks yet. If it eventually does, this logic should remain at least
// until Terraform CLI versions 0.12 through the release containing the
// checks are considered end-of-life.
// Reference: https://github.com/hashicorp/terraform/issues/30669
if a.IsComputed() && !a.IsOptional() && !attributeConfig.IsNull() {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid Configuration for Read-Only Attribute",
"Cannot set value for this attribute as the provider has marked it as read-only. Remove the configuration line setting the value.\n\n"+
"Refer to the provider documentation or contact the provider developers for additional information about configurable and read-only attributes that are supported.",
)
}
// Terraform CLI does not automatically perform certain configuration
// checks yet. If it eventually does, this logic should remain at least
// until Terraform CLI versions 0.12 through the release containing the
// checks are considered end-of-life.
// Reference: https://github.com/hashicorp/terraform/issues/30669
if a.IsRequired() && attributeConfig.IsNull() {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Missing Configuration for Required Attribute",
fmt.Sprintf("Must set a configuration value for the %s attribute as the provider has marked it as required.\n\n", req.AttributePath.String())+
"Refer to the provider documentation or contact the provider developers for additional information about configurable attributes that are required.",
)
}
req.AttributeConfig = attributeConfig
if attributeWithValidators, ok := a.(fwxschema.AttributeWithValidators); ok {
for _, validator := range attributeWithValidators.GetValidators() {
logging.FrameworkDebug(
ctx,
"Calling provider defined AttributeValidator",
map[string]interface{}{
logging.KeyDescription: validator.Description(ctx),
},
)
validator.Validate(ctx, req, resp)
logging.FrameworkDebug(
ctx,
"Called provider defined AttributeValidator",
map[string]interface{}{
logging.KeyDescription: validator.Description(ctx),
},
)
}
}
AttributeValidateNestedAttributes(ctx, a, req, resp)
// Show deprecation warnings only for known values.
if a.GetDeprecationMessage() != "" && !attributeConfig.IsNull() && !attributeConfig.IsUnknown() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Attribute Deprecated",
a.GetDeprecationMessage(),
)
}
}
// AttributeValidateNestedAttributes performs all nested Attributes validation.
//
// TODO: Clean up this abstraction back into an internal Attribute type method.
// The extra Attribute parameter is a carry-over of creating the proto6server
// package from the tfsdk package and not wanting to export the method.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/365
func AttributeValidateNestedAttributes(ctx context.Context, a fwschema.Attribute, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {
if a.GetAttributes() == nil || len(a.GetAttributes().GetAttributes()) == 0 {
return
}
nm := a.GetAttributes().GetNestingMode()
switch nm {
case fwschema.NestingModeList:
listVal, ok := req.AttributeConfig.(types.ListVal)
if !ok {
err := fmt.Errorf("unknown attribute value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Attribute Validation Error Invalid Value Type",
"A type from which a types.List can be obtained is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
l, diags := listVal.ToFrameworkValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for idx := range l.Elements() {
for nestedName, nestedAttr := range a.GetAttributes().GetAttributes() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtListIndex(idx).AtName(nestedName),
AttributePathExpression: req.AttributePathExpression.AtListIndex(idx).AtName(nestedName),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, nestedAttr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
case fwschema.NestingModeSet:
setVal, ok := req.AttributeConfig.(types.SetVal)
if !ok {
err := fmt.Errorf("unknown attribute value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Attribute Validation Error Invalid Value Type",
"A type from which a types.Set can be obtained is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
s, diags := setVal.ToFrameworkValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for _, value := range s.Elements() {
for nestedName, nestedAttr := range a.GetAttributes().GetAttributes() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtSetValue(value).AtName(nestedName),
AttributePathExpression: req.AttributePathExpression.AtSetValue(value).AtName(nestedName),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, nestedAttr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
case fwschema.NestingModeMap:
mapVal, ok := req.AttributeConfig.(types.MapVal)
if !ok {
err := fmt.Errorf("unknown attribute value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Attribute Validation Error Invalid Value Type",
"A type from which a types.Map can be obtained is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
m, diags := mapVal.ToFrameworkValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for key := range m.Elements() {
for nestedName, nestedAttr := range a.GetAttributes().GetAttributes() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtMapKey(key).AtName(nestedName),
AttributePathExpression: req.AttributePathExpression.AtMapKey(key).AtName(nestedName),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, nestedAttr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
case fwschema.NestingModeSingle:
objectVal, ok := req.AttributeConfig.(types.ObjectVal)
if !ok {
err := fmt.Errorf("unknown attribute value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Attribute Validation Error Invalid Value Type",
"A type from which a types.Object can be obtained is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
o, diags := objectVal.ToFrameworkValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if o.IsNull() || o.IsUnknown() {
return
}
for nestedName, nestedAttr := range a.GetAttributes().GetAttributes() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtName(nestedName),
AttributePathExpression: req.AttributePathExpression.AtName(nestedName),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, nestedAttr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
default:
err := fmt.Errorf("unknown attribute validation nesting mode (%T: %v) at path: %s", nm, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Attribute Validation Error",
"Attribute validation cannot walk schema. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
}