Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions cmd/admin/v2/size-reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package v2

import (
"fmt"

"github.com/metal-stack/api/go/errorutil"
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
"github.com/metal-stack/cli/pkg/helpers"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type sizeReservation struct {
c *config.Config
}

func newSizeReservationCmd(c *config.Config) *cobra.Command {
w := &sizeReservation{
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[*adminv2.SizeReservationServiceCreateRequest, *adminv2.SizeReservationServiceUpdateRequest, *apiv2.SizeReservation]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "reservation",
Plural: "reservations",
Description: "manage size reservations which reserve machine capacity for a project",
Sorter: sorters.SizeReservationSorter(),
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
ValidArgsFn: c.Completion.AdminSizeReservation,
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("name", "", "size reservation name to filter for")
cmd.Flags().String("description", "", "size reservation description to filter for")
cmd.Flags().String("size", "", "size id to filter for")
cmd.Flags().String("project", "", "project id to filter for")
cmd.Flags().String("partition", "", "partition id to filter for")
cmd.Flags().StringSlice("labels", nil, "labels to filter for")

genericcli.Must(cmd.RegisterFlagCompletionFunc("size", c.Completion.Size))
genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(cmd.RegisterFlagCompletionFunc("partition", c.Completion.Partition))
},
CreateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("name", "", "the name of the size reservation to create")
cmd.Flags().String("description", "", "the description of the size reservation to create")
cmd.Flags().String("project", "", "the project id of the size reservation to create")
cmd.Flags().String("size", "", "the size id of the size reservation to create")
cmd.Flags().StringSlice("partitions", nil, "the partition ids the size reservation applies to")
cmd.Flags().Int32("amount", 0, "the amount of machines to reserve")
cmd.Flags().StringSlice("labels", nil, "labels to add to the size reservation")

genericcli.Must(cmd.RegisterFlagCompletionFunc("size", c.Completion.Size))
genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.Project))
genericcli.Must(cmd.RegisterFlagCompletionFunc("partitions", c.Completion.Partition))
},
CreateRequestFromCLI: w.createRequestFromCLI,
UpdateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("name", "", "the name of the size reservation")
cmd.Flags().String("description", "", "the description of the size reservation")
cmd.Flags().StringSlice("partitions", nil, "the partition ids the size reservation applies to")
cmd.Flags().Int32("amount", 0, "the amount of machines to reserve")
cmd.Flags().StringSlice("labels", nil, "labels to replace for the size reservation")
cmd.Flags().StringSlice("add-labels", nil, "labels to add to the size reservation")
cmd.Flags().StringSlice("remove-labels", nil, "labels to remove from the size reservation")

genericcli.Must(cmd.RegisterFlagCompletionFunc("partitions", c.Completion.Partition))
},
UpdateRequestFromCLI: w.updateRequestFromCLI,
}

return genericcli.NewCmds(cmdsConfig)
}

func (c *sizeReservation) Get(id string) (*apiv2.SizeReservation, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

// the admin api does not offer a dedicated get call for size reservations,
// so we filter the list by the given id.
Comment on lines +85 to +86

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is why... in the repository the get function is implemented. 😅

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait... I think I found a get in the normal API and not the Admin API?

resp, err := c.c.Client.Adminv2().SizeReservation().List(ctx, &adminv2.SizeReservationServiceListRequest{
Query: &apiv2.SizeReservationQuery{
Id: new(id),
},
})
if err != nil {
return nil, fmt.Errorf("failed to get size reservation: %w", err)
}

reservations := resp.GetSizeReservations()
if len(reservations) == 0 {
return nil, fmt.Errorf("size reservation %q not found", id)
}

return reservations[0], nil
}

func (c *sizeReservation) List() ([]*apiv2.SizeReservation, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

req := &adminv2.SizeReservationServiceListRequest{
Query: &apiv2.SizeReservationQuery{
Name: pointer.PointerOrNil(viper.GetString("name")),
Description: pointer.PointerOrNil(viper.GetString("description")),
Size: pointer.PointerOrNil(viper.GetString("size")),
Project: pointer.PointerOrNil(viper.GetString("project")),
Partition: pointer.PointerOrNil(viper.GetString("partition")),
},
}

if labelSlice := viper.GetStringSlice("labels"); len(labelSlice) > 0 {
labels, err := helpers.LabelsFromSlice(labelSlice)
if err != nil {
return nil, err
}

req.Query.Labels = labels
}

resp, err := c.c.Client.Adminv2().SizeReservation().List(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to list size reservations: %w", err)
}

return resp.GetSizeReservations(), nil
}

func (c *sizeReservation) Create(rq *adminv2.SizeReservationServiceCreateRequest) (*apiv2.SizeReservation, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Adminv2().SizeReservation().Create(ctx, rq)
if err != nil {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

return nil, fmt.Errorf("failed to create size reservation: %w", err)
}

return resp.GetSizeReservation(), nil
}

func (c *sizeReservation) Update(rq *adminv2.SizeReservationServiceUpdateRequest) (*apiv2.SizeReservation, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Adminv2().SizeReservation().Update(ctx, rq)
if err != nil {
return nil, fmt.Errorf("failed to update size reservation: %w", err)
}

return resp.GetSizeReservation(), nil
}

func (c *sizeReservation) Delete(id string) (*apiv2.SizeReservation, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Adminv2().SizeReservation().Delete(ctx, &adminv2.SizeReservationServiceDeleteRequest{
Id: id,
})
if err != nil {
return nil, fmt.Errorf("failed to delete size reservation: %w", err)
}

return resp.GetSizeReservation(), nil
}

func (c *sizeReservation) Convert(r *apiv2.SizeReservation) (string, *adminv2.SizeReservationServiceCreateRequest, *adminv2.SizeReservationServiceUpdateRequest, error) {
return r.Id,
&adminv2.SizeReservationServiceCreateRequest{
SizeReservation: r,
},
&adminv2.SizeReservationServiceUpdateRequest{
Id: r.Id,
UpdateMeta: helpers.UpdateMetaFromMeta(r.Meta),
Name: pointer.PointerOrNil(r.Name),
Description: pointer.PointerOrNil(r.Description),
Partitions: r.Partitions,
Amount: pointer.PointerOrNil(r.Amount),
Labels: helpers.UpdateLabelsFromMeta(r.Meta),
}, nil
}

func (c *sizeReservation) createRequestFromCLI() (*adminv2.SizeReservationServiceCreateRequest, error) {
reservation := &apiv2.SizeReservation{
Name: viper.GetString("name"),
Description: viper.GetString("description"),
Project: viper.GetString("project"),
Size: viper.GetString("size"),
Partitions: viper.GetStringSlice("partitions"),
Amount: viper.GetInt32("amount"),
}

labels, err := helpers.LabelsFromSlice(viper.GetStringSlice("labels"))
if err != nil {
return nil, err
}
if labels != nil {
reservation.Meta = &apiv2.Meta{Labels: labels}
}

return &adminv2.SizeReservationServiceCreateRequest{
SizeReservation: reservation,
}, nil
}

func (c *sizeReservation) updateRequestFromCLI(args []string) (*adminv2.SizeReservationServiceUpdateRequest, error) {
id, err := genericcli.GetExactlyOneArg(args)
if err != nil {
return nil, err
}

updateLabels, err := helpers.UpdateLabelsFromCLI()
if err != nil {
return nil, err
}

req := &adminv2.SizeReservationServiceUpdateRequest{
Id: id,
UpdateMeta: &apiv2.UpdateMeta{
LockingStrategy: apiv2.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_SERVER,
},
Name: pointer.PointerOrNil(viper.GetString("name")),
Description: pointer.PointerOrNil(viper.GetString("description")),
Amount: pointer.PointerOrNil(viper.GetInt32("amount")),
Labels: updateLabels,
}

if partitions := viper.GetStringSlice("partitions"); len(partitions) > 0 {
req.Partitions = partitions
}

return req, nil
}
2 changes: 1 addition & 1 deletion cmd/admin/v2/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newSizeCmd(c *config.Config) *cobra.Command {
},
}

return genericcli.NewCmds(cmdsConfig)
return genericcli.NewCmds(cmdsConfig, newSizeReservationCmd(c))
}

func (c *size) Get(id string) (*apiv2.Size, error) {
Expand Down
23 changes: 23 additions & 0 deletions cmd/completion/size_reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package completion

import (
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/spf13/cobra"
)

func (c *Completion) AdminSizeReservation(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
resp, err := c.Client.Adminv2().SizeReservation().List(cmd.Context(), &adminv2.SizeReservationServiceListRequest{
Query: &apiv2.SizeReservationQuery{},
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var names []string
for _, r := range resp.SizeReservations {
names = append(names, r.Id+"\t"+r.Name)
}

return names, cobra.ShellCompDirectiveNoFileComp
}
29 changes: 29 additions & 0 deletions cmd/sorters/size_reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sorters

import (
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/metal-lib/pkg/multisort"
)

func SizeReservationSorter() *multisort.Sorter[*apiv2.SizeReservation] {
return multisort.New(multisort.FieldMap[*apiv2.SizeReservation]{
"id": func(a, b *apiv2.SizeReservation, descending bool) multisort.CompareResult {
return multisort.Compare(a.Id, b.Id, descending)
},
"name": func(a, b *apiv2.SizeReservation, descending bool) multisort.CompareResult {
return multisort.Compare(a.Name, b.Name, descending)
},
"description": func(a, b *apiv2.SizeReservation, descending bool) multisort.CompareResult {
return multisort.Compare(a.Description, b.Description, descending)
},
"project": func(a, b *apiv2.SizeReservation, descending bool) multisort.CompareResult {
return multisort.Compare(a.Project, b.Project, descending)
},
"size": func(a, b *apiv2.SizeReservation, descending bool) multisort.CompareResult {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter by amount might be interesting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return multisort.Compare(a.Size, b.Size, descending)
},
"amount": func(a, b *apiv2.SizeReservation, descending bool) multisort.CompareResult {
return multisort.Compare(a.Amount, b.Amount, descending)
},
}, multisort.Keys{{ID: "id"}})
}
5 changes: 5 additions & 0 deletions cmd/tableprinters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
case []*apiv2.Size:
return t.SizeTable(d, wide)

case *apiv2.SizeReservation:
return t.SizeReservationTable(pointer.WrapInSlice(d), wide)
case []*apiv2.SizeReservation:
return t.SizeReservationTable(d, wide)

case []*apiv2.Switch:
return t.SwitchTable(d, wide)
case []SwitchDetail:
Expand Down
43 changes: 43 additions & 0 deletions cmd/tableprinters/size_reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tableprinters

import (
"fmt"
"sort"
"strings"

apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/metal-lib/pkg/genericcli"
)

func (t *TablePrinter) SizeReservationTable(data []*apiv2.SizeReservation, wide bool) ([]string, [][]string, error) {
var (
header = []string{"ID", "Name", "Description", "Project", "Size", "Partitions", "Amount"}
rows [][]string
)

if wide {
header = append(header, "Labels")
}

for _, r := range data {
row := []string{
r.Id,
r.Name,
r.Description,
r.Project,
r.Size,
strings.Join(r.Partitions, "\n"),
fmt.Sprintf("%d", r.Amount),
}

if wide {
labels := genericcli.MapToLabels(r.Meta.GetLabels().GetLabels())
sort.Strings(labels)
row = append(row, strings.Join(labels, "\n"))
}

rows = append(rows, row)
}

return header, rows, nil
}
Loading