-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
plan_valid.go
419 lines (378 loc) · 15.5 KB
/
plan_valid.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package objchange
import (
"fmt"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/configs/configschema"
)
// AssertPlanValid checks checks whether a planned new state returned by a
// provider's PlanResourceChange method is suitable to achieve a change
// from priorState to config. It returns a slice with nonzero length if
// any problems are detected. Because problems here indicate bugs in the
// provider that generated the plannedState, they are written with provider
// developers as an audience, rather than end-users.
//
// All of the given values must have the same type and must conform to the
// implied type of the given schema, or this function may panic or produce
// garbage results.
//
// During planning, a provider may only make changes to attributes that are
// null (unset) in the configuration and are marked as "computed" in the
// resource type schema, in order to insert any default values the provider
// may know about. If the default value cannot be determined until apply time,
// the provider can return an unknown value. Providers are forbidden from
// planning a change that disagrees with any non-null argument in the
// configuration.
//
// As a special exception, providers _are_ allowed to provide attribute values
// conflicting with configuration if and only if the planned value exactly
// matches the corresponding attribute value in the prior state. The provider
// can use this to signal that the new value is functionally equivalent to
// the old and thus no change is required.
func AssertPlanValid(schema *configschema.Block, priorState, config, plannedState cty.Value) []error {
return assertPlanValid(schema, priorState, config, plannedState, nil)
}
func assertPlanValid(schema *configschema.Block, priorState, config, plannedState cty.Value, path cty.Path) []error {
var errs []error
if plannedState.IsNull() && !config.IsNull() {
errs = append(errs, path.NewErrorf("planned for absence but config wants existence"))
return errs
}
if config.IsNull() && !plannedState.IsNull() {
errs = append(errs, path.NewErrorf("planned for existence but config wants absence"))
return errs
}
if plannedState.IsNull() {
// No further checks possible if the planned value is null
return errs
}
impTy := schema.ImpliedType()
// verify attributes
moreErrs := assertPlannedAttrsValid(schema.Attributes, priorState, config, plannedState, path)
errs = append(errs, moreErrs...)
for name, blockS := range schema.BlockTypes {
path := append(path, cty.GetAttrStep{Name: name})
plannedV := plannedState.GetAttr(name)
configV := config.GetAttr(name)
priorV := cty.NullVal(impTy.AttributeType(name))
if !priorState.IsNull() {
priorV = priorState.GetAttr(name)
}
if plannedV.RawEquals(configV) {
// Easy path: nothing has changed at all
continue
}
if !configV.IsKnown() {
// An unknown config block represents a dynamic block where the
// for_each value is unknown, and therefor cannot be altered by the
// provider.
errs = append(errs, path.NewErrorf("planned value %#v for unknown dynamic block", plannedV))
continue
}
if !plannedV.IsKnown() {
// Only dynamic configuration can set blocks to unknown, so this is
// not allowed from the provider. This means that either the config
// and plan should match, or we have an error where the plan
// changed the config value, both of which have been checked.
errs = append(errs, path.NewErrorf("attribute representing nested block must not be unknown itself; set nested attribute values to unknown instead"))
continue
}
switch blockS.Nesting {
case configschema.NestingSingle, configschema.NestingGroup:
moreErrs := assertPlanValid(&blockS.Block, priorV, configV, plannedV, path)
errs = append(errs, moreErrs...)
case configschema.NestingList:
// A NestingList might either be a list or a tuple, depending on
// whether there are dynamically-typed attributes inside. However,
// both support a similar-enough API that we can treat them the
// same for our purposes here.
if plannedV.IsNull() {
errs = append(errs, path.NewErrorf("attribute representing a list of nested blocks must be empty to indicate no blocks, not null"))
continue
}
plannedL := plannedV.LengthInt()
configL := configV.LengthInt()
if plannedL != configL {
errs = append(errs, path.NewErrorf("block count in plan (%d) disagrees with count in config (%d)", plannedL, configL))
continue
}
for it := plannedV.ElementIterator(); it.Next(); {
idx, plannedEV := it.Element()
path := append(path, cty.IndexStep{Key: idx})
if !plannedEV.IsKnown() {
errs = append(errs, path.NewErrorf("element representing nested block must not be unknown itself; set nested attribute values to unknown instead"))
continue
}
if !configV.HasIndex(idx).True() {
continue // should never happen since we checked the lengths above
}
configEV := configV.Index(idx)
priorEV := cty.NullVal(blockS.ImpliedType())
if !priorV.IsNull() && priorV.HasIndex(idx).True() {
priorEV = priorV.Index(idx)
}
moreErrs := assertPlanValid(&blockS.Block, priorEV, configEV, plannedEV, path)
errs = append(errs, moreErrs...)
}
case configschema.NestingMap:
if plannedV.IsNull() {
errs = append(errs, path.NewErrorf("attribute representing a map of nested blocks must be empty to indicate no blocks, not null"))
continue
}
// A NestingMap might either be a map or an object, depending on
// whether there are dynamically-typed attributes inside, but
// that's decided statically and so all values will have the same
// kind.
if plannedV.Type().IsObjectType() {
plannedAtys := plannedV.Type().AttributeTypes()
configAtys := configV.Type().AttributeTypes()
for k := range plannedAtys {
if _, ok := configAtys[k]; !ok {
errs = append(errs, path.NewErrorf("block key %q from plan is not present in config", k))
continue
}
path := append(path, cty.GetAttrStep{Name: k})
plannedEV := plannedV.GetAttr(k)
if !plannedEV.IsKnown() {
errs = append(errs, path.NewErrorf("element representing nested block must not be unknown itself; set nested attribute values to unknown instead"))
continue
}
configEV := configV.GetAttr(k)
priorEV := cty.NullVal(blockS.ImpliedType())
if !priorV.IsNull() && priorV.Type().HasAttribute(k) {
priorEV = priorV.GetAttr(k)
}
moreErrs := assertPlanValid(&blockS.Block, priorEV, configEV, plannedEV, path)
errs = append(errs, moreErrs...)
}
for k := range configAtys {
if _, ok := plannedAtys[k]; !ok {
errs = append(errs, path.NewErrorf("block key %q from config is not present in plan", k))
continue
}
}
} else {
plannedL := plannedV.LengthInt()
configL := configV.LengthInt()
if plannedL != configL {
errs = append(errs, path.NewErrorf("block count in plan (%d) disagrees with count in config (%d)", plannedL, configL))
continue
}
for it := plannedV.ElementIterator(); it.Next(); {
idx, plannedEV := it.Element()
path := append(path, cty.IndexStep{Key: idx})
if !plannedEV.IsKnown() {
errs = append(errs, path.NewErrorf("element representing nested block must not be unknown itself; set nested attribute values to unknown instead"))
continue
}
k := idx.AsString()
if !configV.HasIndex(idx).True() {
errs = append(errs, path.NewErrorf("block key %q from plan is not present in config", k))
continue
}
configEV := configV.Index(idx)
priorEV := cty.NullVal(blockS.ImpliedType())
if !priorV.IsNull() && priorV.HasIndex(idx).True() {
priorEV = priorV.Index(idx)
}
moreErrs := assertPlanValid(&blockS.Block, priorEV, configEV, plannedEV, path)
errs = append(errs, moreErrs...)
}
for it := configV.ElementIterator(); it.Next(); {
idx, _ := it.Element()
if !plannedV.HasIndex(idx).True() {
errs = append(errs, path.NewErrorf("block key %q from config is not present in plan", idx.AsString()))
continue
}
}
}
case configschema.NestingSet:
if plannedV.IsNull() {
errs = append(errs, path.NewErrorf("attribute representing a set of nested blocks must be empty to indicate no blocks, not null"))
continue
}
// Because set elements have no identifier with which to correlate
// them, we can't robustly validate the plan for a nested block
// backed by a set, and so unfortunately we need to just trust the
// provider to do the right thing. :(
//
// (In principle we could correlate elements by matching the
// subset of attributes explicitly set in config, except for the
// special diff suppression rule which allows for there to be a
// planned value that is constructed by mixing part of a prior
// value with part of a config value, creating an entirely new
// element that is not present in either prior nor config.)
for it := plannedV.ElementIterator(); it.Next(); {
idx, plannedEV := it.Element()
path := append(path, cty.IndexStep{Key: idx})
if !plannedEV.IsKnown() {
errs = append(errs, path.NewErrorf("element representing nested block must not be unknown itself; set nested attribute values to unknown instead"))
continue
}
}
default:
panic(fmt.Sprintf("unsupported nesting mode %s", blockS.Nesting))
}
}
return errs
}
func assertPlannedAttrsValid(schema map[string]*configschema.Attribute, priorState, config, plannedState cty.Value, path cty.Path) []error {
var errs []error
for name, attrS := range schema {
moreErrs := assertPlannedAttrValid(name, attrS, priorState, config, plannedState, path)
errs = append(errs, moreErrs...)
}
return errs
}
func assertPlannedAttrValid(name string, attrS *configschema.Attribute, priorState, config, plannedState cty.Value, path cty.Path) []error {
plannedV := plannedState.GetAttr(name)
configV := config.GetAttr(name)
priorV := cty.NullVal(attrS.Type)
if !priorState.IsNull() {
priorV = priorState.GetAttr(name)
}
path = append(path, cty.GetAttrStep{Name: name})
return assertPlannedValueValid(attrS, priorV, configV, plannedV, path)
}
func assertPlannedValueValid(attrS *configschema.Attribute, priorV, configV, plannedV cty.Value, path cty.Path) []error {
var errs []error
if plannedV.RawEquals(configV) {
// This is the easy path: provider didn't change anything at all.
return errs
}
if plannedV.RawEquals(priorV) && !priorV.IsNull() && !configV.IsNull() {
// Also pretty easy: there is a prior value and the provider has
// returned it unchanged. This indicates that configV and plannedV
// are functionally equivalent and so the provider wishes to disregard
// the configuration value in favor of the prior.
return errs
}
// the provider is allowed to insert values when the config is
// null, but only if the attribute is computed.
if configV.IsNull() {
if attrS.Computed {
return errs
}
// if the attribute is not computed, then any planned value is incorrect
if !plannedV.IsNull() {
if attrS.Sensitive {
errs = append(errs, path.NewErrorf("sensitive planned value for a non-computed attribute"))
} else {
errs = append(errs, path.NewErrorf("planned value %#v for a non-computed attribute", plannedV))
}
return errs
}
}
// If this attribute has a NestedType, validate the nested object
if attrS.NestedType != nil {
return assertPlannedObjectValid(attrS.NestedType, priorV, configV, plannedV, path)
}
// If none of the above conditions match, the provider has made an invalid
// change to this attribute.
if priorV.IsNull() {
if attrS.Sensitive {
errs = append(errs, path.NewErrorf("sensitive planned value does not match config value"))
} else {
errs = append(errs, path.NewErrorf("planned value %#v does not match config value %#v", plannedV, configV))
}
return errs
}
if attrS.Sensitive {
errs = append(errs, path.NewErrorf("sensitive planned value does not match config value nor prior value"))
} else {
errs = append(errs, path.NewErrorf("planned value %#v does not match config value %#v nor prior value %#v", plannedV, configV, priorV))
}
return errs
}
func assertPlannedObjectValid(schema *configschema.Object, prior, config, planned cty.Value, path cty.Path) []error {
var errs []error
if planned.IsNull() && !config.IsNull() {
errs = append(errs, path.NewErrorf("planned for absence but config wants existence"))
return errs
}
if config.IsNull() && !planned.IsNull() {
errs = append(errs, path.NewErrorf("planned for existence but config wants absence"))
return errs
}
if planned.IsNull() {
// No further checks possible if the planned value is null
return errs
}
switch schema.Nesting {
case configschema.NestingSingle, configschema.NestingGroup:
moreErrs := assertPlannedAttrsValid(schema.Attributes, prior, config, planned, path)
errs = append(errs, moreErrs...)
case configschema.NestingList:
// A NestingList might either be a list or a tuple, depending on
// whether there are dynamically-typed attributes inside. However,
// both support a similar-enough API that we can treat them the
// same for our purposes here.
plannedL := planned.LengthInt()
configL := config.LengthInt()
if plannedL != configL {
errs = append(errs, path.NewErrorf("count in plan (%d) disagrees with count in config (%d)", plannedL, configL))
return errs
}
for it := planned.ElementIterator(); it.Next(); {
idx, plannedEV := it.Element()
path := append(path, cty.IndexStep{Key: idx})
if !config.HasIndex(idx).True() {
continue // should never happen since we checked the lengths above
}
configEV := config.Index(idx)
priorEV := cty.NullVal(schema.ImpliedType())
if !prior.IsNull() && prior.HasIndex(idx).True() {
priorEV = prior.Index(idx)
}
moreErrs := assertPlannedAttrsValid(schema.Attributes, priorEV, configEV, plannedEV, path)
errs = append(errs, moreErrs...)
}
case configschema.NestingMap:
// A NestingMap might either be a map or an object, depending on
// whether there are dynamically-typed attributes inside, so we will
// break these down to maps to handle them both in the same manner.
plannedVals := map[string]cty.Value{}
configVals := map[string]cty.Value{}
priorVals := map[string]cty.Value{}
if !planned.IsNull() {
plannedVals = planned.AsValueMap()
}
if !config.IsNull() {
configVals = config.AsValueMap()
}
if !prior.IsNull() {
priorVals = prior.AsValueMap()
}
for k, plannedEV := range plannedVals {
configEV, ok := configVals[k]
if !ok {
errs = append(errs, path.NewErrorf("map key %q from plan is not present in config", k))
continue
}
path := append(path, cty.GetAttrStep{Name: k})
priorEV, ok := priorVals[k]
if !ok {
priorEV = cty.NullVal(schema.ImpliedType())
}
moreErrs := assertPlannedAttrsValid(schema.Attributes, priorEV, configEV, plannedEV, path)
errs = append(errs, moreErrs...)
}
for k := range configVals {
if _, ok := plannedVals[k]; !ok {
errs = append(errs, path.NewErrorf("map key %q from config is not present in plan", k))
continue
}
}
case configschema.NestingSet:
plannedL := planned.LengthInt()
configL := config.LengthInt()
if plannedL != configL {
errs = append(errs, path.NewErrorf("count in plan (%d) disagrees with count in config (%d)", plannedL, configL))
return errs
}
// Because set elements have no identifier with which to correlate
// them, we can't robustly validate the plan for a nested object
// backed by a set, and so unfortunately we need to just trust the
// provider to do the right thing.
}
return errs
}