/
framework_resource.go
208 lines (169 loc) · 4.38 KB
/
framework_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 sshkey
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/v2/linode/helper"
)
func NewResource() resource.Resource {
return &Resource{
BaseResource: helper.NewBaseResource(
helper.BaseResourceConfig{
Name: "linode_sshkey",
IDType: types.StringType,
Schema: &frameworkResourceSchema,
},
),
}
}
type Resource struct {
helper.BaseResource
}
func (r *Resource) Create(
ctx context.Context,
req resource.CreateRequest,
resp *resource.CreateResponse,
) {
tflog.Debug(ctx, "Create linode_sshkey")
var data ResourceModel
client := r.Meta.Client
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
createOpts := linodego.SSHKeyCreateOptions{
Label: data.Label.ValueString(),
SSHKey: data.SSHKey.ValueString(),
}
tflog.Debug(ctx, "client.CreateSSHKey(...)", map[string]any{
"options": createOpts,
})
key, err := client.CreateSSHKey(ctx, createOpts)
if err != nil {
resp.Diagnostics.AddError(
"Failed to create SSH Key",
err.Error(),
)
return
}
data.FlattenSSHKey(key, true)
// IDs should always be overridden during creation (see #1085)
// TODO: Remove when Crossplane empty string ID issue is resolved
data.ID = types.StringValue(strconv.Itoa(key.ID))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *Resource) Read(
ctx context.Context,
req resource.ReadRequest,
resp *resource.ReadResponse,
) {
tflog.Debug(ctx, "Create linode_sshkey")
client := r.Meta.Client
var data ResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if helper.FrameworkAttemptRemoveResourceForEmptyID(ctx, data.ID, resp) {
return
}
id := helper.StringToInt(data.ID.ValueString(), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "sshkey_id", id)
tflog.Trace(ctx, "client.GetSSHKey(...)")
key, err := client.GetSSHKey(ctx, id)
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
resp.Diagnostics.AddWarning(
"SSH Key",
fmt.Sprintf(
"Removing SSH Key with ID %v from state because it no longer exists",
id,
),
)
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Unable to refresh the SSH Key",
err.Error(),
)
return
}
data.FlattenSSHKey(key, false)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *Resource) Update(
ctx context.Context,
req resource.UpdateRequest,
resp *resource.UpdateResponse,
) {
tflog.Debug(ctx, "Update linode_sshkey")
var plan, state ResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
id := helper.StringToInt(plan.ID.ValueString(), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
var updateOpts linodego.SSHKeyUpdateOptions
shouldUpdate := false
if !state.Label.Equal(plan.Label) {
updateOpts.Label = plan.Label.ValueString()
shouldUpdate = true
}
if shouldUpdate {
tflog.Debug(ctx, "client.UpdateSSHKey(...)", map[string]any{
"options": updateOpts,
})
key, err := r.Meta.Client.UpdateSSHKey(
ctx,
id,
updateOpts,
)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Failed to update SSH Key (%d)", id),
err.Error())
return
}
plan.FlattenSSHKey(key, true)
}
plan.CopyFrom(state, true)
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *Resource) Delete(
ctx context.Context,
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) {
tflog.Debug(ctx, "Delete linode_sshkey")
var data ResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
id := helper.StringToInt(data.ID.ValueString(), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
client := r.Meta.Client
ctx = tflog.SetField(ctx, "sshkey_id", id)
tflog.Trace(ctx, "client.DeleteSSHKey(...)")
err := client.DeleteSSHKey(ctx, id)
if err != nil {
if lErr, ok := err.(*linodego.Error); (ok && lErr.Code != 404) || !ok {
resp.Diagnostics.AddError(
fmt.Sprintf("Failed to delete the SSH Key (%d)", id),
err.Error(),
)
}
return
}
}