/
schema_plan_modification.go
194 lines (145 loc) · 5.36 KB
/
schema_plan_modification.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
package fwserver
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
// ModifySchemaPlanRequest represents a request for a schema to run all
// attribute plan modification functions.
type ModifySchemaPlanRequest struct {
// Config is the configuration the user supplied for the resource.
Config tfsdk.Config
// State is the current state of the resource.
State tfsdk.State
// Plan is the planned new state for the resource.
Plan tfsdk.Plan
// ProviderMeta is metadata from the provider_meta block of the module.
ProviderMeta tfsdk.Config
// Private is provider private state data.
Private *privatestate.ProviderData
}
// ModifySchemaPlanResponse represents a response to a ModifySchemaPlanRequest.
type ModifySchemaPlanResponse struct {
// Plan is the planned new state for the resource.
Plan tfsdk.Plan
// RequiresReplace is a list of attribute paths that require the
// resource to be replaced. They should point to the specific field
// that changed that requires the resource to be destroyed and
// recreated.
RequiresReplace path.Paths
// Private is provider private state data following potential modifications.
Private *privatestate.ProviderData
// Diagnostics report errors or warnings related to running all attribute
// plan modifiers. Returning an empty slice indicates a successful
// plan modification with no warnings or errors generated.
Diagnostics diag.Diagnostics
}
// SchemaModifyPlan runs all AttributePlanModifiers in all schema attributes
// and blocks.
//
// TODO: Clean up this abstraction back into an internal Schema type method.
// The extra Schema parameter is a carry-over of creating the proto6server
// package from the tfsdk package and not wanting to export the method.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/365
func SchemaModifyPlan(ctx context.Context, s fwschema.Schema, req ModifySchemaPlanRequest, resp *ModifySchemaPlanResponse) {
var diags diag.Diagnostics
configData := &fwschemadata.Data{
Description: fwschemadata.DataDescriptionConfiguration,
Schema: req.Config.Schema,
TerraformValue: req.Config.Raw,
}
planData := &fwschemadata.Data{
Description: fwschemadata.DataDescriptionPlan,
Schema: req.Plan.Schema,
TerraformValue: req.Plan.Raw,
}
stateData := &fwschemadata.Data{
Description: fwschemadata.DataDescriptionState,
Schema: req.State.Schema,
TerraformValue: req.State.Raw,
}
for name, attribute := range s.GetAttributes() {
attrReq := tfsdk.ModifyAttributePlanRequest{
AttributePath: path.Root(name),
Config: req.Config,
State: req.State,
Plan: req.Plan,
ProviderMeta: req.ProviderMeta,
Private: req.Private,
}
attrReq.AttributeConfig, diags = configData.ValueAtPath(ctx, attrReq.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
attrReq.AttributePlan, diags = planData.ValueAtPath(ctx, attrReq.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
attrReq.AttributeState, diags = stateData.ValueAtPath(ctx, attrReq.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
attrResp := ModifyAttributePlanResponse{
AttributePlan: attrReq.AttributePlan,
Private: attrReq.Private,
}
AttributeModifyPlan(ctx, attribute, attrReq, &attrResp)
resp.Diagnostics.Append(attrResp.Diagnostics...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.SetAttribute(ctx, attrReq.AttributePath, attrResp.AttributePlan)...)
if resp.Diagnostics.HasError() {
return
}
resp.RequiresReplace = append(resp.RequiresReplace, attrResp.RequiresReplace...)
resp.Private = attrResp.Private
}
for name, block := range s.GetBlocks() {
blockReq := tfsdk.ModifyAttributePlanRequest{
AttributePath: path.Root(name),
Config: req.Config,
State: req.State,
Plan: req.Plan,
ProviderMeta: req.ProviderMeta,
Private: req.Private,
}
blockReq.AttributeConfig, diags = configData.ValueAtPath(ctx, blockReq.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
blockReq.AttributePlan, diags = planData.ValueAtPath(ctx, blockReq.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
blockReq.AttributeState, diags = stateData.ValueAtPath(ctx, blockReq.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
blockResp := ModifyAttributePlanResponse{
AttributePlan: blockReq.AttributePlan,
Private: blockReq.Private,
}
BlockModifyPlan(ctx, block, blockReq, &blockResp)
resp.Diagnostics.Append(blockResp.Diagnostics...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.SetAttribute(ctx, blockReq.AttributePath, blockResp.AttributePlan)...)
if resp.Diagnostics.HasError() {
return
}
resp.RequiresReplace = append(resp.RequiresReplace, blockResp.RequiresReplace...)
resp.Private = blockResp.Private
}
}