From 597c25a2dd3ecbb77dfbdefeb6e1156f7b11a836 Mon Sep 17 00:00:00 2001 From: Alva Zhang <140438385+Alva8756@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:52:06 -0700 Subject: [PATCH] [FS-1497]Update firmware selection logic in mtcl (#119) * update firmware selection logic in mtcl * use new rivets release * minor refactor --- cmd/common.go | 70 +--------------------------------- cmd/get/firmware-set.go | 3 +- cmd/list/firmware_set.go | 31 +++++++++------ docs/mctl_list_firmware-set.md | 7 ++-- go.mod | 4 +- go.sum | 8 ++-- 6 files changed, 33 insertions(+), 90 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 1d9588d0..674f1586 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "net/http" - "strings" "time" "github.com/davecgh/go-spew/spew" @@ -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" ) @@ -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 } @@ -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 diff --git a/cmd/get/firmware-set.go b/cmd/get/firmware-set.go index cbe06bf5..0e2da1bf 100644 --- a/cmd/get/firmware-set.go +++ b/cmd/get/firmware-set.go @@ -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 { @@ -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 } diff --git a/cmd/list/firmware_set.go b/cmd/list/firmware_set.go index f0d37301..b1e6a426 100644 --- a/cmd/list/firmware_set.go +++ b/cmd/list/firmware_set.go @@ -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", @@ -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() { @@ -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'") } diff --git a/docs/mctl_list_firmware-set.md b/docs/mctl_list_firmware-set.md index 2fc57d79..dcf49ded 100644 --- a/docs/mctl_list_firmware-set.md +++ b/docs/mctl_list_firmware-set.md @@ -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 diff --git a/go.mod b/go.mod index 4b976bd0..f66a4870 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b426c6bc..b2262179 100644 --- a/go.sum +++ b/go.sum @@ -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=