-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathblock_validation.go
453 lines (366 loc) · 14.6 KB
/
block_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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
// BlockValidate performs all Block validation.
//
// TODO: Clean up this abstraction back into an internal Block type method.
// The extra Block 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 BlockValidate(ctx context.Context, b fwschema.Block, req ValidateAttributeRequest, resp *ValidateAttributeResponse) {
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
}
req.AttributeConfig = attributeConfig
switch blockWithValidators := b.(type) {
case fwxschema.BlockWithListValidators:
BlockValidateList(ctx, blockWithValidators, req, resp)
case fwxschema.BlockWithObjectValidators:
BlockValidateObject(ctx, blockWithValidators, req, resp)
case fwxschema.BlockWithSetValidators:
BlockValidateSet(ctx, blockWithValidators, req, resp)
}
nestedBlockObject := b.GetNestedObject()
nm := b.GetNestingMode()
switch nm {
case fwschema.BlockNestingModeList:
listVal, ok := req.AttributeConfig.(basetypes.ListValuable)
if !ok {
err := fmt.Errorf("unknown block value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error Invalid Value Type",
"A type that implements basetypes.ListValuable is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
l, diags := listVal.ToListValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for idx, value := range l.Elements() {
nestedBlockObjectReq := ValidateAttributeRequest{
AttributeConfig: value,
AttributePath: req.AttributePath.AtListIndex(idx),
AttributePathExpression: req.AttributePathExpression.AtListIndex(idx),
Config: req.Config,
}
nestedBlockObjectResp := &ValidateAttributeResponse{}
NestedBlockObjectValidate(ctx, nestedBlockObject, nestedBlockObjectReq, nestedBlockObjectResp)
resp.Diagnostics.Append(nestedBlockObjectResp.Diagnostics...)
}
case fwschema.BlockNestingModeSet:
setVal, ok := req.AttributeConfig.(basetypes.SetValuable)
if !ok {
err := fmt.Errorf("unknown block value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error Invalid Value Type",
"A type that implements basetypes.SetValuable is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
s, diags := setVal.ToSetValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for _, value := range s.Elements() {
nestedBlockObjectReq := ValidateAttributeRequest{
AttributeConfig: value,
AttributePath: req.AttributePath.AtSetValue(value),
AttributePathExpression: req.AttributePathExpression.AtSetValue(value),
Config: req.Config,
}
nestedBlockObjectResp := &ValidateAttributeResponse{}
NestedBlockObjectValidate(ctx, nestedBlockObject, nestedBlockObjectReq, nestedBlockObjectResp)
resp.Diagnostics.Append(nestedBlockObjectResp.Diagnostics...)
}
case fwschema.BlockNestingModeSingle:
objectVal, ok := req.AttributeConfig.(basetypes.ObjectValuable)
if !ok {
err := fmt.Errorf("unknown block value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error Invalid Value Type",
"A type that implements basetypes.ObjectValuable is expected here. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
o, diags := objectVal.ToObjectValue(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
nestedBlockObjectReq := ValidateAttributeRequest{
AttributeConfig: o,
AttributePath: req.AttributePath,
AttributePathExpression: req.AttributePathExpression,
Config: req.Config,
}
nestedBlockObjectResp := &ValidateAttributeResponse{}
NestedBlockObjectValidate(ctx, nestedBlockObject, nestedBlockObjectReq, nestedBlockObjectResp)
resp.Diagnostics.Append(nestedBlockObjectResp.Diagnostics...)
default:
err := fmt.Errorf("unknown block validation nesting mode (%T: %v) at path: %s", nm, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error",
"Block validation cannot walk schema. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
// Show deprecation warning only on known values.
if b.GetDeprecationMessage() != "" && !attributeConfig.IsNull() && !attributeConfig.IsUnknown() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Block Deprecated",
b.GetDeprecationMessage(),
)
}
}
// BlockValidateList performs all types.List validation.
func BlockValidateList(ctx context.Context, block fwxschema.BlockWithListValidators, req ValidateAttributeRequest, resp *ValidateAttributeResponse) {
// Use basetypes.ListValuable until custom types cannot re-implement
// ValueFromTerraform. Until then, custom types are not technically
// required to implement this interface. This opts to enforce the
// requirement before compatibility promises would interfere.
configValuable, ok := req.AttributeConfig.(basetypes.ListValuable)
if !ok {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid List Attribute Validator Value Type",
"An unexpected value type was encountered while attempting to perform List attribute validation. "+
"The value type must implement the basetypes.ListValuable interface. "+
"Please report this to the provider developers.\n\n"+
fmt.Sprintf("Incoming Value Type: %T", req.AttributeConfig),
)
return
}
configValue, diags := configValuable.ToListValue(ctx)
resp.Diagnostics.Append(diags...)
// Only return early on new errors as the resp.Diagnostics may have errors
// from other attributes.
if diags.HasError() {
return
}
validateReq := validator.ListRequest{
Config: req.Config,
ConfigValue: configValue,
Path: req.AttributePath,
PathExpression: req.AttributePathExpression,
}
for _, blockValidator := range block.ListValidators() {
// Instantiate a new response for each request to prevent validators
// from modifying or removing diagnostics.
validateResp := &validator.ListResponse{}
logging.FrameworkDebug(
ctx,
"Calling provider defined validator.List",
map[string]interface{}{
logging.KeyDescription: blockValidator.Description(ctx),
},
)
blockValidator.ValidateList(ctx, validateReq, validateResp)
logging.FrameworkDebug(
ctx,
"Called provider defined validator.List",
map[string]interface{}{
logging.KeyDescription: blockValidator.Description(ctx),
},
)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
// BlockValidateObject performs all types.Object validation.
func BlockValidateObject(ctx context.Context, block fwxschema.BlockWithObjectValidators, req ValidateAttributeRequest, resp *ValidateAttributeResponse) {
// Use basetypes.ObjectValuable until custom types cannot re-implement
// ValueFromTerraform. Until then, custom types are not technically
// required to implement this interface. This opts to enforce the
// requirement before compatibility promises would interfere.
configValuable, ok := req.AttributeConfig.(basetypes.ObjectValuable)
if !ok {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid Object Attribute Validator Value Type",
"An unexpected value type was encountered while attempting to perform Object attribute validation. "+
"The value type must implement the basetypes.ObjectValuable interface. "+
"Please report this to the provider developers.\n\n"+
fmt.Sprintf("Incoming Value Type: %T", req.AttributeConfig),
)
return
}
configValue, diags := configValuable.ToObjectValue(ctx)
resp.Diagnostics.Append(diags...)
// Only return early on new errors as the resp.Diagnostics may have errors
// from other attributes.
if diags.HasError() {
return
}
validateReq := validator.ObjectRequest{
Config: req.Config,
ConfigValue: configValue,
Path: req.AttributePath,
PathExpression: req.AttributePathExpression,
}
for _, blockValidator := range block.ObjectValidators() {
// Instantiate a new response for each request to prevent validators
// from modifying or removing diagnostics.
validateResp := &validator.ObjectResponse{}
logging.FrameworkDebug(
ctx,
"Calling provider defined validator.Object",
map[string]interface{}{
logging.KeyDescription: blockValidator.Description(ctx),
},
)
blockValidator.ValidateObject(ctx, validateReq, validateResp)
logging.FrameworkDebug(
ctx,
"Called provider defined validator.Object",
map[string]interface{}{
logging.KeyDescription: blockValidator.Description(ctx),
},
)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
// BlockValidateSet performs all types.Set validation.
func BlockValidateSet(ctx context.Context, block fwxschema.BlockWithSetValidators, req ValidateAttributeRequest, resp *ValidateAttributeResponse) {
// Use basetypes.SetValuable until custom types cannot re-implement
// ValueFromTerraform. Until then, custom types are not technically
// required to implement this interface. This opts to enforce the
// requirement before compatibility promises would interfere.
configValuable, ok := req.AttributeConfig.(basetypes.SetValuable)
if !ok {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Invalid Set Attribute Validator Value Type",
"An unexpected value type was encountered while attempting to perform Set attribute validation. "+
"The value type must implement the basetypes.SetValuable interface. "+
"Please report this to the provider developers.\n\n"+
fmt.Sprintf("Incoming Value Type: %T", req.AttributeConfig),
)
return
}
configValue, diags := configValuable.ToSetValue(ctx)
resp.Diagnostics.Append(diags...)
// Only return early on new errors as the resp.Diagnostics may have errors
// from other attributes.
if diags.HasError() {
return
}
validateReq := validator.SetRequest{
Config: req.Config,
ConfigValue: configValue,
Path: req.AttributePath,
PathExpression: req.AttributePathExpression,
}
for _, blockValidator := range block.SetValidators() {
// Instantiate a new response for each request to prevent validators
// from modifying or removing diagnostics.
validateResp := &validator.SetResponse{}
logging.FrameworkDebug(
ctx,
"Calling provider defined validator.Set",
map[string]interface{}{
logging.KeyDescription: blockValidator.Description(ctx),
},
)
blockValidator.ValidateSet(ctx, validateReq, validateResp)
logging.FrameworkDebug(
ctx,
"Called provider defined validator.Set",
map[string]interface{}{
logging.KeyDescription: blockValidator.Description(ctx),
},
)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
func NestedBlockObjectValidate(ctx context.Context, o fwschema.NestedBlockObject, req ValidateAttributeRequest, resp *ValidateAttributeResponse) {
objectWithValidators, ok := o.(fwxschema.NestedBlockObjectWithValidators)
if ok {
objectVal, ok := req.AttributeConfig.(basetypes.ObjectValuable)
if !ok {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Walk Error",
"An unexpected error occurred while walking the schema for block validation. "+
"This is an issue with terraform-plugin-framework and should be reported to the provider developers.\n\n"+
fmt.Sprintf("Unknown block value type (%T) at path: %s", req.AttributeConfig, req.AttributePath),
)
return
}
object, diags := objectVal.ToObjectValue(ctx)
resp.Diagnostics.Append(diags...)
// Only return early on new errors as the resp.Diagnostics may have
// errors from other attributes.
if diags.HasError() {
return
}
validateReq := validator.ObjectRequest{
Config: req.Config,
ConfigValue: object,
Path: req.AttributePath,
PathExpression: req.AttributePathExpression,
}
for _, objectValidator := range objectWithValidators.ObjectValidators() {
// Instantiate a new response for each request to prevent validators
// from modifying or removing diagnostics.
validateResp := &validator.ObjectResponse{}
logging.FrameworkDebug(
ctx,
"Calling provider defined validator.Object",
map[string]interface{}{
logging.KeyDescription: objectValidator.Description(ctx),
},
)
objectValidator.ValidateObject(ctx, validateReq, validateResp)
logging.FrameworkDebug(
ctx,
"Called provider defined validator.Object",
map[string]interface{}{
logging.KeyDescription: objectValidator.Description(ctx),
},
)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
for nestedName, nestedAttr := range o.GetAttributes() {
nestedAttrReq := ValidateAttributeRequest{
AttributePath: req.AttributePath.AtName(nestedName),
AttributePathExpression: req.AttributePathExpression.AtName(nestedName),
Config: req.Config,
}
nestedAttrResp := &ValidateAttributeResponse{}
AttributeValidate(ctx, nestedAttr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics.Append(nestedAttrResp.Diagnostics...)
}
for nestedName, nestedBlock := range o.GetBlocks() {
nestedBlockReq := ValidateAttributeRequest{
AttributePath: req.AttributePath.AtName(nestedName),
AttributePathExpression: req.AttributePathExpression.AtName(nestedName),
Config: req.Config,
}
nestedBlockResp := &ValidateAttributeResponse{}
BlockValidate(ctx, nestedBlock, nestedBlockReq, nestedBlockResp)
resp.Diagnostics.Append(nestedBlockResp.Diagnostics...)
}
}