/
resource_disk.go
344 lines (310 loc) · 11.2 KB
/
resource_disk.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/oxidecomputer/oxide.go/oxide"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = (*diskResource)(nil)
_ resource.ResourceWithConfigure = (*diskResource)(nil)
)
// NewDiskResource is a helper function to simplify the provider implementation.
func NewDiskResource() resource.Resource {
return &diskResource{}
}
// diskResource is the resource implementation.
type diskResource struct {
client *oxide.Client
}
type diskResourceModel struct {
BlockSize types.Int64 `tfsdk:"block_size"`
Description types.String `tfsdk:"description"`
DevicePath types.String `tfsdk:"device_path"`
ID types.String `tfsdk:"id"`
SourceImageID types.String `tfsdk:"source_image_id"`
Name types.String `tfsdk:"name"`
ProjectID types.String `tfsdk:"project_id"`
Size types.Int64 `tfsdk:"size"`
SourceSnapshotID types.String `tfsdk:"source_snapshot_id"`
TimeCreated types.String `tfsdk:"time_created"`
TimeModified types.String `tfsdk:"time_modified"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
// Metadata returns the resource type name.
func (r *diskResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "oxide_disk"
}
// Configure adds the provider configured client to the data source.
func (r *diskResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(*oxide.Client)
}
func (r *diskResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
// Schema defines the schema for the resource.
func (r *diskResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
Required: true,
Description: "ID of the project that will contain the disk.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"name": schema.StringAttribute{
Required: true,
Description: "Name of the disk.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"size": schema.Int64Attribute{
Required: true,
Description: "Size of the disk in bytes.",
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Required: true,
Description: "Description for the disk.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"source_image_id": schema.StringAttribute{
Optional: true,
Description: "Image ID of the disk source if applicable.",
Validators: []validator.String{
stringvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("block_size"),
}...),
stringvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("source_snapshot_id"),
}...),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"source_snapshot_id": schema.StringAttribute{
Optional: true,
Description: "Snapshot ID of the disk source if applicable.",
Validators: []validator.String{
stringvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("block_size"),
}...),
stringvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("source_image_id"),
}...),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"block_size": schema.Int64Attribute{
Optional: true,
Computed: true,
Description: "Size of blocks in bytes.",
Validators: []validator.Int64{
int64validator.ConflictsWith(path.Expressions{
path.MatchRoot("source_image_id"),
}...),
int64validator.ConflictsWith(path.Expressions{
path.MatchRoot("source_snapshot_id"),
}...),
},
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"timeouts": timeouts.Attributes(ctx, timeouts.Opts{
Create: true,
Read: true,
// TODO: Restore once updates are enabled
// Update: true,
Delete: true,
}),
"device_path": schema.StringAttribute{
Computed: true,
Description: "Path of the disk.",
},
"id": schema.StringAttribute{
Computed: true,
Description: "Unique, immutable, system-controlled identifier of the image.",
},
"time_created": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this image was created.",
},
"time_modified": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this image was last modified.",
},
},
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *diskResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan diskResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
createTimeout, diags := plan.Timeouts.Create(ctx, defaultTimeout())
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, createTimeout)
defer cancel()
params := oxide.DiskCreateParams{
Project: oxide.NameOrId(plan.ProjectID.ValueString()),
Body: &oxide.DiskCreate{
Description: plan.Description.ValueString(),
Name: oxide.Name(plan.Name.ValueString()),
Size: oxide.ByteCount(plan.Size.ValueInt64()),
},
}
ds := oxide.DiskSource{}
if !plan.SourceImageID.IsNull() {
ds.ImageId = plan.SourceImageID.ValueString()
ds.Type = oxide.DiskSourceTypeImage
} else if !plan.SourceSnapshotID.IsNull() {
ds.SnapshotId = plan.SourceSnapshotID.ValueString()
ds.Type = oxide.DiskSourceTypeSnapshot
} else if !plan.BlockSize.IsNull() {
ds.BlockSize = oxide.BlockSize(plan.BlockSize.ValueInt64())
ds.Type = oxide.DiskSourceTypeBlank
}
params.Body.DiskSource = ds
disk, err := r.client.DiskCreate(ctx, params)
if err != nil {
resp.Diagnostics.AddError(
"Error creating disk",
"API error: "+err.Error(),
)
return
}
tflog.Trace(ctx, fmt.Sprintf("created disk with ID: %v", disk.Id), map[string]any{"success": true})
// Map response body to schema and populate Computed attribute values
plan.ID = types.StringValue(disk.Id)
plan.DevicePath = types.StringValue(disk.DevicePath)
plan.BlockSize = types.Int64Value(int64(disk.BlockSize))
plan.TimeCreated = types.StringValue(disk.TimeCreated.String())
plan.TimeModified = types.StringValue(disk.TimeModified.String())
// Save plan into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
}
// Read refreshes the Terraform state with the latest data.
func (r *diskResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state diskResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
readTimeout, diags := state.Timeouts.Read(ctx, defaultTimeout())
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, readTimeout)
defer cancel()
params := oxide.DiskViewParams{
Disk: oxide.NameOrId(state.ID.ValueString()),
}
disk, err := r.client.DiskView(ctx, params)
if err != nil {
if is404(err) {
// Remove resource from state during a refresh
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Unable to read disk:",
"API error: "+err.Error(),
)
return
}
tflog.Trace(ctx, fmt.Sprintf("read disk with ID: %v", disk.Id), map[string]any{"success": true})
state.BlockSize = types.Int64Value(int64(disk.BlockSize))
state.Description = types.StringValue(disk.Description)
state.DevicePath = types.StringValue(disk.DevicePath)
state.ID = types.StringValue(disk.Id)
state.Name = types.StringValue(string(disk.Name))
state.ProjectID = types.StringValue(disk.ProjectId)
state.Size = types.Int64Value(int64(disk.Size))
state.TimeCreated = types.StringValue(disk.TimeCreated.String())
state.TimeModified = types.StringValue(disk.TimeModified.String())
// Only set SourceImageID and SourceSnapshotID if they've been set to avoid unintentional drift
if disk.ImageId != "" {
state.SourceImageID = types.StringValue(disk.ImageId)
}
if disk.SnapshotId != "" {
state.SourceSnapshotID = types.StringValue(disk.SnapshotId)
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *diskResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError(
"Error updating disk",
"the oxide API currently does not support updating disks")
}
// Delete deletes the resource and removes the Terraform state on success.
func (r *diskResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state diskResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
deleteTimeout, diags := state.Timeouts.Delete(ctx, defaultTimeout())
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, deleteTimeout)
defer cancel()
params := oxide.DiskDeleteParams{
Disk: oxide.NameOrId(state.ID.ValueString()),
}
if err := r.client.DiskDelete(ctx, params); err != nil {
if !is404(err) {
resp.Diagnostics.AddError(
"Unable to delete disk:",
"API error: "+err.Error(),
)
return
}
}
tflog.Trace(ctx, fmt.Sprintf("deleted disk with ID: %v", state.ID.ValueString()), map[string]any{"success": true})
}