Skip to content

Commit

Permalink
model.Device to embed common.Device type
Browse files Browse the repository at this point in the history
The common.Device type is provided by github.com/bmc-toolbox/common
the ironlib device and components are now of the common.Device underlying type.

The changes included updates all Device, Component types and Slug
constants to work with the common types and constants.

Any tooling that now builds on top ironlib, bmclib
can work with a consistent set of Device, Component, Firmware types and
Slug constants. This means theres less work need to be done to transform
between types from both the libraries, and compare slugs/constants.
  • Loading branch information
joelrebel committed Jun 16, 2022
1 parent b1be93a commit d4c1661
Show file tree
Hide file tree
Showing 35 changed files with 3,536 additions and 2,891 deletions.
15 changes: 8 additions & 7 deletions actions/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions
import (
"context"

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

Expand Down Expand Up @@ -32,37 +33,37 @@ type InventoryCollector interface {

// DriveCollector defines an interface to return disk drive inventory
type DriveCollector interface {
Drives(ctx context.Context) ([]*model.Drive, error)
Drives(ctx context.Context) ([]*common.Drive, error)
}

// NICCollector defines an interface to returns NIC inventory
type NICCollector interface {
NICs(ctx context.Context) ([]*model.NIC, error)
NICs(ctx context.Context) ([]*common.NIC, error)
}

// BMCCollector defines an interface to return BMC inventory
type BMCCollector interface {
BMC(ctx context.Context) (*model.BMC, error)
BMC(ctx context.Context) (*common.BMC, error)
}

// CPLDCollector defines an interface to return CPLD inventory
type CPLDCollector interface {
CPLD(ctx context.Context) (*model.CPLD, error)
CPLDs(ctx context.Context) ([]*common.CPLD, error)
}

// BIOSCollector defines an interface to return BIOS inventory
type BIOSCollector interface {
BIOS(ctx context.Context) (*model.BIOS, error)
BIOS(ctx context.Context) (*common.BIOS, error)
}

// StorageControllerCollector defines an interface to returns storage controllers inventory
type StorageControllerCollector interface {
StorageControllers(ctx context.Context) ([]*model.StorageController, error)
StorageControllers(ctx context.Context) ([]*common.StorageController, error)
}

// TPMCollector defines an interface to collect TPM device inventory
type TPMCollector interface {
TPM(ctx context.Context) (*model.TPM, error)
TPMs(ctx context.Context) ([]*common.TPM, error)
}

// Updaters
Expand Down
85 changes: 54 additions & 31 deletions actions/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"strings"

"github.com/bmc-toolbox/common"
"github.com/r3labs/diff/v2"

"github.com/metal-toolbox/ironlib/model"
Expand All @@ -25,9 +26,9 @@ type Collectors struct {
Drives DriveCollector
NICs NICCollector
BMC BMCCollector
CPLD CPLDCollector
CPLDs CPLDCollector
BIOS BIOSCollector
TPM TPMCollector
TPMs TPMCollector
StorageControllers StorageControllerCollector
}

Expand Down Expand Up @@ -67,10 +68,10 @@ func Collect(ctx context.Context, device *model.Device, collectors *Collectors,
}

// register a TPM inventory collector
if collectors.TPM == nil {
if collectors.TPMs == nil {
var err error

collectors.TPM, err = utils.NewDmidecode()
collectors.TPMs, err = utils.NewDmidecode()
if err != nil && failOnError {
return errors.Wrap(err, "error in dmidecode inventory collector")
}
Expand Down Expand Up @@ -101,7 +102,7 @@ func Collect(ctx context.Context, device *model.Device, collectors *Collectors,
}

// Collect CPLD info
err = CPLD(ctx, device.CPLD, collectors.CPLD)
err = CPLDs(ctx, &device.CPLDs, collectors.CPLDs)
if err != nil && failOnError {
return errors.Wrap(err, "error retrieving CPLD inventory")
}
Expand All @@ -113,7 +114,7 @@ func Collect(ctx context.Context, device *model.Device, collectors *Collectors,
}

// Collect TPM info
err = TPM(ctx, device.TPM, collectors.TPM)
err = TPMs(ctx, &device.TPMs, collectors.TPMs)
if err != nil && failOnError {
return errors.Wrap(err, "error retrieving TPM inventory")
}
Expand All @@ -133,15 +134,17 @@ func Collect(ctx context.Context, device *model.Device, collectors *Collectors,
device.BIOS.Model = device.Model
}

if device.CPLD != nil && device.CPLD.Model == "" {
device.CPLD.Model = device.Model
for _, cpld := range device.CPLDs {
if cpld != nil {
cpld.Model = device.Model
}
}

return nil
}

// Drives executes drive collectors and merges the drive smart data into device.[]*Drive
func Drives(ctx context.Context, drives []*model.Drive, c DriveCollector) error {
func Drives(ctx context.Context, drives []*common.Drive, c DriveCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in Drives(): ", r)
Expand Down Expand Up @@ -181,7 +184,7 @@ func Drives(ctx context.Context, drives []*model.Drive, c DriveCollector) error
}

// NICs executes nic collectors and merges the nic data into device.[]*NIC
func NICs(ctx context.Context, nics []*model.NIC, c NICCollector) error {
func NICs(ctx context.Context, nics []*common.NIC, c NICCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in NICs(): ", r)
Expand Down Expand Up @@ -221,7 +224,7 @@ func NICs(ctx context.Context, nics []*model.NIC, c NICCollector) error {
}

// BMC executes the bmc collector and updates device bmc information
func BMC(ctx context.Context, bmc *model.BMC, c BMCCollector) error {
func BMC(ctx context.Context, bmc *common.BMC, c BMCCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in BMC(): ", r)
Expand All @@ -248,36 +251,46 @@ func BMC(ctx context.Context, bmc *model.BMC, c BMCCollector) error {
return nil
}

// CPLD executes the bmc collector and updates device cpld information
func CPLD(ctx context.Context, cpld *model.CPLD, c CPLDCollector) error {
// CPLDs executes the bmc collector and updates device cpld information
func CPLDs(ctx context.Context, cplds *[]*common.CPLD, c CPLDCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in CPLD(): ", r)
log.Println("recovered from panic in CPLDs(): ", r)
}
}()

if c == nil {
return nil
}

ncpld, err := c.CPLD(ctx)
ncplds, err := c.CPLDs(ctx)
if err != nil {
return err
}

changelog, err := diff.Diff(cpld, ncpld)
if err != nil {
return err
// no new cplds identified
if len(ncplds) == 0 {
return nil
}

changelog = vetChanges(changelog)
diff.Patch(changelog, cpld)
// no existing cplds were passed in
if len(*cplds) > 0 {
changelog, err := diff.Diff(cplds, ncplds)
if err != nil {
return err
}

changelog = vetChanges(changelog)
diff.Patch(changelog, cplds)
} else {
*cplds = append(*cplds, ncplds...)
}

return nil
}

// BIOS executes the bios collector and updates device bios information
func BIOS(ctx context.Context, bios *model.BIOS, c BIOSCollector) error {
func BIOS(ctx context.Context, bios *common.BIOS, c BIOSCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in BIOS(): ", r)
Expand Down Expand Up @@ -308,8 +321,8 @@ func BIOS(ctx context.Context, bios *model.BIOS, c BIOSCollector) error {
return nil
}

// TPM executes the TPM collector and updates device TPM information
func TPM(ctx context.Context, tpm *model.TPM, c TPMCollector) error {
// TPMs executes the TPM collector and updates device TPM information
func TPMs(ctx context.Context, tpms *[]*common.TPM, c TPMCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in TPM(): ", r)
Expand All @@ -320,28 +333,38 @@ func TPM(ctx context.Context, tpm *model.TPM, c TPMCollector) error {
return nil
}

ntpm, err := c.TPM(ctx)
ntpms, err := c.TPMs(ctx)
if err != nil {
return err
}

if ntpm == nil {
if ntpms == nil {
return nil
}

changelog, err := diff.Diff(tpm, ntpm)
if err != nil {
return err
// no tpms identified
if len(ntpms) == 0 {
return nil
}

changelog = vetChanges(changelog)
diff.Patch(changelog, tpm)
// no existing tpms were passed in
if len(*tpms) > 0 {
changelog, err := diff.Diff(tpms, ntpms)
if err != nil {
return err
}

changelog = vetChanges(changelog)
diff.Patch(changelog, tpms)
} else {
*tpms = append(*tpms, ntpms...)
}

return nil
}

// StorageControllers executes the StorageControllers collector and updates device storage controller data
func StorageController(ctx context.Context, controllers []*model.StorageController, c StorageControllerCollector) error {
func StorageController(ctx context.Context, controllers []*common.StorageController, c StorageControllerCollector) error {
defer func() {
if r := recover(); r != nil {
log.Println("recovered from panic in StorageController(): ", r)
Expand Down
14 changes: 10 additions & 4 deletions actions/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"os"
"testing"

"gotest.tools/assert"

dellFixtures "github.com/metal-toolbox/ironlib/fixtures/dell"
smcFixtures "github.com/metal-toolbox/ironlib/fixtures/supermicro"

"github.com/metal-toolbox/ironlib/model"
"github.com/metal-toolbox/ironlib/utils"
"gotest.tools/assert"
)

func Test_Inventory_dell(t *testing.T) {
Expand All @@ -28,7 +28,6 @@ func Test_Inventory_dell(t *testing.T) {

lshw := utils.NewFakeLshw(bytes.NewReader(lshwb))
smartctl := utils.NewFakeSmartctl("../fixtures/dell/r6515/smartctl")

collectors := &Collectors{
Inventory: lshw,
Drives: smartctl,
Expand Down Expand Up @@ -94,13 +93,20 @@ func Test_Inventory_smc(t *testing.T) {
ipmicfg1 := utils.NewFakeIpmicfg(bytes.NewReader(ipmicfgb))
ipmicfg2 := utils.NewFakeIpmicfg(bytes.NewReader(ipmicfgb))

// tpms
dmi, err := utils.InitFakeDmidecode("../fixtures/supermicro/x11dph-t/dmidecode/tpm")
if err != nil {
t.Error(err)
}

collectors := &Collectors{
Inventory: lshw,
Drives: smartctl,
NICs: mlxup,
CPLD: ipmicfg0,
CPLDs: ipmicfg0,
BIOS: ipmicfg1,
BMC: ipmicfg2,
TPMs: dmi,
StorageControllers: storecli,
}

Expand Down
Loading

0 comments on commit d4c1661

Please sign in to comment.