-
Notifications
You must be signed in to change notification settings - Fork 32
/
resource.go
208 lines (192 loc) · 5.78 KB
/
resource.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
package fromproto
import (
"fmt"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
)
func ValidateResourceTypeConfigRequest(in *tfplugin5.ValidateResourceTypeConfig_Request) (*tfprotov5.ValidateResourceTypeConfigRequest, error) {
resp := &tfprotov5.ValidateResourceTypeConfigRequest{
TypeName: in.TypeName,
}
if in.Config != nil {
resp.Config = DynamicValue(in.Config)
}
return resp, nil
}
func ValidateResourceTypeConfigResponse(in *tfplugin5.ValidateResourceTypeConfig_Response) (*tfprotov5.ValidateResourceTypeConfigResponse, error) {
diags, err := Diagnostics(in.Diagnostics)
if err != nil {
return nil, err
}
return &tfprotov5.ValidateResourceTypeConfigResponse{
Diagnostics: diags,
}, nil
}
func UpgradeResourceStateRequest(in *tfplugin5.UpgradeResourceState_Request) (*tfprotov5.UpgradeResourceStateRequest, error) {
resp := &tfprotov5.UpgradeResourceStateRequest{
TypeName: in.TypeName,
Version: in.Version,
}
if in.RawState != nil {
resp.RawState = RawState(in.RawState)
}
return resp, nil
}
func UpgradeResourceStateResponse(in *tfplugin5.UpgradeResourceState_Response) (*tfprotov5.UpgradeResourceStateResponse, error) {
diags, err := Diagnostics(in.Diagnostics)
if err != nil {
return nil, err
}
resp := &tfprotov5.UpgradeResourceStateResponse{
Diagnostics: diags,
}
if in.UpgradedState != nil {
resp.UpgradedState = DynamicValue(in.UpgradedState)
}
return resp, nil
}
func ReadResourceRequest(in *tfplugin5.ReadResource_Request) (*tfprotov5.ReadResourceRequest, error) {
resp := &tfprotov5.ReadResourceRequest{
TypeName: in.TypeName,
Private: in.Private,
}
if in.CurrentState != nil {
resp.CurrentState = DynamicValue(in.CurrentState)
}
if in.ProviderMeta != nil {
resp.ProviderMeta = DynamicValue(in.ProviderMeta)
}
return resp, nil
}
func ReadResourceResponse(in *tfplugin5.ReadResource_Response) (*tfprotov5.ReadResourceResponse, error) {
resp := &tfprotov5.ReadResourceResponse{
Private: in.Private,
}
diags, err := Diagnostics(in.Diagnostics)
if err != nil {
return resp, err
}
resp.Diagnostics = diags
if in.NewState != nil {
resp.NewState = DynamicValue(in.NewState)
}
return resp, nil
}
func PlanResourceChangeRequest(in *tfplugin5.PlanResourceChange_Request) (*tfprotov5.PlanResourceChangeRequest, error) {
resp := &tfprotov5.PlanResourceChangeRequest{
TypeName: in.TypeName,
PriorPrivate: in.PriorPrivate,
}
if in.Config != nil {
resp.Config = DynamicValue(in.Config)
}
if in.PriorState != nil {
resp.PriorState = DynamicValue(in.PriorState)
}
if in.ProposedNewState != nil {
resp.ProposedNewState = DynamicValue(in.ProposedNewState)
}
if in.ProviderMeta != nil {
resp.ProviderMeta = DynamicValue(in.ProviderMeta)
}
return resp, nil
}
func PlanResourceChangeResponse(in *tfplugin5.PlanResourceChange_Response) (*tfprotov5.PlanResourceChangeResponse, error) {
resp := &tfprotov5.PlanResourceChangeResponse{
PlannedPrivate: in.PlannedPrivate,
UnsafeToUseLegacyTypeSystem: in.LegacyTypeSystem,
}
attributePaths, err := AttributePaths(in.RequiresReplace)
if err != nil {
return resp, err
}
resp.RequiresReplace = attributePaths
diags, err := Diagnostics(in.Diagnostics)
if err != nil {
return resp, err
}
resp.Diagnostics = diags
if in.PlannedState != nil {
resp.PlannedState = DynamicValue(in.PlannedState)
}
return resp, nil
}
func ApplyResourceChangeRequest(in *tfplugin5.ApplyResourceChange_Request) (*tfprotov5.ApplyResourceChangeRequest, error) {
resp := &tfprotov5.ApplyResourceChangeRequest{
TypeName: in.TypeName,
PlannedPrivate: in.PlannedPrivate,
}
if in.Config != nil {
resp.Config = DynamicValue(in.Config)
}
if in.PriorState != nil {
resp.PriorState = DynamicValue(in.PriorState)
}
if in.PlannedState != nil {
resp.PlannedState = DynamicValue(in.PlannedState)
}
if in.ProviderMeta != nil {
resp.ProviderMeta = DynamicValue(in.ProviderMeta)
}
return resp, nil
}
func ApplyResourceChangeResponse(in *tfplugin5.ApplyResourceChange_Response) (*tfprotov5.ApplyResourceChangeResponse, error) {
resp := &tfprotov5.ApplyResourceChangeResponse{
Private: in.Private,
UnsafeToUseLegacyTypeSystem: in.LegacyTypeSystem,
}
diags, err := Diagnostics(in.Diagnostics)
if err != nil {
return resp, err
}
resp.Diagnostics = diags
if in.NewState != nil {
resp.NewState = DynamicValue(in.NewState)
}
return resp, nil
}
func ImportResourceStateRequest(in *tfplugin5.ImportResourceState_Request) (*tfprotov5.ImportResourceStateRequest, error) {
return &tfprotov5.ImportResourceStateRequest{
TypeName: in.TypeName,
ID: in.Id,
}, nil
}
func ImportResourceStateResponse(in *tfplugin5.ImportResourceState_Response) (*tfprotov5.ImportResourceStateResponse, error) {
imported, err := ImportedResources(in.ImportedResources)
if err != nil {
return nil, err
}
diags, err := Diagnostics(in.Diagnostics)
if err != nil {
return nil, err
}
return &tfprotov5.ImportResourceStateResponse{
ImportedResources: imported,
Diagnostics: diags,
}, nil
}
func ImportedResource(in *tfplugin5.ImportResourceState_ImportedResource) (*tfprotov5.ImportedResource, error) {
resp := &tfprotov5.ImportedResource{
TypeName: in.TypeName,
Private: in.Private,
}
if in.State != nil {
resp.State = DynamicValue(in.State)
}
return resp, nil
}
func ImportedResources(in []*tfplugin5.ImportResourceState_ImportedResource) ([]*tfprotov5.ImportedResource, error) {
resp := make([]*tfprotov5.ImportedResource, 0, len(in))
for pos, i := range in {
if i == nil {
resp = append(resp, nil)
continue
}
r, err := ImportedResource(i)
if err != nil {
return resp, fmt.Errorf("Error converting imported resource %d/%d: %w", pos+1, len(in), err)
}
resp = append(resp, r)
}
return resp, nil
}