Skip to content

Commit

Permalink
[FS-1497]Update firmware selection logic in mtcl (#119)
Browse files Browse the repository at this point in the history
* update firmware selection logic in mtcl

* use new rivets release

* minor refactor
  • Loading branch information
Alva8756 authored Jun 20, 2024
1 parent 8630883 commit 597c25a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 90 deletions.
70 changes: 2 additions & 68 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/davecgh/go-spew/spew"
Expand All @@ -18,6 +17,7 @@ import (
coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/types"
fleetdbapi "github.com/metal-toolbox/fleetdb/pkg/api/v1"
rctypes "github.com/metal-toolbox/rivets/condition"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
rt "github.com/metal-toolbox/rivets/types"
)

Expand Down Expand Up @@ -90,7 +90,7 @@ func VendorModelFromAttrs(attrs []fleetdbapi.Attributes) (vendor, model string)
//
// TODO: move into common library
func FirmwareSetIDByVendorModel(ctx context.Context, vendor, model string, client *fleetdbapi.Client) (uuid.UUID, error) {
fwSet, err := FirmwareSetByVendorModel(ctx, vendor, model, client)
fwSet, err := rfleetdb.FirmwareSetByVendorModel(ctx, vendor, model, client)
if err != nil {
return uuid.Nil, err
}
Expand All @@ -105,72 +105,6 @@ func FirmwareSetIDByVendorModel(ctx context.Context, vendor, model string, clien
return fwSet[0].UUID, nil
}

// FirmwareSetByVendorModel returns the firmware set matched by the vendor, model attributes
//
// TODO: move into common library
func FirmwareSetByVendorModel(ctx context.Context, vendor, model string, client *fleetdbapi.Client) ([]fleetdbapi.ComponentFirmwareSet, error) {
vendor = strings.TrimSpace(vendor)
if vendor == "" {
return []fleetdbapi.ComponentFirmwareSet{}, errors.Wrap(
ErrFwSetByVendorModel,
"got empty vendor attribute",
)
}

model = strings.TrimSpace(model)
if model == "" {
return []fleetdbapi.ComponentFirmwareSet{}, errors.Wrap(
ErrFwSetByVendorModel,
"got empty model attribute",
)
}

// ?attr=sh.hollow.firmware_set.labels~vendor~eq~dell&attr=sh.hollow.firmware_set.labels~model~eq~r750&attr=sh.hollow.firmware_set.labels~latest~eq~false
// list latest, default firmware sets by vendor, model attributes
fwSetListparams := &fleetdbapi.ComponentFirmwareSetListParams{
AttributeListParams: []fleetdbapi.AttributeListParams{
{
Namespace: FirmwareSetAttributeNS,
Keys: []string{"vendor"},
Operator: "eq",
Value: strings.ToLower(vendor),
},
{
Namespace: FirmwareSetAttributeNS,
Keys: []string{"model"},
Operator: "like",
Value: strings.ToLower(model),
},
{
Namespace: FirmwareSetAttributeNS,
Keys: []string{"latest"}, // latest indicates the most current revision of the firmware set.
Operator: "eq",
Value: "true",
},
{
Namespace: FirmwareSetAttributeNS,
Keys: []string{"default"}, // default indicates the firmware set does not belong to an org/project.
Operator: "eq",
Value: "true",
},
},
}

fwSet, _, err := client.ListServerComponentFirmwareSet(ctx, fwSetListparams)
if err != nil {
return []fleetdbapi.ComponentFirmwareSet{}, errors.Wrap(ErrFwSetByVendorModel, err.Error())
}

if len(fwSet) == 0 {
return []fleetdbapi.ComponentFirmwareSet{}, errors.Wrap(
ErrFwSetByVendorModel,
fmt.Sprintf("no fw sets identified for vendor: %s, model: %s", vendor, model),
)
}

return fwSet, nil
}

type ErrUnexpectedResponse struct {
statusCode int
message string
Expand Down
3 changes: 2 additions & 1 deletion cmd/get/firmware-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
)

type getFirmwareSetFlags struct {
Expand Down Expand Up @@ -95,7 +96,7 @@ func firmwareSetForServer(ctx context.Context, client *fleetdbapi.Client, server
}

// identify firmware set by vendor, model attributes
fwSet, err := mctl.FirmwareSetByVendorModel(ctx, vendor, model, client)
fwSet, err := rfleetdb.FirmwareSetByVendorModel(ctx, vendor, model, client)
if err != nil {
return nil, err
}
Expand Down
31 changes: 19 additions & 12 deletions cmd/list/firmware_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ import (
mctl "github.com/metal-toolbox/mctl/cmd"
"github.com/metal-toolbox/mctl/internal/app"
"github.com/metal-toolbox/mctl/pkg/model"
rfleetdb "github.com/metal-toolbox/rivets/fleetdb"
)

type listFirmwareSetFlags struct {
vendor string
model string
labels map[string]string
}

var (
flagsDefinedListFwSet *listFirmwareSetFlags
)

func sendListFirmwareRequest(client *fleetdbapi.Client, cmd *cobra.Command) ([]fleetdbapi.ComponentFirmwareSet, error) {
if flagsDefinedListFwSet.vendor == "" && flagsDefinedListFwSet.model == "" {
fwSet, _, err := client.ListServerComponentFirmwareSet(cmd.Context(), &fleetdbapi.ComponentFirmwareSetListParams{})
return fwSet, err
}

if len(flagsDefinedListFwSet.labels) != 0 {
return rfleetdb.FirmwareSetByLabels(cmd.Context(), flagsDefinedListFwSet.vendor, flagsDefinedListFwSet.model, flagsDefinedListFwSet.labels, client)
}

return rfleetdb.FirmwareSetByVendorModel(cmd.Context(), flagsDefinedListFwSet.vendor, flagsDefinedListFwSet.model, client)
}

// List
var listFirmwareSet = &cobra.Command{
Use: "firmware-set",
Expand All @@ -35,18 +50,9 @@ var listFirmwareSet = &cobra.Command{
log.Fatal(err)
}

var fwSet []fleetdbapi.ComponentFirmwareSet

if flagsDefinedListFwSet.vendor != "" || flagsDefinedListFwSet.model != "" {
fwSet, err = mctl.FirmwareSetByVendorModel(cmd.Context(), flagsDefinedListFwSet.vendor, flagsDefinedListFwSet.model, client)
if err != nil {
log.Fatal(err)
}
} else {
fwSet, _, err = client.ListServerComponentFirmwareSet(cmd.Context(), &fleetdbapi.ComponentFirmwareSetListParams{})
if err != nil {
log.Fatal(err)
}
fwSet, err := sendListFirmwareRequest(client, cmd)
if err != nil {
log.Fatal(err)
}

if output == mctl.OutputTypeJSON.String() {
Expand Down Expand Up @@ -80,4 +86,5 @@ func init() {

mctl.AddModelFlag(listFirmwareSet, &flagsDefinedListFwSet.model)
mctl.AddVendorFlag(listFirmwareSet, &flagsDefinedListFwSet.vendor)
mctl.AddLabelsFlag(listFirmwareSet, &flagsDefinedListFwSet.labels, "Labels to from the firmware set - 'foo=bar,foo2=bar2'")
}
7 changes: 4 additions & 3 deletions docs/mctl_list_firmware-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ mctl list firmware-set [flags]
### Options

```
-h, --help help for firmware-set
-m, --model string filter by model
-v, --vendor string filter by vendor
-h, --help help for firmware-set
-l, --labels stringToString Labels to from the firmware set - 'foo=bar,foo2=bar2' (default [])
-m, --model string filter by model
-v, --vendor string filter by vendor
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/google/uuid v1.6.0
github.com/metal-toolbox/bomservice v0.1.5
github.com/metal-toolbox/conditionorc v1.0.6
github.com/metal-toolbox/fleetdb v1.18.3
github.com/metal-toolbox/rivets v1.0.4
github.com/metal-toolbox/fleetdb v1.18.5
github.com/metal-toolbox/rivets v1.0.5
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20220510032225-4f9f17eaec4c
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,10 @@ github.com/metal-toolbox/bomservice v0.1.5 h1:1QxDox9hiqxdv2uxEdOEoLBkA/IgvS2BBQ
github.com/metal-toolbox/bomservice v0.1.5/go.mod h1:jS7IDe24FrNWmf7fRu5LmGjMBVNXxg8thctMy+a2i6Q=
github.com/metal-toolbox/conditionorc v1.0.6 h1:SsFgAJdYaWbuJ12c9vuRlJxYfkx/AbRRCVcJpOS4njk=
github.com/metal-toolbox/conditionorc v1.0.6/go.mod h1:KAzZINJE58V49+DsYAZ2mgFFGUJVNVPqyz20dZi4gu8=
github.com/metal-toolbox/fleetdb v1.18.3 h1:vD7uaps4CEysAf0IMyujGiFHe6r3a5Zbt5bKhNjsWD8=
github.com/metal-toolbox/fleetdb v1.18.3/go.mod h1:RRQt0MZQApuQKp4gUrE+ZwPVqvI6qhMbpakGBlxPJZc=
github.com/metal-toolbox/rivets v1.0.4 h1:BFvlYWsN+5Zb2JS+oJJBDtaQ94M5x2Llf6kmwi+Byjo=
github.com/metal-toolbox/rivets v1.0.4/go.mod h1:EMQJRT1mjIyFRXxvKNaBlz7Z4Sp88rTaGO8W18olN2I=
github.com/metal-toolbox/fleetdb v1.18.5 h1:xFY6SzThdPMWtTnb8cNE0Dpa+kCMFcZdM72MITW530g=
github.com/metal-toolbox/fleetdb v1.18.5/go.mod h1:RRQt0MZQApuQKp4gUrE+ZwPVqvI6qhMbpakGBlxPJZc=
github.com/metal-toolbox/rivets v1.0.5 h1:PIOT8OPoNMVeSRIbjZUMuDqZD+nHdiXwgIDjWV330m8=
github.com/metal-toolbox/rivets v1.0.5/go.mod h1:JBbPEDevQkQmNHNGi4zalTjqTTMs0/0/xCtx1EKe10c=
github.com/microsoft/go-mssqldb v0.17.0/go.mod h1:OkoNGhGEs8EZqchVTtochlXruEhEOaO4S0d2sB5aeGQ=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
Expand Down

0 comments on commit 597c25a

Please sign in to comment.