Skip to content

Commit cd70a9b

Browse files
committed
Define and use our own Drive type
Going to invert how drive wiping works by having the drive know how to wipe itself, so we need our own type to be able to give it methods.
1 parent d3a0069 commit cd70a9b

13 files changed

+135
-110
lines changed

actions/interface.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/bmc-toolbox/common"
77
"github.com/metal-toolbox/ironlib/model"
8+
"github.com/metal-toolbox/ironlib/model/drive"
89
"github.com/metal-toolbox/ironlib/utils"
910
)
1011

@@ -84,7 +85,7 @@ type InventoryCollector interface {
8485
// DriveCollector defines an interface to return disk drive inventory
8586
type DriveCollector interface {
8687
UtilAttributeGetter
87-
Drives(ctx context.Context) ([]*common.Drive, error)
88+
Drives(ctx context.Context) ([]*drive.Drive, error)
8889
}
8990

9091
// DriveCapabilityCollector defines an interface to collect disk drive capability attributes

actions/inventory.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/bmc-toolbox/common"
1212
"github.com/metal-toolbox/ironlib/firmware"
1313
"github.com/metal-toolbox/ironlib/model"
14+
"github.com/metal-toolbox/ironlib/model/drive"
1415
"github.com/metal-toolbox/ironlib/utils"
1516
"github.com/pkg/errors"
1617
"github.com/r3labs/diff/v3"
@@ -402,19 +403,19 @@ func (a *InventoryCollectorAction) CollectDrives(ctx context.Context) (err error
402403

403404
// add drive if it isn't part of the drives slice based on its serial
404405
for _, new := range ndrives {
405-
found := a.findDriveBySerial(new.Serial, a.device.Drives)
406+
found := a.findCommonDriveBySerial(new.Serial, a.device.Drives)
406407
if found != nil && found.Serial != "" {
407408
continue
408409
}
409410

410-
a.device.Drives = append(a.device.Drives, new)
411+
a.device.Drives = append(a.device.Drives, &new.Drive)
411412
}
412413
}
413414

414415
return nil
415416
}
416417

417-
func (a *InventoryCollectorAction) findDriveBySerial(serial string, drives []*common.Drive) *common.Drive {
418+
func (a *InventoryCollectorAction) findCommonDriveBySerial(serial string, drives []*common.Drive) *common.Drive {
418419
for _, drive := range drives {
419420
if strings.EqualFold(serial, drive.Serial) {
420421
return drive
@@ -424,7 +425,17 @@ func (a *InventoryCollectorAction) findDriveBySerial(serial string, drives []*co
424425
return nil
425426
}
426427

427-
func (a *InventoryCollectorAction) findDriveByLogicalName(logicalName string, drives []*common.Drive) *common.Drive {
428+
func (a *InventoryCollectorAction) findDriveBySerial(serial string, drives []*drive.Drive) *drive.Drive {
429+
for _, drive := range drives {
430+
if strings.EqualFold(serial, drive.Serial) {
431+
return drive
432+
}
433+
}
434+
435+
return nil
436+
}
437+
438+
func (a *InventoryCollectorAction) findDriveByLogicalName(logicalName string, drives []*drive.Drive) *drive.Drive {
428439
for _, drive := range drives {
429440
if strings.EqualFold(logicalName, drive.LogicalName) {
430441
return drive

model/drive/drive.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package drive
2+
3+
import (
4+
"github.com/bmc-toolbox/common"
5+
"github.com/metal-toolbox/ironlib/actions/wipe"
6+
)
7+
8+
type Drive struct {
9+
common.Drive
10+
wipeGetter WipersGetter
11+
}
12+
13+
type WipersGetter interface {
14+
Wipers(*Drive) []wipe.Wiper
15+
}
16+
17+
func New(d *common.Drive, w WipersGetter) *Drive {
18+
if d == nil {
19+
return &Drive{}
20+
}
21+
return &Drive{
22+
Drive: *d,
23+
wipeGetter: w,
24+
}
25+
}
26+
27+
func (d *Drive) Wipers() []wipe.Wiper {
28+
return d.wipeGetter.Wipers(d)
29+
}

utils/lsblk.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/bmc-toolbox/common"
1010
"github.com/metal-toolbox/ironlib/model"
11+
"github.com/metal-toolbox/ironlib/model/drive"
1112
"github.com/pkg/errors"
1213
)
1314

@@ -57,10 +58,8 @@ func (l *Lsblk) Attributes() (utilName model.CollectorUtility, absolutePath stri
5758
return "lsblk", l.Executor.CmdPath(), er
5859
}
5960

60-
// Executes lsblk list, parses the output and returns a slice of *common.Drive
61-
func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
62-
drives := make([]*common.Drive, 0)
63-
61+
// Executes lsblk list, parses the output and returns a slice of *drive.Drive
62+
func (l *Lsblk) Drives(ctx context.Context) ([]*drive.Drive, error) {
6463
out, err := l.list(ctx)
6564
if err != nil {
6665
return nil, err
@@ -73,18 +72,16 @@ func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
7372
return nil, err
7473
}
7574

76-
for _, d := range items["blockdevices"] {
75+
drives := make([]*drive.Drive, len(items["blockdevices"]))
76+
for i, d := range items["blockdevices"] {
7777
dModel := d.Model
7878

7979
var vendor string
80-
81-
modelTokens := strings.Split(d.Model, " ")
82-
83-
if len(modelTokens) > 1 {
80+
if modelTokens := strings.Split(d.Model, " "); len(modelTokens) > 1 {
8481
vendor = modelTokens[1]
8582
}
8683

87-
drive := &common.Drive{
84+
drives[i] = drive.New(&common.Drive{
8885
Protocol: strings.ToLower(d.Transport),
8986
Common: common.Common{
9087
LogicalName: strings.TrimSpace(d.Device),
@@ -93,9 +90,7 @@ func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
9390
Model: strings.TrimSpace(dModel),
9491
},
9592
StorageControllerDriveID: -1,
96-
}
97-
98-
drives = append(drives, drive)
93+
}, nil)
9994
}
10095

10196
return drives, nil

utils/lsblk_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/bmc-toolbox/common"
8+
"github.com/metal-toolbox/ironlib/model/drive"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -19,41 +20,41 @@ func Test_lsblk_Drives(t *testing.T) {
1920
assert.Equal(t, fixtureLsblkDrives, drives)
2021
}
2122

22-
var fixtureLsblkDrives = []*common.Drive{
23-
{
23+
var fixtureLsblkDrives = []*drive.Drive{
24+
{Drive: common.Drive{
2425
Common: common.Common{
2526
Model: "MTFDDAV240TDU",
2627
Serial: "203329F89392",
2728
LogicalName: "/dev/sda",
2829
},
2930
Protocol: "sata",
3031
StorageControllerDriveID: -1,
31-
},
32-
{
32+
}},
33+
{Drive: common.Drive{
3334
Common: common.Common{
3435
Model: "MTFDDAV240TDU",
3536
Serial: "203329F89796",
3637
LogicalName: "/dev/sdb",
3738
},
3839
Protocol: "sata",
3940
StorageControllerDriveID: -1,
40-
},
41-
{
41+
}},
42+
{Drive: common.Drive{
4243
Common: common.Common{
4344
Model: "Micron_9300_MTFDHAL3T8TDP",
4445
Serial: "202728F691F5",
4546
LogicalName: "/dev/nvme0",
4647
},
4748
Protocol: "nvme",
4849
StorageControllerDriveID: -1,
49-
},
50-
{
50+
}},
51+
{Drive: common.Drive{
5152
Common: common.Common{
5253
Model: "Micron_9300_MTFDHAL3T8TDP",
5354
Serial: "202728F691C6",
5455
LogicalName: "/dev/nvme1",
5556
},
5657
Protocol: "nvme",
5758
StorageControllerDriveID: -1,
58-
},
59+
}},
5960
}

utils/msecli.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/bmc-toolbox/common"
1111
"github.com/metal-toolbox/ironlib/model"
12+
"github.com/metal-toolbox/ironlib/model/drive"
1213
"github.com/pkg/errors"
1314
)
1415

@@ -58,15 +59,14 @@ func (m *Msecli) Attributes() (utilName model.CollectorUtility, absolutePath str
5859
}
5960

6061
// Drives returns a slice of drive components identified
61-
func (m *Msecli) Drives(ctx context.Context) ([]*common.Drive, error) {
62+
func (m *Msecli) Drives(ctx context.Context) ([]*drive.Drive, error) {
6263
devices, err := m.Query(ctx)
6364
if err != nil {
6465
return nil, err
6566
}
6667

67-
drives := []*common.Drive{}
68-
69-
for _, d := range devices {
68+
drives := make([]*drive.Drive, len(devices))
69+
for i, d := range devices {
7070
item := &common.Drive{
7171
Common: common.Common{
7272
Model: d.ModelNumber,
@@ -80,7 +80,7 @@ func (m *Msecli) Drives(ctx context.Context) ([]*common.Drive, error) {
8080
Type: model.DriveTypeSlug(d.ModelNumber),
8181
}
8282

83-
drives = append(drives, item)
83+
drives[i] = drive.New(item, nil)
8484
}
8585

8686
return drives, nil

utils/msecli_test.go

+12-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/bmc-toolbox/common"
9+
"github.com/metal-toolbox/ironlib/model/drive"
910
"github.com/pkg/errors"
1011
"github.com/stretchr/testify/assert"
1112
)
@@ -24,36 +25,29 @@ func newFakeMsecli() (*Msecli, error) {
2425
}
2526

2627
func Test_MsecliDrives(t *testing.T) {
27-
expected := []*common.Drive{
28-
{
28+
expected := []*drive.Drive{
29+
{Drive: common.Drive{
2930
Common: common.Common{
3031
Serial: "193423710BDA",
3132
Vendor: "micron",
3233
Model: "Micron_5200_MTFDDAK480TDN",
3334
Description: "Micron_5200_MTFDDAK480TDN",
34-
Firmware: &common.Firmware{
35-
Installed: "D1MU020",
36-
},
37-
Metadata: map[string]string{},
35+
Firmware: &common.Firmware{Installed: "D1MU020"},
36+
Metadata: map[string]string{},
3837
},
39-
4038
Type: common.SlugDriveTypeSATASSD,
41-
},
42-
{
39+
}},
40+
{Drive: common.Drive{
4341
Common: common.Common{
44-
Serial: "193423711167",
45-
Vendor: "micron",
46-
42+
Serial: "193423711167",
43+
Vendor: "micron",
4744
Model: "Micron_5200_MTFDDAK480TDN",
4845
Description: "Micron_5200_MTFDDAK480TDN",
49-
Firmware: &common.Firmware{
50-
Installed: "D1MU020",
51-
},
52-
Metadata: map[string]string{},
46+
Firmware: &common.Firmware{Installed: "D1MU020"},
47+
Metadata: map[string]string{},
5348
},
54-
5549
Type: common.SlugDriveTypeSATASSD,
56-
},
50+
}},
5751
}
5852

5953
m, err := newFakeMsecli()

utils/mvcli.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/bmc-toolbox/common"
1515
"github.com/metal-toolbox/ironlib/model"
16+
"github.com/metal-toolbox/ironlib/model/drive"
1617
"github.com/pkg/errors"
1718
)
1819

@@ -187,16 +188,15 @@ func (m *Mvcli) StorageControllers(ctx context.Context) ([]*common.StorageContro
187188
return hbas, nil
188189
}
189190

190-
func (m *Mvcli) Drives(ctx context.Context) ([]*common.Drive, error) {
191+
func (m *Mvcli) Drives(ctx context.Context) ([]*drive.Drive, error) {
191192
devices, err := m.Info(ctx, "pd")
192193
if err != nil {
193194
return nil, err
194195
}
195196

196-
drives := []*common.Drive{}
197-
198-
for _, d := range devices {
199-
drive := &common.Drive{
197+
drives := make([]*drive.Drive, len(devices))
198+
for i, d := range devices {
199+
drives[i] = drive.New(&common.Drive{
200200
Common: common.Common{
201201
Model: d.Model,
202202
Vendor: common.VendorFromString(d.Model),
@@ -211,9 +211,7 @@ func (m *Mvcli) Drives(ctx context.Context) ([]*common.Drive, error) {
211211
Type: m.processDriveType(d.Type, d.SSDType),
212212
NegotiatedSpeedGbps: d.CurrentSpeed,
213213
StorageControllerDriveID: d.ID,
214-
}
215-
216-
drives = append(drives, drive)
214+
}, nil)
217215
}
218216

219217
return drives, nil

utils/mvcli_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/bmc-toolbox/common"
9+
"github.com/metal-toolbox/ironlib/model/drive"
910
"github.com/stretchr/testify/assert"
1011
)
1112

@@ -61,8 +62,8 @@ func Test_MvcliStorageControllers(t *testing.T) {
6162
}
6263

6364
func Test_MvcliDrives(t *testing.T) {
64-
expected := []*common.Drive{
65-
{
65+
expected := []*drive.Drive{
66+
{Drive: common.Drive{
6667
Common: common.Common{
6768
Description: "MTFDDAV240TCB",
6869
Vendor: "micron",
@@ -80,8 +81,8 @@ func Test_MvcliDrives(t *testing.T) {
8081
Type: common.SlugDriveTypeSATASSD,
8182
NegotiatedSpeedGbps: 6,
8283
StorageControllerDriveID: 0,
83-
},
84-
{
84+
}},
85+
{Drive: common.Drive{
8586
Common: common.Common{
8687
Description: "MTFDDAV240TCB",
8788
Vendor: "micron",
@@ -99,7 +100,7 @@ func Test_MvcliDrives(t *testing.T) {
99100
Type: common.SlugDriveTypeSATASSD,
100101
NegotiatedSpeedGbps: 6,
101102
StorageControllerDriveID: 1,
102-
},
103+
}},
103104
}
104105

105106
cli := newFakeMvcli(t, "info-pd")

0 commit comments

Comments
 (0)