From 3ac7cba66400f7028fc4234f0c83ed5babd1fb02 Mon Sep 17 00:00:00 2001 From: Alva Zhang <140438385+Alva8756@users.noreply.github.com> Date: Sun, 28 Apr 2024 20:14:29 -0700 Subject: [PATCH] use bmc-toolbox component types (#21) --- go.mod | 2 +- go.sum | 4 ++-- internal/fleetdb/fleetdb.go | 17 +++++------------ .../inventoryconverter/inventoryconverter.go | 8 +++----- pkg/api/routes/components.go | 2 +- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 65fd102..ccf5891 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 toolchain go1.22.2 require ( - github.com/bmc-toolbox/common v0.0.0-20240416132216-a56a09c16f4e + github.com/bmc-toolbox/common v0.0.0-20240423132505-01bb4bac6913 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 diff --git a/go.sum b/go.sum index 916d070..a73f53d 100644 --- a/go.sum +++ b/go.sum @@ -98,8 +98,8 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= -github.com/bmc-toolbox/common v0.0.0-20240416132216-a56a09c16f4e h1:MepqMSwXZPngO7KWD0MyaUKuQkZD/yNfpRJhYZszbr4= -github.com/bmc-toolbox/common v0.0.0-20240416132216-a56a09c16f4e/go.mod h1:SY//n1PJjZfbFbmAsB6GvEKbc7UXz3d30s3kWxfJQ/c= +github.com/bmc-toolbox/common v0.0.0-20240423132505-01bb4bac6913 h1:tqNUIR23EV+jn/0uPI+yqJoHEKa59M+WrL3oaXPxwwc= +github.com/bmc-toolbox/common v0.0.0-20240423132505-01bb4bac6913/go.mod h1:SY//n1PJjZfbFbmAsB6GvEKbc7UXz3d30s3kWxfJQ/c= github.com/bytedance/sonic v1.11.5 h1:G00FYjjqll5iQ1PYXynbg/hyzqBqavH8Mo9/oTopd9k= github.com/bytedance/sonic v1.11.5/go.mod h1:X2PC2giUdj/Cv2lliWFLk6c/DUQok5rViJSemeB0wDw= github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY= diff --git a/internal/fleetdb/fleetdb.go b/internal/fleetdb/fleetdb.go index 57449d8..3b7757d 100644 --- a/internal/fleetdb/fleetdb.go +++ b/internal/fleetdb/fleetdb.go @@ -2,8 +2,8 @@ package internalfleetdb import ( "context" - "fmt" + common "github.com/bmc-toolbox/common" "github.com/google/uuid" "github.com/metal-toolbox/component-inventory/internal/app" "github.com/metal-toolbox/component-inventory/internal/inventoryconverter" @@ -20,7 +20,7 @@ type Client interface { } // Creates a new Client, with reasonable defaults -func NewFleetDBClient(ctx context.Context, cfg *app.Configuration) (Client, error) { +func NewFleetDBClient(_ context.Context, cfg *app.Configuration) (Client, error) { client, err := fleetdb.NewClient(cfg.FleetDBAddress, nil) if err != nil { return nil, err @@ -30,17 +30,10 @@ func NewFleetDBClient(ctx context.Context, cfg *app.Configuration) (Client, erro client.SetToken(cfg.FleetDBToken) } - // TODO: replace it with common.ComponentTypes() after figuring out - // how to fetch ServerComponentType ID. - // Then it's cleaner to move inventoryConverterInstance to the router - // instead of the Client interface. - slugs := make(map[string]*fleetdb.ServerComponentType) - serverComponentTypes, _, err := client.ListServerComponentTypes(ctx, nil) - if err != nil { - return nil, fmt.Errorf("failed to get server component types: %w", err) - } + slugs := make(map[string]bool) + serverComponentTypes := common.ComponentTypes() for _, ct := range serverComponentTypes { - slugs[ct.Slug] = ct + slugs[ct] = true } return &fleetDBClient{ diff --git a/internal/inventoryconverter/inventoryconverter.go b/internal/inventoryconverter/inventoryconverter.go index ad66d92..3f50352 100644 --- a/internal/inventoryconverter/inventoryconverter.go +++ b/internal/inventoryconverter/inventoryconverter.go @@ -6,15 +6,14 @@ import ( "strings" "github.com/bmc-toolbox/common" - fleetdb "github.com/metal-toolbox/fleetdb/pkg/api/v1" rivets "github.com/metal-toolbox/rivets/types" ) type InventoryConverter struct { - componentSlug map[string]*fleetdb.ServerComponentType + componentSlug map[string]bool } -func NewInventoryConverter(componentSlug map[string]*fleetdb.ServerComponentType) *InventoryConverter { +func NewInventoryConverter(componentSlug map[string]bool) *InventoryConverter { return &InventoryConverter{ componentSlug: componentSlug, } @@ -55,11 +54,10 @@ func (ic *InventoryConverter) newComponent(slug, cvendor, cmodel, cserial, cprod cmodel = cproduct } return &rivets.Component{ - ID: ic.componentSlug[slug].ID, Firmware: firmware, Status: status, Attributes: attrs, - Name: ic.componentSlug[slug].Name, + Name: slug, Vendor: cvendor, Model: cmodel, Serial: cserial, diff --git a/pkg/api/routes/components.go b/pkg/api/routes/components.go index 3a43666..304bcc2 100644 --- a/pkg/api/routes/components.go +++ b/pkg/api/routes/components.go @@ -98,7 +98,7 @@ func compareComponents(fleetServer, alloyServer *rivets.Server, log *zap.Logger) } if !listedByAlloy { - // has slug in alloy list, but fleet vendor+model doesn't in alloy list + // alloy did not report hardware that has previously been in fleetdb match = false fields := []zap.Field{ zap.String("device.id", fleetServer.ID),