-
Notifications
You must be signed in to change notification settings - Fork 10
/
exactly_one_of.go
248 lines (199 loc) · 7.46 KB
/
exactly_one_of.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package schemavalidator
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
// This type of validator must satisfy all types.
var (
_ validator.Bool = ExactlyOneOfValidator{}
_ validator.Float64 = ExactlyOneOfValidator{}
_ validator.Int64 = ExactlyOneOfValidator{}
_ validator.List = ExactlyOneOfValidator{}
_ validator.Map = ExactlyOneOfValidator{}
_ validator.Number = ExactlyOneOfValidator{}
_ validator.Object = ExactlyOneOfValidator{}
_ validator.Set = ExactlyOneOfValidator{}
_ validator.String = ExactlyOneOfValidator{}
)
// ExactlyOneOfValidator is the underlying struct implementing ExactlyOneOf.
type ExactlyOneOfValidator struct {
PathExpressions path.Expressions
}
type ExactlyOneOfValidatorRequest struct {
Config tfsdk.Config
ConfigValue attr.Value
Path path.Path
PathExpression path.Expression
}
type ExactlyOneOfValidatorResponse struct {
Diagnostics diag.Diagnostics
}
func (av ExactlyOneOfValidator) Description(ctx context.Context) string {
return av.MarkdownDescription(ctx)
}
func (av ExactlyOneOfValidator) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("Ensure that one and only one attribute from this collection is set: %q", av.PathExpressions)
}
func (av ExactlyOneOfValidator) Validate(ctx context.Context, req ExactlyOneOfValidatorRequest, res *ExactlyOneOfValidatorResponse) {
count := 0
expressions := req.PathExpression.MergeExpressions(av.PathExpressions...)
// If current attribute is unknown, delay validation
if req.ConfigValue.IsUnknown() {
return
}
// Now that we know the current attribute is known, check whether it is
// null to determine if it should contribute to the count. Later logic
// will remove a duplicate matching path, should it be included in the
// given expressions.
if !req.ConfigValue.IsNull() {
count++
}
for _, expression := range expressions {
matchedPaths, diags := req.Config.PathMatches(ctx, expression)
res.Diagnostics.Append(diags...)
// Collect all errors
if diags.HasError() {
continue
}
for _, mp := range matchedPaths {
// If the user specifies the same attribute this validator is applied to,
// also as part of the input, skip it
if mp.Equal(req.Path) {
continue
}
var mpVal attr.Value
diags := req.Config.GetAttribute(ctx, mp, &mpVal)
res.Diagnostics.Append(diags...)
// Collect all errors
if diags.HasError() {
continue
}
// Delay validation until all involved attribute have a known value
if mpVal.IsUnknown() {
return
}
if !mpVal.IsNull() {
count++
}
}
}
if count == 0 {
res.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("No attribute specified when one (and only one) of %s is required", expressions),
))
}
if count > 1 {
res.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("%d attributes specified when one (and only one) of %s is required", count, expressions),
))
}
}
func (av ExactlyOneOfValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateFloat64(ctx context.Context, req validator.Float64Request, resp *validator.Float64Response) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateNumber(ctx context.Context, req validator.NumberRequest, resp *validator.NumberResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
func (av ExactlyOneOfValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
validateReq := ExactlyOneOfValidatorRequest{
Config: req.Config,
ConfigValue: req.ConfigValue,
Path: req.Path,
PathExpression: req.PathExpression,
}
validateResp := &ExactlyOneOfValidatorResponse{}
av.Validate(ctx, validateReq, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}