From 7f91e2834a22f6ba24c9fbfa2295b9e8a7b33170 Mon Sep 17 00:00:00 2001 From: Alva8756 Date: Wed, 13 Mar 2024 14:23:53 -0700 Subject: [PATCH] refactor minor --- pkg/api/client/client.go | 16 +++++----------- .../client/{httpdoer.go => client_private.go} | 4 +++- pkg/api/routes/endpoints.go | 9 +++++++++ pkg/api/routes/inventory.go | 3 --- pkg/api/routes/routes.go | 18 ++++++------------ 5 files changed, 23 insertions(+), 27 deletions(-) rename pkg/api/client/{httpdoer.go => client_private.go} (97%) create mode 100644 pkg/api/routes/endpoints.go diff --git a/pkg/api/client/client.go b/pkg/api/client/client.go index 450fb8b..743dead 100644 --- a/pkg/api/client/client.go +++ b/pkg/api/client/client.go @@ -7,16 +7,10 @@ import ( "net/http" "github.com/bmc-toolbox/common" + "github.com/metal-toolbox/component-inventory/pkg/api/routes" rivets "github.com/metal-toolbox/rivets/types" ) -const ( - versionEndpoint = "/api/version" - componentsEndpoint = "/components" - inbandInventoryEndpoint = "/inventory/in-band" - outofbandInventoryEndpoint = "/inventory/out-of-band" -) - type ServerComponents map[string][]*rivets.Component // Client can perform queries against the Component Inventory Service. @@ -57,7 +51,7 @@ func NewClient(serverAddress string, opts ...Option) (Client, error) { } func (c componentInventoryClient) GetServerComponents(ctx context.Context, serverID string) (ServerComponents, error) { - path := fmt.Sprintf("%v/%v", componentsEndpoint, serverID) + path := fmt.Sprintf("%v/%v", routes.ComponentsEndpoint, serverID) resp, err := c.get(ctx, path) if err != nil { return nil, err @@ -72,7 +66,7 @@ func (c componentInventoryClient) GetServerComponents(ctx context.Context, serve } func (c componentInventoryClient) Version(ctx context.Context) (string, error) { - resp, err := c.get(ctx, versionEndpoint) + resp, err := c.get(ctx, routes.VersionEndpoint) if err != nil { return "", err } @@ -81,7 +75,7 @@ func (c componentInventoryClient) Version(ctx context.Context) (string, error) { } func (c componentInventoryClient) UpdateInbandInventory(ctx context.Context, serverID string, device *common.Device) (string, error) { - path := fmt.Sprintf("%v/%v", inbandInventoryEndpoint, serverID) + path := fmt.Sprintf("%v/%v", routes.InbandInventoryEndpoint, serverID) body, err := json.Marshal(device) if err != nil { return "", fmt.Errorf("failed to parse device: %v", err) @@ -96,7 +90,7 @@ func (c componentInventoryClient) UpdateInbandInventory(ctx context.Context, ser } func (c componentInventoryClient) UpdateOutOfbandInventory(ctx context.Context, serverID string, device *common.Device) (string, error) { - path := fmt.Sprintf("%v/%v", outofbandInventoryEndpoint, serverID) + path := fmt.Sprintf("%v/%v", routes.OutofbandInventoryEndpoint, serverID) body, err := json.Marshal(device) if err != nil { return "", fmt.Errorf("failed to parse device: %v", err) diff --git a/pkg/api/client/httpdoer.go b/pkg/api/client/client_private.go similarity index 97% rename from pkg/api/client/httpdoer.go rename to pkg/api/client/client_private.go index 62fa2aa..7a1c753 100644 --- a/pkg/api/client/httpdoer.go +++ b/pkg/api/client/client_private.go @@ -7,6 +7,8 @@ import ( "io" "net/http" "net/url" + + "github.com/pkg/errors" ) // Doer performs HTTP requests. @@ -39,7 +41,7 @@ func WithAuthToken(authToken string) Option { func (c *componentInventoryClient) get(ctx context.Context, path string) ([]byte, error) { requestURL, err := url.Parse(fmt.Sprintf("%s%s", c.serverAddress, path)) if err != nil { - return nil, Error{Cause: err.Error()} + return nil, errors.Wrap(err, "parsing URL") } req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), http.NoBody) diff --git a/pkg/api/routes/endpoints.go b/pkg/api/routes/endpoints.go new file mode 100644 index 0000000..3b94ff0 --- /dev/null +++ b/pkg/api/routes/endpoints.go @@ -0,0 +1,9 @@ +package routes + +const ( + LivenessEndpoint = "/_health/liveness" + VersionEndpoint = "/api/version" + ComponentsEndpoint = "/components" + InbandInventoryEndpoint = "/inventory/in-band" + OutofbandInventoryEndpoint = "/inventory/out-of-band" +) diff --git a/pkg/api/routes/inventory.go b/pkg/api/routes/inventory.go index 238fc6f..060b0f4 100644 --- a/pkg/api/routes/inventory.go +++ b/pkg/api/routes/inventory.go @@ -2,7 +2,6 @@ package routes import ( "errors" - "fmt" "github.com/bmc-toolbox/common" "github.com/google/uuid" @@ -12,12 +11,10 @@ import ( func processInband(c *fleetdb.Client, srvID uuid.UUID, dev *common.Device, log *zap.Logger) error { log.Info("processing", zap.String("server id", srvID.String()), zap.String("device", dev.Serial)) - fmt.Printf("not implemented for client %v", c) return errors.New("not implemented") } func processOutofband(c *fleetdb.Client, srvID uuid.UUID, dev *common.Device, log *zap.Logger) error { log.Info("processing", zap.String("server id", srvID.String()), zap.String("device", dev.Serial)) - fmt.Printf("not implemented for client %v", c) return errors.New("not implemented") } diff --git a/pkg/api/routes/routes.go b/pkg/api/routes/routes.go index f197593..d50260e 100644 --- a/pkg/api/routes/routes.go +++ b/pkg/api/routes/routes.go @@ -21,12 +21,6 @@ var ( readTimeout = 10 * time.Second writeTimeout = 20 * time.Second - livenessEndpoint = "/_health/liveness" - versionEndpoint = "/api/version" - componentsEndpoint = "/components/:server" - inbandInventoryEndpoint = "/inventory/in-band/:server" - outofbandInventoryEndpoint = "/inventory/out-of-band/:server" - authMiddleWare *ginauth.MultiTokenMiddleware ginNoOp = func(_ *gin.Context) {} ) @@ -92,7 +86,7 @@ func ComposeHTTPServer(theApp *app.App) *http.Server { } // set up common middleware for logging and metrics - g.Use(composeAppLogging(theApp.Log, livenessEndpoint), gin.Recovery()) + g.Use(composeAppLogging(theApp.Log, LivenessEndpoint), gin.Recovery()) // some boilerplate setup g.NoRoute(func(c *gin.Context) { @@ -104,11 +98,11 @@ func ComposeHTTPServer(theApp *app.App) *http.Server { }) // a liveness endpoint - g.GET(livenessEndpoint, func(c *gin.Context) { + g.GET(LivenessEndpoint, func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"time": time.Now()}) }) - g.GET(versionEndpoint, func(c *gin.Context) { + g.GET(VersionEndpoint, func(c *gin.Context) { c.JSON(http.StatusOK, version.Current()) }) @@ -123,7 +117,7 @@ func ComposeHTTPServer(theApp *app.App) *http.Server { // add other API endpoints to the gin Engine as required // get the components associated with a server - g.GET(componentsEndpoint, + g.GET(ComponentsEndpoint+"/:server", composeAuthHandler(readScopes("server:component")), func(ctx *gin.Context) { serverID, err := uuid.Parse(ctx.Param("server")) @@ -148,12 +142,12 @@ func ComposeHTTPServer(theApp *app.App) *http.Server { }) // add an API to ingest inventory data - g.POST(inbandInventoryEndpoint, + g.POST(InbandInventoryEndpoint+"/:server", composeAuthHandler(updateScopes("server:component")), composeInventoryHandler(theApp, processInband), ) - g.POST(outofbandInventoryEndpoint, + g.POST(OutofbandInventoryEndpoint+"/:server", composeAuthHandler(updateScopes("server:component")), composeInventoryHandler(theApp, processOutofband), )