Skip to content

Commit

Permalink
Define and use our own Drive type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mmlb committed Jun 4, 2024
1 parent d3a0069 commit 44d8d0b
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 110 deletions.
2 changes: 1 addition & 1 deletion actions/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type InventoryCollector interface {
// DriveCollector defines an interface to return disk drive inventory
type DriveCollector interface {
UtilAttributeGetter
Drives(ctx context.Context) ([]*common.Drive, error)
Drives(ctx context.Context) ([]*model.Drive, error)
}

// DriveCapabilityCollector defines an interface to collect disk drive capability attributes
Expand Down
18 changes: 14 additions & 4 deletions actions/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,19 @@ func (a *InventoryCollectorAction) CollectDrives(ctx context.Context) (err error

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

a.device.Drives = append(a.device.Drives, new)
a.device.Drives = append(a.device.Drives, &new.Drive)
}
}

return nil
}

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

func (a *InventoryCollectorAction) findDriveByLogicalName(logicalName string, drives []*common.Drive) *common.Drive {
func (a *InventoryCollectorAction) findDriveBySerial(serial string, drives []*model.Drive) *model.Drive {
for _, drive := range drives {
if strings.EqualFold(serial, drive.Serial) {
return drive
}
}

return nil
}

func (a *InventoryCollectorAction) findDriveByLogicalName(logicalName string, drives []*model.Drive) *model.Drive {
for _, drive := range drives {
if strings.EqualFold(logicalName, drive.LogicalName) {
return drive
Expand Down
32 changes: 32 additions & 0 deletions model/drive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package model

import (
"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/actions/wipe"
)

type Drive struct {
common.Drive
wipersGetter WipersGetter
}

type WipersGetter interface {
Wipers(*Drive) []wipe.Wiper
}

func NewDrive(d *common.Drive, w WipersGetter) *Drive {
if d == nil {
return &Drive{}
}
return &Drive{
Drive: *d,
wipersGetter: w,
}
}

func (d *Drive) Wipers() []wipe.Wiper {
if d.wipersGetter == nil {
return nil
}
return d.wipersGetter.Wipers(d)
}
20 changes: 7 additions & 13 deletions utils/lsblk.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ func (l *Lsblk) Attributes() (utilName model.CollectorUtility, absolutePath stri
return "lsblk", l.Executor.CmdPath(), er
}

// Executes lsblk list, parses the output and returns a slice of *common.Drive
func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
drives := make([]*common.Drive, 0)

// Executes lsblk list, parses the output and returns a slice of *model.Drive
func (l *Lsblk) Drives(ctx context.Context) ([]*model.Drive, error) {
out, err := l.list(ctx)
if err != nil {
return nil, err
Expand All @@ -73,18 +71,16 @@ func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
return nil, err
}

for _, d := range items["blockdevices"] {
drives := make([]*model.Drive, len(items["blockdevices"]))
for i, d := range items["blockdevices"] {
dModel := d.Model

var vendor string

modelTokens := strings.Split(d.Model, " ")

if len(modelTokens) > 1 {
if modelTokens := strings.Split(d.Model, " "); len(modelTokens) > 1 {
vendor = modelTokens[1]
}

drive := &common.Drive{
drives[i] = model.NewDrive(&common.Drive{
Protocol: strings.ToLower(d.Transport),
Common: common.Common{
LogicalName: strings.TrimSpace(d.Device),
Expand All @@ -93,9 +89,7 @@ func (l *Lsblk) Drives(ctx context.Context) ([]*common.Drive, error) {
Model: strings.TrimSpace(dModel),
},
StorageControllerDriveID: -1,
}

drives = append(drives, drive)
}, nil)
}

return drives, nil
Expand Down
19 changes: 10 additions & 9 deletions utils/lsblk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/model"
"github.com/stretchr/testify/assert"
)

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

var fixtureLsblkDrives = []*common.Drive{
{
var fixtureLsblkDrives = []*model.Drive{
{Drive: common.Drive{
Common: common.Common{
Model: "MTFDDAV240TDU",
Serial: "203329F89392",
LogicalName: "/dev/sda",
},
Protocol: "sata",
StorageControllerDriveID: -1,
},
{
}},
{Drive: common.Drive{
Common: common.Common{
Model: "MTFDDAV240TDU",
Serial: "203329F89796",
LogicalName: "/dev/sdb",
},
Protocol: "sata",
StorageControllerDriveID: -1,
},
{
}},
{Drive: common.Drive{
Common: common.Common{
Model: "Micron_9300_MTFDHAL3T8TDP",
Serial: "202728F691F5",
LogicalName: "/dev/nvme0",
},
Protocol: "nvme",
StorageControllerDriveID: -1,
},
{
}},
{Drive: common.Drive{
Common: common.Common{
Model: "Micron_9300_MTFDHAL3T8TDP",
Serial: "202728F691C6",
LogicalName: "/dev/nvme1",
},
Protocol: "nvme",
StorageControllerDriveID: -1,
},
}},
}
9 changes: 4 additions & 5 deletions utils/msecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ func (m *Msecli) Attributes() (utilName model.CollectorUtility, absolutePath str
}

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

drives := []*common.Drive{}

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

drives = append(drives, item)
drives[i] = model.NewDrive(item, nil)
}

return drives, nil
Expand Down
30 changes: 12 additions & 18 deletions utils/msecli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/model"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
Expand All @@ -24,36 +25,29 @@ func newFakeMsecli() (*Msecli, error) {
}

func Test_MsecliDrives(t *testing.T) {
expected := []*common.Drive{
{
expected := []*model.Drive{
{Drive: common.Drive{
Common: common.Common{
Serial: "193423710BDA",
Vendor: "micron",
Model: "Micron_5200_MTFDDAK480TDN",
Description: "Micron_5200_MTFDDAK480TDN",
Firmware: &common.Firmware{
Installed: "D1MU020",
},
Metadata: map[string]string{},
Firmware: &common.Firmware{Installed: "D1MU020"},
Metadata: map[string]string{},
},

Type: common.SlugDriveTypeSATASSD,
},
{
}},
{Drive: common.Drive{
Common: common.Common{
Serial: "193423711167",
Vendor: "micron",

Serial: "193423711167",
Vendor: "micron",
Model: "Micron_5200_MTFDDAK480TDN",
Description: "Micron_5200_MTFDDAK480TDN",
Firmware: &common.Firmware{
Installed: "D1MU020",
},
Metadata: map[string]string{},
Firmware: &common.Firmware{Installed: "D1MU020"},
Metadata: map[string]string{},
},

Type: common.SlugDriveTypeSATASSD,
},
}},
}

m, err := newFakeMsecli()
Expand Down
13 changes: 5 additions & 8 deletions utils/mvcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,15 @@ func (m *Mvcli) StorageControllers(ctx context.Context) ([]*common.StorageContro
return hbas, nil
}

func (m *Mvcli) Drives(ctx context.Context) ([]*common.Drive, error) {
func (m *Mvcli) Drives(ctx context.Context) ([]*model.Drive, error) {
devices, err := m.Info(ctx, "pd")
if err != nil {
return nil, err
}

drives := []*common.Drive{}

for _, d := range devices {
drive := &common.Drive{
drives := make([]*model.Drive, len(devices))
for i, d := range devices {
drives[i] = model.NewDrive(&common.Drive{
Common: common.Common{
Model: d.Model,
Vendor: common.VendorFromString(d.Model),
Expand All @@ -211,9 +210,7 @@ func (m *Mvcli) Drives(ctx context.Context) ([]*common.Drive, error) {
Type: m.processDriveType(d.Type, d.SSDType),
NegotiatedSpeedGbps: d.CurrentSpeed,
StorageControllerDriveID: d.ID,
}

drives = append(drives, drive)
}, nil)
}

return drives, nil
Expand Down
11 changes: 6 additions & 5 deletions utils/mvcli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/bmc-toolbox/common"
"github.com/metal-toolbox/ironlib/model"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -61,8 +62,8 @@ func Test_MvcliStorageControllers(t *testing.T) {
}

func Test_MvcliDrives(t *testing.T) {
expected := []*common.Drive{
{
expected := []*model.Drive{
{Drive: common.Drive{
Common: common.Common{
Description: "MTFDDAV240TCB",
Vendor: "micron",
Expand All @@ -80,8 +81,8 @@ func Test_MvcliDrives(t *testing.T) {
Type: common.SlugDriveTypeSATASSD,
NegotiatedSpeedGbps: 6,
StorageControllerDriveID: 0,
},
{
}},
{Drive: common.Drive{
Common: common.Common{
Description: "MTFDDAV240TCB",
Vendor: "micron",
Expand All @@ -99,7 +100,7 @@ func Test_MvcliDrives(t *testing.T) {
Type: common.SlugDriveTypeSATASSD,
NegotiatedSpeedGbps: 6,
StorageControllerDriveID: 1,
},
}},
}

cli := newFakeMvcli(t, "info-pd")
Expand Down
Loading

0 comments on commit 44d8d0b

Please sign in to comment.