-
Notifications
You must be signed in to change notification settings - Fork 114
/
attribute.go
229 lines (184 loc) · 6.78 KB
/
attribute.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
package planmodifiers
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// DefaultValue accepts an attr.Value and uses the supplied value to set a default if the config for
// the attribute is null.
func DefaultValue(val attr.Value) tfsdk.AttributePlanModifier {
return &defaultValueAttributePlanModifier{val}
}
type defaultValueAttributePlanModifier struct {
val attr.Value
}
func (d *defaultValueAttributePlanModifier) Description(ctx context.Context) string {
return "If the config does not contain a value, a default will be set using val."
}
func (d *defaultValueAttributePlanModifier) MarkdownDescription(ctx context.Context) string {
return d.Description(ctx)
}
// Modify checks that the value of the attribute in the configuration and assigns the default value if
// the value in the config is null. This is a destructive operation in that it will overwrite any value
// present in the plan.
func (d *defaultValueAttributePlanModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
// Do not set default if the attribute configuration has been set.
if !req.AttributeConfig.IsNull() {
return
}
resp.AttributePlan = d.val
}
// RequiresReplace returns an attribute plan modifier that is identical to resource.RequiresReplace() with
// the exception that there is no check for `configRaw.IsNull && attrSchema.Computed` as a replacement
// needs to be triggered when the attribute has been removed from the config.
func RequiresReplace() tfsdk.AttributePlanModifier {
return RequiresReplaceModifier{}
}
type RequiresReplaceModifier struct{}
// Description returns a human-readable description of the plan modifier.
func (r RequiresReplaceModifier) Description(ctx context.Context) string {
return "If the value of this attribute changes, Terraform will destroy and recreate the resource."
}
// MarkdownDescription returns a markdown description of the plan modifier.
func (r RequiresReplaceModifier) MarkdownDescription(ctx context.Context) string {
return r.Description(ctx)
}
// Modify will trigger replacement (i.e., destroy-create) when `configRaw.IsNull && attrSchema.Computed`,
// which differs from the behaviour of `resource.RequiresReplace()`.
func (r RequiresReplaceModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
if req.AttributeConfig == nil || req.AttributePlan == nil || req.AttributeState == nil {
// shouldn't happen, but let's not panic if it does
return
}
if req.State.Raw.IsNull() {
// if we're creating the resource, no need to delete and
// recreate it
return
}
if req.Plan.Raw.IsNull() {
// if we're deleting the resource, no need to delete and
// recreate it
return
}
if req.AttributePlan.Equal(req.AttributeState) {
// if the plan and the state are in agreement, this attribute
// isn't changing, don't require replace
return
}
resp.RequiresReplace = true
}
func NumberNumericAttributePlanModifier() tfsdk.AttributePlanModifier {
return &numberNumericAttributePlanModifier{}
}
type numberNumericAttributePlanModifier struct {
}
func (d *numberNumericAttributePlanModifier) Description(ctx context.Context) string {
return "Ensures that number and numeric attributes are kept synchronised."
}
func (d *numberNumericAttributePlanModifier) MarkdownDescription(ctx context.Context) string {
return d.Description(ctx)
}
func (d *numberNumericAttributePlanModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
numberConfig := types.Bool{}
diags := req.Config.GetAttribute(ctx, path.Root("number"), &numberConfig)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
numericConfig := types.Bool{}
diags = req.Config.GetAttribute(ctx, path.Root("numeric"), &numericConfig)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if !numberConfig.Null && !numericConfig.Null && (numberConfig.Value != numericConfig.Value) {
resp.Diagnostics.AddError(
"Number and numeric are both configured with different values",
"Number is deprecated, use numeric instead",
)
return
}
// Default to true for both number and numeric when both are null.
if numberConfig.Null && numericConfig.Null {
resp.AttributePlan = types.Bool{Value: true}
return
}
// Default to using value for numeric if number is null.
if numberConfig.IsNull() && !numericConfig.IsNull() {
resp.AttributePlan = numericConfig
return
}
// Default to using value for number if numeric is null.
if !numberConfig.IsNull() && numericConfig.IsNull() {
resp.AttributePlan = numberConfig
return
}
}
func RequiresReplaceIfValuesNotNull() tfsdk.AttributePlanModifier {
return requiresReplaceIfValuesNotNullModifier{}
}
type requiresReplaceIfValuesNotNullModifier struct{}
func (r requiresReplaceIfValuesNotNullModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
if req.AttributeConfig == nil || req.AttributePlan == nil || req.AttributeState == nil {
// shouldn't happen, but let's not panic if it does
return
}
if req.State.Raw.IsNull() {
// if we're creating the resource, no need to delete and
// recreate it
return
}
if req.Plan.Raw.IsNull() {
// if we're deleting the resource, no need to delete and
// recreate it
return
}
configMap, ok := req.AttributeConfig.(types.Map)
if !ok {
return
}
stateMap, ok := req.AttributeState.(types.Map)
if !ok {
return
}
replace := false
additionalElems := map[string]attr.Value{}
for k, v := range configMap.Elems {
if _, ok := stateMap.Elems[k]; !ok {
if !v.IsNull() || v.IsUnknown() {
replace = true
break
}
continue
}
if stateMap.Elems[k] != configMap.Elems[k] {
replace = true
break
}
if v.IsNull() && !v.IsUnknown() {
additionalElems[k] = v
}
}
if replace {
resp.RequiresReplace = true
return
}
respPlan := resp.AttributePlan
pm, ok := respPlan.(types.Map)
if ok {
for k, v := range additionalElems {
pm.Elems[k] = v
}
}
resp.AttributePlan = pm
}
// Description returns a human-readable description of the plan modifier.
func (r requiresReplaceIfValuesNotNullModifier) Description(ctx context.Context) string {
return "If the value of this attribute changes, Terraform will destroy and recreate the resource."
}
// MarkdownDescription returns a markdown description of the plan modifier.
func (r requiresReplaceIfValuesNotNullModifier) MarkdownDescription(ctx context.Context) string {
return "If the value of this attribute changes, Terraform will destroy and recreate the resource."
}