-
Notifications
You must be signed in to change notification settings - Fork 94
/
data_default.go
307 lines (218 loc) · 7.89 KB
/
data_default.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package fwschemadata
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fromtftypes"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
)
// TransformDefaults walks the schema and applies schema defined default values
// when configRaw contains a null value at the same path.
func (d *Data) TransformDefaults(ctx context.Context, configRaw tftypes.Value) diag.Diagnostics {
var diags diag.Diagnostics
configData := Data{
Description: DataDescriptionConfiguration,
Schema: d.Schema,
TerraformValue: configRaw,
}
// Errors are handled as richer diag.Diagnostics instead.
d.TerraformValue, _ = tftypes.Transform(d.TerraformValue, func(tfTypePath *tftypes.AttributePath, tfTypeValue tftypes.Value) (tftypes.Value, error) {
fwPath, fwPathDiags := fromtftypes.AttributePath(ctx, tfTypePath, d.Schema)
diags.Append(fwPathDiags...)
// Do not transform if path cannot be converted.
// Checking against fwPathDiags will capture all errors.
if fwPathDiags.HasError() {
return tfTypeValue, nil
}
configValue, configValueDiags := configData.ValueAtPath(ctx, fwPath)
diags.Append(configValueDiags...)
// Do not transform if rawConfig value cannot be retrieved.
if configValueDiags.HasError() {
return tfTypeValue, nil
}
// Do not transform if rawConfig value is not null.
if !configValue.IsNull() {
return tfTypeValue, nil
}
attrAtPath, err := d.Schema.AttributeAtTerraformPath(ctx, tfTypePath)
if err != nil {
if errors.Is(err, fwschema.ErrPathInsideAtomicAttribute) {
// ignore attributes/elements inside schema.Attributes, they have no schema of their own
logging.FrameworkTrace(ctx, "attribute is a non-schema attribute, not setting default")
return tfTypeValue, nil
}
if errors.Is(err, fwschema.ErrPathIsBlock) {
// ignore blocks, they do not have a computed field
logging.FrameworkTrace(ctx, "attribute is a block, not setting default")
return tfTypeValue, nil
}
return tftypes.Value{}, fmt.Errorf("couldn't find attribute in resource schema: %w", err)
}
switch a := attrAtPath.(type) {
case fwschema.AttributeWithBoolDefaultValue:
defaultValue := a.BoolDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.BoolRequest{
Path: fwPath,
}
resp := defaults.BoolResponse{}
defaultValue.DefaultBool(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithFloat64DefaultValue:
defaultValue := a.Float64DefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.Float64Request{
Path: fwPath,
}
resp := defaults.Float64Response{}
defaultValue.DefaultFloat64(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithInt64DefaultValue:
defaultValue := a.Int64DefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.Int64Request{
Path: fwPath,
}
resp := defaults.Int64Response{}
defaultValue.DefaultInt64(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithListDefaultValue:
defaultValue := a.ListDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.ListRequest{
Path: fwPath,
}
resp := defaults.ListResponse{}
defaultValue.DefaultList(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
if resp.PlanValue.ElementType(ctx) == nil {
logging.FrameworkWarn(ctx, "attribute default declared, but returned no value")
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithMapDefaultValue:
defaultValue := a.MapDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.MapRequest{
Path: fwPath,
}
resp := defaults.MapResponse{}
defaultValue.DefaultMap(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
if resp.PlanValue.ElementType(ctx) == nil {
logging.FrameworkWarn(ctx, "attribute default declared, but returned no value")
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithNumberDefaultValue:
defaultValue := a.NumberDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.NumberRequest{
Path: fwPath,
}
resp := defaults.NumberResponse{}
defaultValue.DefaultNumber(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithObjectDefaultValue:
defaultValue := a.ObjectDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.ObjectRequest{
Path: fwPath,
}
resp := defaults.ObjectResponse{}
defaultValue.DefaultObject(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithSetDefaultValue:
defaultValue := a.SetDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.SetRequest{
Path: fwPath,
}
resp := defaults.SetResponse{}
defaultValue.DefaultSet(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
if resp.PlanValue.ElementType(ctx) == nil {
logging.FrameworkWarn(ctx, "attribute default declared, but returned no value")
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
case fwschema.AttributeWithStringDefaultValue:
defaultValue := a.StringDefaultValue()
if defaultValue == nil {
return tfTypeValue, nil
}
req := defaults.StringRequest{
Path: fwPath,
}
resp := defaults.StringResponse{}
defaultValue.DefaultString(ctx, req, &resp)
diags.Append(resp.Diagnostics...)
if resp.Diagnostics.HasError() {
return tfTypeValue, nil
}
logging.FrameworkTrace(ctx, fmt.Sprintf("setting attribute %s to default value: %s", fwPath, resp.PlanValue))
return resp.PlanValue.ToTerraformValue(ctx)
}
return tfTypeValue, nil
})
return diags
}