-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathresource.go
More file actions
559 lines (508 loc) · 17.2 KB
/
Copy pathresource.go
File metadata and controls
559 lines (508 loc) · 17.2 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// 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 disk
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/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"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/stringdefault"
"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"
"github.com/oxidecomputer/terraform-provider-oxide/internal/provider/shared"
oxidevalidator "github.com/oxidecomputer/terraform-provider-oxide/internal/provider/validator"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = (*Resource)(nil)
_ resource.ResourceWithConfigure = (*Resource)(nil)
_ resource.ResourceWithConfigValidators = (*Resource)(nil)
)
// NewResource is a helper function to simplify the provider implementation.
func NewResource() resource.Resource {
return &Resource{}
}
// Resource is the resource implementation.
type Resource struct {
client *oxide.Client
}
type ResourceModel struct {
BlockSize types.Int64 `tfsdk:"block_size"`
Description types.String `tfsdk:"description"`
DevicePath types.String `tfsdk:"device_path"`
DiskType types.String `tfsdk:"disk_type"`
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"`
ReadOnly types.Bool `tfsdk:"read_only"`
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 *Resource) 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 *Resource) Configure(
_ context.Context,
req resource.ConfigureRequest,
_ *resource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(*oxide.Client)
}
// ImportState imports an existing disk resource into Terraform state.
func (r *Resource) ImportState(
ctx context.Context,
req resource.ImportStateRequest,
resp *resource.ImportStateResponse,
) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
// ConfigValidators returns the config validators for the resource.
func (r *Resource) ConfigValidators(_ context.Context) []resource.ConfigValidator {
return []resource.ConfigValidator{
&LocalSourceValidator{},
&ReadOnlySourceValidator{},
}
}
// Schema defines the schema for the resource.
func (r *Resource) Schema(
ctx context.Context,
_ resource.SchemaRequest,
resp *resource.SchemaResponse,
) {
resp.Schema = schema.Schema{
MarkdownDescription: shared.ReplaceBackticks(`
This resource manages disks.
To create a blank disk it's necessary to set ''block_size''. Otherwise, one of ''source_image_id'' or ''source_snapshot_id'' must be set; ''block_size'' will be automatically calculated.
!> Disks cannot be deleted while attached to instances. Please detach or delete associated instances before attempting to delete.
-> This resource currently only provides create, read and delete actions. An update requires a resource replacement
`),
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
Required: true,
Description: "ID of the project that will contain the disk.",
Validators: []validator.String{
oxidevalidator.IsUUID(),
},
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{
oxidevalidator.IsUUID(),
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{
oxidevalidator.IsUUID(),
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"),
}...),
int64validator.OneOf(512, 2048, 4096),
},
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"disk_type": schema.StringAttribute{
Optional: true,
Computed: true,
Description: `Type of disk. Must be one of "distributed" or "local". Defaults to "distributed".`,
Default: stringdefault.StaticString(string(oxide.DiskBackendTypeDistributed)),
Validators: []validator.String{
stringvalidator.OneOf(
string(oxide.DiskBackendTypeDistributed),
string(oxide.DiskBackendTypeLocal),
),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"read_only": schema.BoolAttribute{
Optional: true,
Computed: true,
Description: `Whether the disk is read-only. Defaults to "false".`,
Default: booldefault.StaticBool(false),
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.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 disk.",
},
"time_created": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this disk was created.",
},
"time_modified": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this disk was last modified.",
},
},
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *Resource) Create(
ctx context.Context,
req resource.CreateRequest,
resp *resource.CreateResponse,
) {
var plan ResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
createTimeout, diags := plan.Timeouts.Create(ctx, shared.DefaultTimeout())
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, createTimeout)
defer cancel()
var diskBackend oxide.DiskBackend
switch oxide.DiskBackendType(plan.DiskType.ValueString()) {
case oxide.DiskBackendTypeDistributed:
var ds oxide.DiskSource
if !plan.SourceImageID.IsNull() {
ds = oxide.DiskSource{Value: &oxide.DiskSourceImage{
ImageId: plan.SourceImageID.ValueString(),
ReadOnly: plan.ReadOnly.ValueBoolPointer(),
}}
} else if !plan.SourceSnapshotID.IsNull() {
ds = oxide.DiskSource{Value: &oxide.DiskSourceSnapshot{
SnapshotId: plan.SourceSnapshotID.ValueString(),
ReadOnly: plan.ReadOnly.ValueBoolPointer(),
}}
} else if !plan.BlockSize.IsNull() {
ds = oxide.DiskSource{Value: &oxide.DiskSourceBlank{
BlockSize: oxide.BlockSize(plan.BlockSize.ValueInt64()),
}}
}
diskBackend = oxide.DiskBackend{Value: &oxide.DiskBackendDistributed{
DiskSource: ds,
}}
case oxide.DiskBackendTypeLocal:
diskBackend = oxide.DiskBackend{Value: &oxide.DiskBackendLocal{}}
default:
resp.Diagnostics.AddError(
"Invalid disk type",
fmt.Sprintf("Unexpected disk type: %s", plan.DiskType.ValueString()),
)
return
}
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()),
DiskBackend: diskBackend,
},
}
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.DiskType = types.StringValue(string(disk.DiskType))
plan.ReadOnly = types.BoolPointerValue(disk.ReadOnly)
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 *Resource) Read(
ctx context.Context,
req resource.ReadRequest,
resp *resource.ReadResponse,
) {
var state ResourceModel
// 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, shared.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 shared.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.DiskType = types.StringValue(string(disk.DiskType))
state.ReadOnly = types.BoolPointerValue(disk.ReadOnly)
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 *Resource) 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 *Resource) Delete(
ctx context.Context,
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) {
var state ResourceModel
// 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, shared.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 !shared.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},
)
}
// LocalSourceValidator validates that source-related fields are not set when disk_type is
// "local".
type LocalSourceValidator struct{}
func (v *LocalSourceValidator) Description(_ context.Context) string {
return `Validates that source_image_id, source_snapshot_id, block_size, and read_only are not set when disk_type is "local".`
}
func (v *LocalSourceValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
func (v *LocalSourceValidator) ValidateResource(
ctx context.Context,
req resource.ValidateConfigRequest,
resp *resource.ValidateConfigResponse,
) {
var config ResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
if config.DiskType.IsNull() || config.DiskType.IsUnknown() ||
config.DiskType.ValueString() != string(oxide.DiskBackendTypeLocal) {
return
}
if !config.SourceImageID.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("source_image_id"),
"Invalid configuration",
`"source_image_id" cannot be set when disk_type is "local".`,
)
}
if !config.SourceSnapshotID.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("source_snapshot_id"),
"Invalid configuration",
`"source_snapshot_id" cannot be set when disk_type is "local".`,
)
}
if !config.BlockSize.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("block_size"),
"Invalid configuration",
`"block_size" cannot be set when disk_type is "local".`,
)
}
if !config.ReadOnly.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("read_only"),
"Invalid configuration",
`"read_only" cannot be set when disk_type is "local".`,
)
}
}
// ReadOnlySourceValidator validates that one of source_image_id or source_snapshot_id is set
// when read_only is set.
type ReadOnlySourceValidator struct{}
func (v *ReadOnlySourceValidator) Description(_ context.Context) string {
return `Validates that one of source_image_id or source_snapshot_id is set when read_only is set.`
}
func (v *ReadOnlySourceValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
func (v *ReadOnlySourceValidator) ValidateResource(
ctx context.Context,
req resource.ValidateConfigRequest,
resp *resource.ValidateConfigResponse,
) {
var config ResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
if config.ReadOnly.IsNull() || config.ReadOnly.IsUnknown() || !config.ReadOnly.ValueBool() {
return
}
if config.SourceImageID.IsNull() && config.SourceSnapshotID.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("read_only"),
"Invalid configuration",
`"read_only" requires "source_image_id" or "source_snapshot_id" to be set.`,
)
}
}