From 55c6e9210c6d4ab7e9040423d821851eb1c6936b Mon Sep 17 00:00:00 2001 From: Alva8756 Date: Tue, 23 Apr 2024 19:43:25 -0700 Subject: [PATCH] cleanup code --- go.mod | 2 +- go.sum | 7 +++---- internal/fleetdb/fleetdb.go | 12 +++++++----- pkg/api/routes/inventory.go | 1 + pkg/api/routes/routes.go | 5 +++++ 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 1ca5259..65fd102 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/equinix-labs/otel-init-go v0.0.9 github.com/gin-gonic/gin v1.9.1 github.com/google/uuid v1.6.0 - github.com/metal-toolbox/alloy v0.3.3-0.20240320183632-05dfbd5e9110 + github.com/metal-toolbox/alloy v0.3.3-0.20240415055734-d09250fed38a github.com/metal-toolbox/fleetdb v0.17.2-0.20240419204835-60c421433f0a github.com/metal-toolbox/rivets v1.0.3 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 221f425..916d070 100644 --- a/go.sum +++ b/go.sum @@ -500,12 +500,11 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw= github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= -github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/metal-toolbox/alloy v0.3.3-0.20240320183632-05dfbd5e9110 h1:v3PTHUK9KIF/mEmctQ5mgsBtG8dtdfboHI/7QQg/Ft0= -github.com/metal-toolbox/alloy v0.3.3-0.20240320183632-05dfbd5e9110/go.mod h1:NcVZtbVhBCcpyTHew0TJ8yzon69vMf+rGMLoGb9jTa0= +github.com/metal-toolbox/alloy v0.3.3-0.20240415055734-d09250fed38a h1:w6uita6zAXZoGn75vcnEDW2Y6K1rYeLWnv7EXAemByE= +github.com/metal-toolbox/alloy v0.3.3-0.20240415055734-d09250fed38a/go.mod h1:Sv+oD+xnACNV3E7r9kmtgUi1LGKQumTv7mpWOe8a2VU= github.com/metal-toolbox/fleetdb v0.17.2-0.20240419204835-60c421433f0a h1:Jrjbk0M8EiljlgxeLso232AizQzuaQ4Bnqf8L9ITkug= github.com/metal-toolbox/fleetdb v0.17.2-0.20240419204835-60c421433f0a/go.mod h1:U9YArAdibILSBJzNbmtJAz6m+Cljce6a7pL0JkOckqQ= github.com/metal-toolbox/rivets v1.0.3 h1:ZW9q8V3vz6VxAczC4eR1YJdl+kapHF3ebVc+4r3NmR8= diff --git a/internal/fleetdb/fleetdb.go b/internal/fleetdb/fleetdb.go index a5ab89a..b266a8c 100644 --- a/internal/fleetdb/fleetdb.go +++ b/internal/fleetdb/fleetdb.go @@ -46,8 +46,7 @@ func NewFleetDBClient(ctx context.Context, cfg *app.Configuration) (Client, erro } type fleetDBClient struct { - client *fleetdb.Client - // We may want to support slug refresh. + client *fleetdb.Client slugs map[string]*fleetdb.ServerComponentType inventoryConverterInstance *inventoryconverter.InventoryConverter } @@ -60,13 +59,16 @@ func (fc fleetDBClient) GetComponents(ctx context.Context, id uuid.UUID, params return fc.client.GetComponents(ctx, id, params) } -func (fc fleetDBClient) UpdateServerInventory(ctx context.Context, server *fleetdb.Server, dev *types.InventoryDevice, _ *zap.Logger, inband bool) error { - typeServer, err := fc.inventoryConverterInstance.ToRivetsServer(server.UUID.String(), server.FacilityCode, dev.Inv, *dev.BiosCfg) +func (fc fleetDBClient) UpdateServerInventory(ctx context.Context, server *fleetdb.Server, dev *types.InventoryDevice, log *zap.Logger, inband bool) error { + log.Info("update server inventory", zap.String("server", server.Name)) + rivetsServer, err := fc.inventoryConverterInstance.ToRivetsServer(server.UUID.String(), server.FacilityCode, dev.Inv, dev.BiosCfg) if err != nil { + log.Error("convert inventory fail", zap.String("server", server.Name), zap.String("err", err.Error())) return err } - _, err = fc.client.SetServerInventory(ctx, server.UUID, typeServer, inband) + _, err = fc.client.SetServerInventory(ctx, server.UUID, rivetsServer, inband) if err != nil { + log.Error("set inventory fail", zap.String("server", server.Name), zap.String("err", err.Error())) return err } return nil diff --git a/pkg/api/routes/inventory.go b/pkg/api/routes/inventory.go index b7de9fe..9fd4596 100644 --- a/pkg/api/routes/inventory.go +++ b/pkg/api/routes/inventory.go @@ -22,6 +22,7 @@ func processInband(ctx context.Context, c internalfleetdb.Client, server *fleetd func processOutofband(ctx context.Context, c internalfleetdb.Client, server *fleetdb.Server, dev *types.InventoryDevice, log *zap.Logger) error { //nolint log.Info("processing", zap.String("server", server.Name), zap.String("device", dev.Inv.Serial)) if err := verifyComponent(c, server, dev, log); err != nil { + log.Error("verify component", zap.String("server", server.Name), zap.String("err", err.Error())) return err } return c.UpdateServerInventory(ctx, server, dev, log, false) diff --git a/pkg/api/routes/routes.go b/pkg/api/routes/routes.go index 0d45c3d..e3109b8 100644 --- a/pkg/api/routes/routes.go +++ b/pkg/api/routes/routes.go @@ -224,6 +224,11 @@ func composeInventoryHandler(theApp *app.App, fn inventoryHandler) gin.HandlerFu return } + if dev.Inv == nil { + reject(ctx, http.StatusBadRequest, "empty inventory") + return + } + fleetDBClient, err := internalfleetdb.NewFleetDBClient(ctx, theApp.Cfg) if err != nil { ctx.JSON(http.StatusInternalServerError, map[string]any{