Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
refactor minor
Browse files Browse the repository at this point in the history
  • Loading branch information
Alva8756 committed Mar 13, 2024
1 parent cbc6e8a commit 7f91e28
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
16 changes: 5 additions & 11 deletions pkg/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"

"github.com/pkg/errors"
)

// Doer performs HTTP requests.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/routes/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package routes

const (
LivenessEndpoint = "/_health/liveness"
VersionEndpoint = "/api/version"
ComponentsEndpoint = "/components"
InbandInventoryEndpoint = "/inventory/in-band"
OutofbandInventoryEndpoint = "/inventory/out-of-band"
)
3 changes: 0 additions & 3 deletions pkg/api/routes/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package routes

import (
"errors"
"fmt"

"github.com/bmc-toolbox/common"
"github.com/google/uuid"
Expand All @@ -12,12 +11,10 @@ import (

func processInband(c *fleetdb.Client, srvID uuid.UUID, dev *common.Device, log *zap.Logger) error {

Check warning on line 12 in pkg/api/routes/inventory.go

View workflow job for this annotation

GitHub Actions / lint-test

unused-parameter: parameter 'c' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 12 in pkg/api/routes/inventory.go

View workflow job for this annotation

GitHub Actions / lint-test

unused-parameter: parameter 'c' seems to be unused, consider removing or renaming it as _ (revive)
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 {

Check warning on line 17 in pkg/api/routes/inventory.go

View workflow job for this annotation

GitHub Actions / lint-test

unused-parameter: parameter 'c' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 17 in pkg/api/routes/inventory.go

View workflow job for this annotation

GitHub Actions / lint-test

unused-parameter: parameter 'c' seems to be unused, consider removing or renaming it as _ (revive)
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")
}
18 changes: 6 additions & 12 deletions pkg/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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())
})

Expand All @@ -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"))
Expand All @@ -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),
)
Expand Down

0 comments on commit 7f91e28

Please sign in to comment.