/
server_updateresource.go
139 lines (113 loc) · 3.97 KB
/
server_updateresource.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
package fwserver
import (
"context"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
// UpdateResourceRequest is the framework server request for an update request
// with the ApplyResourceChange RPC.
type UpdateResourceRequest struct {
Config *tfsdk.Config
PlannedPrivate *privatestate.Data
PlannedState *tfsdk.Plan
PriorState *tfsdk.State
ProviderMeta *tfsdk.Config
ResourceSchema fwschema.Schema
Resource resource.Resource
}
// UpdateResourceResponse is the framework server response for an update request
// with the ApplyResourceChange RPC.
type UpdateResourceResponse struct {
Diagnostics diag.Diagnostics
NewState *tfsdk.State
Private *privatestate.Data
}
// UpdateResource implements the framework server update request logic for the
// ApplyResourceChange RPC.
func (s *Server) UpdateResource(ctx context.Context, req *UpdateResourceRequest, resp *UpdateResourceResponse) {
if req == nil {
return
}
if _, ok := req.Resource.(resource.ResourceWithConfigure); ok {
logging.FrameworkTrace(ctx, "Resource implements ResourceWithConfigure")
configureReq := resource.ConfigureRequest{
ProviderData: s.ResourceConfigureData,
}
configureResp := resource.ConfigureResponse{}
logging.FrameworkDebug(ctx, "Calling provider defined Resource Configure")
req.Resource.(resource.ResourceWithConfigure).Configure(ctx, configureReq, &configureResp)
logging.FrameworkDebug(ctx, "Called provider defined Resource Configure")
resp.Diagnostics.Append(configureResp.Diagnostics...)
if resp.Diagnostics.HasError() {
return
}
}
nullSchemaData := tftypes.NewValue(req.ResourceSchema.Type().TerraformType(ctx), nil)
updateReq := resource.UpdateRequest{
Config: tfsdk.Config{
Schema: req.ResourceSchema,
Raw: nullSchemaData,
},
Plan: tfsdk.Plan{
Schema: req.ResourceSchema,
Raw: nullSchemaData,
},
State: tfsdk.State{
Schema: req.ResourceSchema,
Raw: nullSchemaData,
},
}
updateResp := resource.UpdateResponse{
State: tfsdk.State{
Schema: req.ResourceSchema,
Raw: nullSchemaData,
},
}
if req.Config != nil {
updateReq.Config = *req.Config
}
if req.PlannedState != nil {
updateReq.Plan = *req.PlannedState
}
if req.PriorState != nil {
updateReq.State = *req.PriorState
// Require explicit provider updates for tracking successful updates.
updateResp.State = *req.PriorState
}
if req.ProviderMeta != nil {
updateReq.ProviderMeta = *req.ProviderMeta
}
privateProviderData := privatestate.EmptyProviderData(ctx)
updateReq.Private = privateProviderData
updateResp.Private = privateProviderData
if req.PlannedPrivate != nil {
if req.PlannedPrivate.Provider != nil {
updateReq.Private = req.PlannedPrivate.Provider
updateResp.Private = req.PlannedPrivate.Provider
}
resp.Private = req.PlannedPrivate
}
logging.FrameworkDebug(ctx, "Calling provider defined Resource Update")
req.Resource.Update(ctx, updateReq, &updateResp)
logging.FrameworkDebug(ctx, "Called provider defined Resource Update")
resp.Diagnostics = updateResp.Diagnostics
resp.NewState = &updateResp.State
if !resp.Diagnostics.HasError() && updateResp.State.Raw.Equal(nullSchemaData) {
resp.Diagnostics.AddError(
"Missing Resource State After Update",
"The Terraform Provider unexpectedly returned no resource state after having no errors in the resource update. "+
"This is always an issue in the Terraform Provider and should be reported to the provider developers.",
)
}
if updateResp.Private != nil {
if resp.Private == nil {
resp.Private = &privatestate.Data{}
}
resp.Private.Provider = updateResp.Private
}
}