Skip to content

Commit 798c24d

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 798c24d

13 files changed

+132
-110
lines changed

actions/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type InventoryCollector interface {
8484
// DriveCollector defines an interface to return disk drive inventory
8585
type DriveCollector interface {
8686
UtilAttributeGetter
87-
Drives(ctx context.Context) ([]*common.Drive, error)
87+
Drives(ctx context.Context) ([]*model.Drive, error)
8888
}
8989

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

actions/inventory.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -402,19 +402,19 @@ func (a *InventoryCollectorAction) CollectDrives(ctx context.Context) (err error
402402

403403
// add drive if it isn't part of the drives slice based on its serial
404404
for _, new := range ndrives {
405-
found := a.findDriveBySerial(new.Serial, a.device.Drives)
405+
found := a.findCommonDriveBySerial(new.Serial, a.device.Drives)
406406
if found != nil && found.Serial != "" {
407407
continue
408408
}
409409

410-
a.device.Drives = append(a.device.Drives, new)
410+
a.device.Drives = append(a.device.Drives, &new.Drive)
411411
}
412412
}
413413

414414
return nil
415415
}
416416

417-
func (a *InventoryCollectorAction) findDriveBySerial(serial string, drives []*common.Drive) *common.Drive {
417+
func (a *InventoryCollectorAction) findCommonDriveBySerial(serial string, drives []*common.Drive) *common.Drive {
418418
for _, drive := range drives {
419419
if strings.EqualFold(serial, drive.Serial) {
420420
return drive
@@ -424,7 +424,17 @@ func (a *InventoryCollectorAction) findDriveBySerial(serial string, drives []*co
424424
return nil
425425
}
426426

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

model/drive.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package model
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 NewDrive(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

+7-13
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ func (l *Lsblk) Attributes() (utilName model.CollectorUtility, absolutePath stri
5757
return "lsblk", l.Executor.CmdPath(), er
5858
}
5959

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-
60+
// Executes lsblk list, parses the output and returns a slice of *model.Drive
61+
func (l *Lsblk) Drives(ctx context.Context) ([]*model.Drive, error) {
6462
out, err := l.list(ctx)
6563
if err != nil {
6664
return nil, err
@@ -73,18 +71,16 @@ func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
7371
return nil, err
7472
}
7573

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

7978
var vendor string
80-
81-
modelTokens := strings.Split(d.Model, " ")
82-
83-
if len(modelTokens) > 1 {
79+
if modelTokens := strings.Split(d.Model, " "); len(modelTokens) > 1 {
8480
vendor = modelTokens[1]
8581
}
8682

87-
drive := &common.Drive{
83+
drives[i] = model.NewDrive(&common.Drive{
8884
Protocol: strings.ToLower(d.Transport),
8985
Common: common.Common{
9086
LogicalName: strings.TrimSpace(d.Device),
@@ -93,9 +89,7 @@ func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
9389
Model: strings.TrimSpace(dModel),
9490
},
9591
StorageControllerDriveID: -1,
96-
}
97-
98-
drives = append(drives, drive)
92+
}, nil)
9993
}
10094

10195
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"
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 = []*model.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

+4-5
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ func (m *Msecli) Attributes() (utilName model.CollectorUtility, absolutePath str
5858
}
5959

6060
// Drives returns a slice of drive components identified
61-
func (m *Msecli) Drives(ctx context.Context) ([]*common.Drive, error) {
61+
func (m *Msecli) Drives(ctx context.Context) ([]*model.Drive, error) {
6262
devices, err := m.Query(ctx)
6363
if err != nil {
6464
return nil, err
6565
}
6666

67-
drives := []*common.Drive{}
68-
69-
for _, d := range devices {
67+
drives := make([]*model.Drive, len(devices))
68+
for i, d := range devices {
7069
item := &common.Drive{
7170
Common: common.Common{
7271
Model: d.ModelNumber,
@@ -80,7 +79,7 @@ func (m *Msecli) Drives(ctx context.Context) ([]*common.Drive, error) {
8079
Type: model.DriveTypeSlug(d.ModelNumber),
8180
}
8281

83-
drives = append(drives, item)
82+
drives[i] = model.NewDrive(item, nil)
8483
}
8584

8685
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"
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 := []*model.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

+5-8
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,15 @@ func (m *Mvcli) StorageControllers(ctx context.Context) ([]*common.StorageContro
187187
return hbas, nil
188188
}
189189

190-
func (m *Mvcli) Drives(ctx context.Context) ([]*common.Drive, error) {
190+
func (m *Mvcli) Drives(ctx context.Context) ([]*model.Drive, error) {
191191
devices, err := m.Info(ctx, "pd")
192192
if err != nil {
193193
return nil, err
194194
}
195195

196-
drives := []*common.Drive{}
197-
198-
for _, d := range devices {
199-
drive := &common.Drive{
196+
drives := make([]*model.Drive, len(devices))
197+
for i, d := range devices {
198+
drives[i] = model.NewDrive(&common.Drive{
200199
Common: common.Common{
201200
Model: d.Model,
202201
Vendor: common.VendorFromString(d.Model),
@@ -211,9 +210,7 @@ func (m *Mvcli) Drives(ctx context.Context) ([]*common.Drive, error) {
211210
Type: m.processDriveType(d.Type, d.SSDType),
212211
NegotiatedSpeedGbps: d.CurrentSpeed,
213212
StorageControllerDriveID: d.ID,
214-
}
215-
216-
drives = append(drives, drive)
213+
}, nil)
217214
}
218215

219216
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"
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 := []*model.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)