/
block_validation.go
256 lines (212 loc) · 8.69 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
package fwserver
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/diag"
"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/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// 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 tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {
blockWithValidators, ok := b.(fwxschema.BlockWithValidators)
if !ok {
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
}
req.AttributeConfig = attributeConfig
for _, validator := range blockWithValidators.GetValidators() {
validator.Validate(ctx, req, resp)
}
nm := b.GetNestingMode()
switch nm {
case fwschema.BlockNestingModeList:
l, ok := req.AttributeConfig.(types.List)
if !ok {
err := fmt.Errorf("unknown block value type (%s) for nesting mode (%T) at path: %s", req.AttributeConfig.Type(ctx), 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
}
for idx := range l.Elems {
for name, attr := range b.GetAttributes() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtListIndex(idx).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtListIndex(idx).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, attr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
for name, block := range b.GetBlocks() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtListIndex(idx).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtListIndex(idx).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
BlockValidate(ctx, block, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
// Terraform 0.12 through 0.15.1 do not implement block MaxItems
// validation.
//
// Terraform 0.15.2 and later implements MaxItems validation during
// configuration decoding, so if this framework drops Terraform support
// for earlier versions, this validation can be removed.
if b.GetMaxItems() > 0 && int64(len(l.Elems)) > b.GetMaxItems() {
resp.Diagnostics.Append(blockMaxItemsDiagnostic(req.AttributePath, b.GetMaxItems(), len(l.Elems)))
}
// Terraform 0.12 through 0.15.1 implement conservative block MinItems
// validation, where the MinItems can be reset to 1 in certain
// situations. This validation must ensure the list itself is not
// unknown, which could erroneously trigger the error since the list
// would have 0 elements.
//
// Terraform 0.15.2 and later implements proper MinItems validation
// during configuration decoding, so if this framework drops Terraform
// support for earlier versions, this validation can be removed.
if b.GetMinItems() > 0 && int64(len(l.Elems)) < b.GetMinItems() && !l.IsUnknown() {
resp.Diagnostics.Append(blockMinItemsDiagnostic(req.AttributePath, b.GetMinItems(), len(l.Elems)))
}
case fwschema.BlockNestingModeSet:
s, ok := req.AttributeConfig.(types.Set)
if !ok {
err := fmt.Errorf("unknown block value type (%s) for nesting mode (%T) at path: %s", req.AttributeConfig.Type(ctx), 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
}
for _, value := range s.Elems {
for name, attr := range b.GetAttributes() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtSetValue(value).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtSetValue(value).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, attr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
for name, block := range b.GetBlocks() {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtSetValue(value).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtSetValue(value).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
BlockValidate(ctx, block, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
// Terraform 0.12 through 0.15.1 do not implement block MaxItems
// validation.
//
// Terraform 0.15.2 and later implements MaxItems validation during
// configuration decoding, so if this framework drops Terraform support
// for earlier versions, this validation can be removed.
if b.GetMaxItems() > 0 && int64(len(s.Elems)) > b.GetMaxItems() {
resp.Diagnostics.Append(blockMaxItemsDiagnostic(req.AttributePath, b.GetMaxItems(), len(s.Elems)))
}
// Terraform 0.12 through 0.15.1 implement conservative block MinItems
// validation, where the MinItems can be reset to 1 in certain
// situations. This validation must ensure the set itself is not
// unknown, which could erroneously trigger the error since the set
// would have 0 elements.
//
// Terraform 0.15.2 and later implements proper MinItems validation
// during configuration decoding, so if this framework drops Terraform
// support for earlier versions, this validation can be removed.
if b.GetMinItems() > 0 && int64(len(s.Elems)) < b.GetMinItems() && !s.IsUnknown() {
resp.Diagnostics.Append(blockMinItemsDiagnostic(req.AttributePath, b.GetMinItems(), len(s.Elems)))
}
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(),
)
}
}
func blockMaxItemsDiagnostic(attrPath path.Path, maxItems int64, elements int) diag.Diagnostic {
var details strings.Builder
details.WriteString("The configuration should declare a maximum of ")
if maxItems == 1 {
details.WriteString("1 block")
} else {
details.WriteString(fmt.Sprintf("%d blocks", maxItems))
}
// Elements will always be greater than 1, so do not need to handle the
// singular case.
details.WriteString(fmt.Sprintf(", however %d blocks were configured.", elements))
return diag.NewAttributeErrorDiagnostic(
attrPath,
"Extra Block Configuration",
details.String(),
)
}
func blockMinItemsDiagnostic(attrPath path.Path, minItems int64, elements int) diag.Diagnostic {
var details strings.Builder
details.WriteString("The configuration should declare a minimum of ")
if minItems == 1 {
details.WriteString("1 block")
} else {
details.WriteString(fmt.Sprintf("%d blocks", minItems))
}
details.WriteString(", however ")
if elements == 1 {
details.WriteString("1 block was")
} else {
details.WriteString(fmt.Sprintf("%d blocks were", elements))
}
details.WriteString(" configured.")
return diag.NewAttributeErrorDiagnostic(
attrPath,
"Missing Block Configuration",
details.String(),
)
}