Skip to content

Commit

Permalink
Refactor Device to be a BootDeviceType
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeefreak101 committed Nov 21, 2023
1 parent 3ecceab commit b145fcb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
18 changes: 17 additions & 1 deletion bmc/boot_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ import (
"github.com/pkg/errors"
)

type BootDeviceType string

const (
BootDeviceTypeBIOS BootDeviceType = "bios"
BootDeviceTypeCDROM BootDeviceType = "cdrom"
BootDeviceTypeDiag BootDeviceType = "diag"
BootDeviceTypeFloppy BootDeviceType = "floppy"
BootDeviceTypeDisk BootDeviceType = "disk"
BootDeviceTypeNone BootDeviceType = "none"
BootDeviceTypePXE BootDeviceType = "pxe"
BootDeviceTypeRemoteDrive BootDeviceType = "remote_drive"
BootDeviceTypeSDCard BootDeviceType = "sd_card"
BootDeviceTypeUSB BootDeviceType = "usb"
BootDeviceTypeUtil BootDeviceType = "utilities"
)

// BootDeviceSetter sets the next boot device for a machine
type BootDeviceSetter interface {
BootDeviceSet(ctx context.Context, bootDevice string, setPersistent, efiBoot bool) (ok bool, err error)
Expand All @@ -34,7 +50,7 @@ type bootOverrideProvider struct {
type BootDeviceOverride struct {
IsPersistent bool
IsEFIBoot bool
Device string
Device BootDeviceType
}

// setBootDevice sets the next boot device.
Expand Down
2 changes: 1 addition & 1 deletion bmc/boot_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestBootDeviceOverrideGet(t *testing.T) {
successOverride := BootDeviceOverride{
IsPersistent: false,
IsEFIBoot: true,
Device: "disk",
Device: BootDeviceTypeDisk,
}

successMetadata := &Metadata{
Expand Down
74 changes: 37 additions & 37 deletions internal/redfishwrapper/boot_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,75 @@ import (
rf "github.com/stmcginnis/gofish/redfish"
)

type bootDeviceType struct {
Name string
RedFishTarget rf.BootSourceOverrideTarget
type bootDeviceMapping struct {
BootDeviceType bmc.BootDeviceType
RedFishTarget rf.BootSourceOverrideTarget
}

var bootDeviceTypes = []bootDeviceType{
var bootDeviceTypeMappings = []bootDeviceMapping{
{
Name: "bios",
RedFishTarget: rf.BiosSetupBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeBIOS,
RedFishTarget: rf.BiosSetupBootSourceOverrideTarget,
},
{
Name: "cdrom",
RedFishTarget: rf.CdBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeCDROM,
RedFishTarget: rf.CdBootSourceOverrideTarget,
},
{
Name: "diag",
RedFishTarget: rf.DiagsBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeDiag,
RedFishTarget: rf.DiagsBootSourceOverrideTarget,
},
{
Name: "floppy",
RedFishTarget: rf.FloppyBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeFloppy,
RedFishTarget: rf.FloppyBootSourceOverrideTarget,
},
{
Name: "disk",
RedFishTarget: rf.HddBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeDisk,
RedFishTarget: rf.HddBootSourceOverrideTarget,
},
{
Name: "none",
RedFishTarget: rf.NoneBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeNone,
RedFishTarget: rf.NoneBootSourceOverrideTarget,
},
{
Name: "pxe",
RedFishTarget: rf.PxeBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypePXE,
RedFishTarget: rf.PxeBootSourceOverrideTarget,
},
{
Name: "remote_drive",
RedFishTarget: rf.RemoteDriveBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeRemoteDrive,
RedFishTarget: rf.RemoteDriveBootSourceOverrideTarget,
},
{
Name: "sd_card",
RedFishTarget: rf.SDCardBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeSDCard,
RedFishTarget: rf.SDCardBootSourceOverrideTarget,
},
{
Name: "usb",
RedFishTarget: rf.UsbBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeUSB,
RedFishTarget: rf.UsbBootSourceOverrideTarget,
},
{
Name: "utilities",
RedFishTarget: rf.UtilitiesBootSourceOverrideTarget,
BootDeviceType: bmc.BootDeviceTypeUtil,
RedFishTarget: rf.UtilitiesBootSourceOverrideTarget,
},
}

// bootDeviceToTarget gets the RedFish BootSourceOverrideTarget that corresponds to the given device,
// bootDeviceStringToTarget gets the RedFish BootSourceOverrideTarget that corresponds to the given device string,
// or an error if the device is not a RedFish BootSourceOverrideTarget.
func bootDeviceToTarget(device string) (rf.BootSourceOverrideTarget, error) {
for _, bootDevice := range bootDeviceTypes {
if bootDevice.Name == device {
func bootDeviceStringToTarget(device string) (rf.BootSourceOverrideTarget, error) {
for _, bootDevice := range bootDeviceTypeMappings {
if string(bootDevice.BootDeviceType) == device {
return bootDevice.RedFishTarget, nil

Check warning on line 69 in internal/redfishwrapper/boot_device.go

View check run for this annotation

Codecov / codecov/patch

internal/redfishwrapper/boot_device.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}
}
return "", errors.New("invalid boot device")

Check warning on line 72 in internal/redfishwrapper/boot_device.go

View check run for this annotation

Codecov / codecov/patch

internal/redfishwrapper/boot_device.go#L72

Added line #L72 was not covered by tests
}

// bootTargetToDevice converts the redfish boot target to a bmclib supported device string.
// bootTargetToBootDeviceType converts the redfish boot target to a bmc.BootDeviceType.
// if the target is unknown or unsupported, then an error is returned.
func bootTargetToDevice(target rf.BootSourceOverrideTarget) (string, error) {
for _, bootDevice := range bootDeviceTypes {
func bootTargetToBootDeviceType(target rf.BootSourceOverrideTarget) (bmc.BootDeviceType, error) {
for _, bootDevice := range bootDeviceTypeMappings {
if bootDevice.RedFishTarget == target {
return bootDevice.Name, nil
return bootDevice.BootDeviceType, nil

Check warning on line 80 in internal/redfishwrapper/boot_device.go

View check run for this annotation

Codecov / codecov/patch

internal/redfishwrapper/boot_device.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}
}
return "", errors.New("invalid boot device")

Check warning on line 83 in internal/redfishwrapper/boot_device.go

View check run for this annotation

Codecov / codecov/patch

internal/redfishwrapper/boot_device.go#L83

Added line #L83 was not covered by tests
Expand All @@ -97,7 +97,7 @@ func (c *Client) SystemBootDeviceSet(_ context.Context, bootDevice string, setPe
for _, system := range systems {
boot := system.Boot

boot.BootSourceOverrideTarget, err = bootDeviceToTarget(bootDevice)
boot.BootSourceOverrideTarget, err = bootDeviceStringToTarget(bootDevice)
if err != nil {
return false, err

Check warning on line 102 in internal/redfishwrapper/boot_device.go

View check run for this annotation

Codecov / codecov/patch

internal/redfishwrapper/boot_device.go#L100-L102

Added lines #L100 - L102 were not covered by tests
}
Expand Down Expand Up @@ -147,9 +147,9 @@ func (c *Client) GetBootDeviceOverride(_ context.Context) (override bmc.BootDevi
}

boot := system.Boot
bootDevice, err := bootTargetToDevice(boot.BootSourceOverrideTarget)
bootDevice, err := bootTargetToBootDeviceType(boot.BootSourceOverrideTarget)
if err != nil {
bootDevice = string(boot.BootSourceOverrideTarget)
return override, err

Check warning on line 152 in internal/redfishwrapper/boot_device.go

View check run for this annotation

Codecov / codecov/patch

internal/redfishwrapper/boot_device.go#L149-L152

Added lines #L149 - L152 were not covered by tests
}

override = bmc.BootDeviceOverride{
Expand Down

0 comments on commit b145fcb

Please sign in to comment.