-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathserver_deleteresource.go
100 lines (81 loc) · 3.01 KB
/
server_deleteresource.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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"
)
// DeleteResourceRequest is the framework server request for a delete request
// with the ApplyResourceChange RPC.
type DeleteResourceRequest struct {
PlannedPrivate *privatestate.Data
PriorState *tfsdk.State
ProviderMeta *tfsdk.Config
ResourceSchema fwschema.Schema
Resource resource.Resource
}
// DeleteResourceResponse is the framework server response for a delete request
// with the ApplyResourceChange RPC.
type DeleteResourceResponse struct {
Diagnostics diag.Diagnostics
NewState *tfsdk.State
Private *privatestate.Data
}
// DeleteResource implements the framework server delete request logic for the
// ApplyResourceChange RPC.
func (s *Server) DeleteResource(ctx context.Context, req *DeleteResourceRequest, resp *DeleteResourceResponse) {
if req == nil {
return
}
if resourceWithConfigure, ok := req.Resource.(resource.ResourceWithConfigure); ok {
logging.FrameworkTrace(ctx, "Resource implements ResourceWithConfigure")
configureReq := resource.ConfigureRequest{
ProviderData: s.ResourceConfigureData,
}
configureResp := resource.ConfigureResponse{}
logging.FrameworkTrace(ctx, "Calling provider defined Resource Configure")
resourceWithConfigure.Configure(ctx, configureReq, &configureResp)
logging.FrameworkTrace(ctx, "Called provider defined Resource Configure")
resp.Diagnostics.Append(configureResp.Diagnostics...)
if resp.Diagnostics.HasError() {
return
}
}
deleteReq := resource.DeleteRequest{
State: tfsdk.State{
Schema: req.ResourceSchema,
Raw: tftypes.NewValue(req.ResourceSchema.Type().TerraformType(ctx), nil),
},
}
deleteResp := resource.DeleteResponse{
State: tfsdk.State{
Schema: req.ResourceSchema,
Raw: tftypes.NewValue(req.ResourceSchema.Type().TerraformType(ctx), nil),
},
}
if req.PriorState != nil {
deleteReq.State = *req.PriorState
deleteResp.State = *req.PriorState
}
if req.ProviderMeta != nil {
deleteReq.ProviderMeta = *req.ProviderMeta
}
if req.PlannedPrivate != nil {
deleteReq.Private = req.PlannedPrivate.Provider
}
logging.FrameworkTrace(ctx, "Calling provider defined Resource Delete")
req.Resource.Delete(ctx, deleteReq, &deleteResp)
logging.FrameworkTrace(ctx, "Called provider defined Resource Delete")
if !deleteResp.Diagnostics.HasError() {
logging.FrameworkTrace(ctx, "No provider defined Delete errors detected, ensuring State is cleared")
deleteResp.State.RemoveResource(ctx)
}
resp.Diagnostics = deleteResp.Diagnostics
resp.NewState = &deleteResp.State
}