Skip to content

Commit

Permalink
add BMC address to inventory (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVin authored Nov 18, 2024
1 parent 8998df5 commit d3c9080
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
44 changes: 41 additions & 3 deletions internal/inventory/device_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"go.uber.org/zap"

"github.com/metal-toolbox/fleetdb/internal/dbtools"
"github.com/metal-toolbox/fleetdb/internal/metrics"
Expand All @@ -37,11 +38,13 @@ var (
// XXX: enable this when Server supports UEFI variables
// alloyUefiVarsNamespace = "sh.hollow.alloy.server_uefi_variables" // this is a versioned attribute, we expect it to change
serverStatusNamespace = "sh.hollow.alloy.server_status" // versioned
bmcInfoNamespace = "sh.hollow.bmc_info"

// metadata keys
modelKey = "model"
vendorKey = "vendor"
serialKey = "serial"
modelKey = "model"
vendorKey = "vendor"
serialKey = "serial"
bmcAddressKey = "address"
// XXX: again, enable after UEFI Variables are a thing. uefiVarsKey = "uefi-variables"

errBadServer = errors.New("data is missing required field")
Expand Down Expand Up @@ -95,6 +98,15 @@ func (dv *DeviceView) vendorAttributes() json.RawMessage {
return byt
}

func (dv *DeviceView) bmcInfo() json.RawMessage {
m := map[string]string{
bmcAddressKey: dv.Inv.BMCAddress,
}
byt, _ := json.Marshal(m)

return byt
}

/* XXX: return this when rivet's Server datatype has a facility to store UEFI vars.
func (dv *DeviceView) uefiVariables() (json.RawMessage, error) {
var varString string
Expand All @@ -117,6 +129,18 @@ func (dv *DeviceView) updateVendorAttributes(ctx context.Context, exec boil.Cont
return updateAnyAttribute(ctx, exec, true, dv.DeviceID.String(), alloyVendorNamespace, dv.vendorAttributes())
}

func (dv *DeviceView) updateBMCInfo(ctx context.Context, exec boil.ContextExecutor) error {
// bmc info is an optional attribute (for example, in-band inventory can't collect it) so only update
// if we have a non-zero value here.
if dv.Inv.BMCAddress == "" {
zap.L().With(
zap.String("server.id", dv.DeviceID.String()),
).Debug("no bmc info attribute")
return nil
}
return updateAnyAttribute(ctx, exec, true, dv.DeviceID.String(), bmcInfoNamespace, dv.bmcInfo())
}

// write all the versioned-attributes from this server
func (dv *DeviceView) updateServerVAs(ctx context.Context, exec boil.ContextExecutor) error {
statusData, _ := json.Marshal(dv.Inv.Status)
Expand All @@ -131,6 +155,10 @@ func (dv *DeviceView) UpsertInventory(ctx context.Context, exec boil.ContextExec
return errors.Wrap(err, "server vendor attributes update")
}

if err := dv.updateBMCInfo(ctx, exec); err != nil {
return errors.Wrap(err, "bmc info update")
}

if err := dv.updateServerVAs(ctx, exec); err != nil {
return errors.Wrap(err, "server versioned attribute update")
}
Expand Down Expand Up @@ -172,6 +200,16 @@ func (dv *DeviceView) FromDatastore(ctx context.Context, exec boil.ContextExecut
dv.Inv.Vendor = m[vendorKey]
dv.Inv.Model = m[modelKey]
dv.Inv.Serial = m[serialKey]
case bmcInfoNamespace:
m := map[string]string{}
if err := a.Data.Unmarshal(&m); err == nil {
dv.Inv.BMCAddress = m[bmcAddressKey]
} else {
zap.L().With(
zap.Error(err),
zap.String("server.id", dv.DeviceID.String()),
).Warn("deserializing bmc info attribute")
}
default:
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/inventory/device_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Test_DeviceViewUpdate(t *testing.T) {
},
DeviceID: srvID,
}
// make a server for out attributes
// make a server for our attributes
server := models.Server{
ID: srvID.String(),
Name: null.StringFrom("dvtest-server"),
Expand All @@ -86,6 +86,7 @@ func Test_DeviceViewUpdate(t *testing.T) {

// do it again to test the update
dv.Inv.Status = "different status"
dv.Inv.BMCAddress = "my bmc address"
err = dv.UpsertInventory(context.TODO(), db)
require.NoError(t, err)

Expand All @@ -96,6 +97,7 @@ func Test_DeviceViewUpdate(t *testing.T) {
err = read.FromDatastore(context.TODO(), db)
require.NoError(t, err)
require.Equal(t, "different status", read.Inv.Status)
require.Equal(t, "my bmc address", read.Inv.BMCAddress)

require.Len(t, read.Inv.Components, 1)
bios := read.Inv.Components[0]
Expand Down

0 comments on commit d3c9080

Please sign in to comment.