Skip to content

Commit a5e3113

Browse files
authored
This filters the boot disk out of attachments (#444)
1 parent f803d8b commit a5e3113

2 files changed

Lines changed: 99 additions & 4 deletions

File tree

internal/provider/resource_instance.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
411411
if resp.Diagnostics.HasError() {
412412
return
413413
}
414-
params.Body.Disks = disks
414+
415+
// The control plane API counts the BootDisk and the Disk attachments when it calculates the limit on disk attachments.
416+
// If bootdisk is set explicitly, we don't want it to be in the API call, but we need it in the state entry.
417+
params.Body.Disks = filterBootDiskFromDisks(disks, params.Body.BootDisk)
415418

416419
externalIPs := newExternalIPsOnCreate(plan.ExternalIPs)
417420
params.Body.ExternalIps = externalIPs
@@ -1178,6 +1181,17 @@ func newDiskAttachmentsOnCreate(ctx context.Context, client *oxide.Client, diskI
11781181
return disks, diags
11791182
}
11801183

1184+
func filterBootDiskFromDisks(disks []oxide.InstanceDiskAttachment, boot_disk *oxide.InstanceDiskAttachment) []oxide.InstanceDiskAttachment {
1185+
var filtered_disks = []oxide.InstanceDiskAttachment{}
1186+
for _, disk := range disks {
1187+
if disk == *boot_disk {
1188+
continue
1189+
}
1190+
filtered_disks = append(filtered_disks, disk)
1191+
}
1192+
return filtered_disks
1193+
}
1194+
11811195
func newExternalIPsOnCreate(externalIPs []instanceResourceExternalIPModel) []oxide.ExternalIpCreate {
11821196
var ips []oxide.ExternalIpCreate
11831197

internal/provider/resource_instance_test.go

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package provider
77
import (
88
"context"
99
"fmt"
10+
"reflect"
1011
"testing"
1112
"time"
1213

@@ -382,13 +383,13 @@ func TestAccCloudResourceInstance_nic(t *testing.T) {
382383
data "oxide_project" "{{.SupportBlockName}}" {
383384
name = "tf-acc-test"
384385
}
385-
386+
386387
data "oxide_vpc_subnet" "{{.SubnetBlockName}}" {
387388
project_name = data.oxide_project.{{.SupportBlockName}}.name
388389
vpc_name = "default"
389390
name = "default"
390391
}
391-
392+
392393
resource "oxide_instance" "{{.BlockName}}" {
393394
project_id = data.oxide_project.{{.SupportBlockName}}.id
394395
description = "a test instance"
@@ -412,7 +413,7 @@ resource "oxide_instance" "{{.BlockName}}" {
412413
data "oxide_project" "{{.SupportBlockName}}" {
413414
name = "tf-acc-test"
414415
}
415-
416+
416417
resource "oxide_instance" "{{.BlockName}}" {
417418
project_id = data.oxide_project.{{.SupportBlockName}}.id
418419
description = "a test instance"
@@ -1227,3 +1228,83 @@ func testAccInstanceDestroy(s *terraform.State) error {
12271228

12281229
return nil
12291230
}
1231+
1232+
func TestFilterBootDiskFromDisks(t *testing.T) {
1233+
boot_disk := oxide.InstanceDiskAttachment{
1234+
Name: "testboot01",
1235+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1236+
}
1237+
1238+
tests := []struct {
1239+
boot_disk oxide.InstanceDiskAttachment
1240+
disks []oxide.InstanceDiskAttachment
1241+
want []oxide.InstanceDiskAttachment
1242+
}{
1243+
{
1244+
boot_disk: boot_disk,
1245+
disks: []oxide.InstanceDiskAttachment{
1246+
boot_disk,
1247+
{
1248+
Name: "testdisk01",
1249+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1250+
},
1251+
{
1252+
Name: "testdisk01",
1253+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1254+
},
1255+
},
1256+
want: []oxide.InstanceDiskAttachment{
1257+
{
1258+
Name: "testdisk01",
1259+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1260+
},
1261+
{
1262+
Name: "testdisk01",
1263+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1264+
},
1265+
},
1266+
},
1267+
{
1268+
boot_disk: oxide.InstanceDiskAttachment{},
1269+
disks: []oxide.InstanceDiskAttachment{
1270+
{
1271+
Name: "testdisk01",
1272+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1273+
},
1274+
{
1275+
Name: "testdisk01",
1276+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1277+
},
1278+
{
1279+
Name: "testboot01",
1280+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1281+
},
1282+
},
1283+
want: []oxide.InstanceDiskAttachment{
1284+
{
1285+
Name: "testdisk01",
1286+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1287+
},
1288+
{
1289+
Name: "testdisk01",
1290+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1291+
},
1292+
{
1293+
Name: "testboot01",
1294+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1295+
},
1296+
},
1297+
},
1298+
{
1299+
boot_disk: oxide.InstanceDiskAttachment{},
1300+
disks: []oxide.InstanceDiskAttachment{},
1301+
want: []oxide.InstanceDiskAttachment{},
1302+
},
1303+
}
1304+
for _, tt := range tests {
1305+
disks := filterBootDiskFromDisks(tt.disks, &tt.boot_disk)
1306+
if !reflect.DeepEqual(disks, tt.want) {
1307+
t.Errorf("want: %+v, got: %+v", tt.want, disks)
1308+
}
1309+
}
1310+
}

0 commit comments

Comments
 (0)