/
resource.go
192 lines (162 loc) · 4.94 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
package project_ssh_key
import (
"context"
"fmt"
"github.com/equinix/equinix-sdk-go/services/metalv1"
equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
"github.com/equinix/terraform-provider-equinix/internal/framework"
"github.com/hashicorp/terraform-plugin-framework/resource"
)
func NewResource() resource.Resource {
return &Resource{
BaseResource: framework.NewBaseResource(
framework.BaseResourceConfig{
Name: "equinix_metal_project_ssh_key",
Schema: GetResourceSchema(),
},
),
}
}
type Resource struct {
framework.BaseResource
}
func (r *Resource) Create(
ctx context.Context,
req resource.CreateRequest,
resp *resource.CreateResponse,
) {
client := r.Meta.NewMetalClientForFramework(ctx, req.ProviderMeta)
// Retrieve values from plan
var plan ResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// Generate API request body from plan
createRequest := &metalv1.SSHKeyCreateInput{
Label: plan.Name.ValueStringPointer(),
Key: plan.PublicKey.ValueStringPointer(),
}
projectId := plan.ProjectID.ValueString()
// Create API resource
key, _, err := client.SSHKeysApi.CreateProjectSSHKey(ctx, projectId).SSHKeyCreateInput(*createRequest).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Failed to create Project SSH Key",
equinix_errors.FriendlyError(err).Error(),
)
return
}
// Parse API response into the Terraform state
resp.Diagnostics.Append(plan.parse(key)...)
if resp.Diagnostics.HasError() {
return
}
// Set state to fully populated data
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *Resource) Read(
ctx context.Context,
req resource.ReadRequest,
resp *resource.ReadResponse,
) {
client := r.Meta.NewMetalClientForFramework(ctx, req.ProviderMeta)
// Retrieve values from state
var state ResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// Extract the ID of the resource from the state
id := state.ID.ValueString()
// Use API client to get the current state of the resource
key, _, err := client.SSHKeysApi.FindSSHKeyById(ctx, id).Include(nil).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)
// If the key is somehow already destroyed, mark as
// succesfully gone
if equinix_errors.IsNotFound(err) {
resp.Diagnostics.AddWarning(
"Equinix Metal Project SSHKey not found during refresh",
fmt.Sprintf("[WARN] SSHKey (%s) not found, removing from state", id),
)
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
fmt.Sprintf("Failed to get Project SSHKey %s", id),
err.Error(),
)
}
// Set state to fully populated data
resp.Diagnostics.Append(state.parse(key)...)
if resp.Diagnostics.HasError() {
return
}
// Update the Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *Resource) Update(
ctx context.Context,
req resource.UpdateRequest,
resp *resource.UpdateResponse,
) {
client := r.Meta.NewMetalClientForFramework(ctx, req.ProviderMeta)
// Retrieve values from plan
var state, plan ResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// Extract the ID of the resource from the state
id := plan.ID.ValueString()
updateRequest := &metalv1.SSHKeyInput{}
if !state.Name.Equal(plan.Name) {
updateRequest.Label = plan.Name.ValueStringPointer()
}
if !state.PublicKey.Equal(plan.PublicKey) {
updateRequest.Key = plan.PublicKey.ValueStringPointer()
}
// Update the resource
key, _, err := client.SSHKeysApi.UpdateSSHKey(ctx, id).SSHKeyInput(*updateRequest).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
"Error updating resource",
"Could not update resource with ID "+id+": "+err.Error(),
)
return
}
// Set state to fully populated data
resp.Diagnostics.Append(plan.parse(key)...)
if resp.Diagnostics.HasError() {
return
}
// Read the updated state back into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *Resource) Delete(
ctx context.Context,
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) {
client := r.Meta.NewMetalClientForFramework(ctx, req.ProviderMeta)
// Retrieve values from plan
var state ResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// Extract the ID of the resource from the state
id := state.ID.ValueString()
// Use API client to delete the resource
deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute()
if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
fmt.Sprintf("Failed to delete Project SSHKey %s", id),
err.Error(),
)
}
}