-
Notifications
You must be signed in to change notification settings - Fork 10
/
at_most_sum_of.go
116 lines (95 loc) · 3.56 KB
/
at_most_sum_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package int64validator
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
)
var _ validator.Int64 = atMostSumOfValidator{}
// atMostSumOfValidator validates that an integer Attribute's value is at most the sum of one
// or more integer Attributes retrieved via the given path expressions.
type atMostSumOfValidator struct {
attributesToSumPathExpressions path.Expressions
}
// Description describes the validation in plain text formatting.
func (av atMostSumOfValidator) Description(_ context.Context) string {
var attributePaths []string
for _, p := range av.attributesToSumPathExpressions {
attributePaths = append(attributePaths, p.String())
}
return fmt.Sprintf("value must be at most sum of %s", strings.Join(attributePaths, " + "))
}
// MarkdownDescription describes the validation in Markdown formatting.
func (av atMostSumOfValidator) MarkdownDescription(ctx context.Context) string {
return av.Description(ctx)
}
// ValidateInt64 performs the validation.
func (av atMostSumOfValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
// Ensure input path expressions resolution against the current attribute
expressions := request.PathExpression.MergeExpressions(av.attributesToSumPathExpressions...)
// Sum the value of all the attributes involved, but only if they are all known.
var sumOfAttribs int64
for _, expression := range expressions {
matchedPaths, diags := request.Config.PathMatches(ctx, expression)
response.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(request.Path) {
continue
}
// Get the value
var matchedValue attr.Value
diags := request.Config.GetAttribute(ctx, mp, &matchedValue)
response.Diagnostics.Append(diags...)
if diags.HasError() {
continue
}
if matchedValue.IsUnknown() {
return
}
if matchedValue.IsNull() {
continue
}
// We know there is a value, convert it to the expected type
var attribToSum types.Int64
diags = tfsdk.ValueAs(ctx, matchedValue, &attribToSum)
response.Diagnostics.Append(diags...)
if diags.HasError() {
continue
}
sumOfAttribs += attribToSum.ValueInt64()
}
}
if request.ConfigValue.ValueInt64() > sumOfAttribs {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
av.Description(ctx),
fmt.Sprintf("%d", request.ConfigValue.ValueInt64()),
))
}
}
// AtMostSumOf returns an AttributeValidator which ensures that any configured
// attribute value:
//
// - Is a number, which can be represented by a 64-bit integer.
// - Is at most the sum of the given attributes retrieved via the given path expression(s).
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func AtMostSumOf(attributesToSumPathExpressions ...path.Expression) validator.Int64 {
return atMostSumOfValidator{attributesToSumPathExpressions}
}