From 178be818ed3e00acbea6edfea5611011cbc4c6c1 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 9 Jul 2024 12:02:29 +0200 Subject: [PATCH] Size reservation labels and dependency updates. (#247) --- cmd/size.go | 26 ++++++++++++++++----- cmd/size_test.go | 30 ++++++++++++++++++++----- cmd/tableprinters/size.go | 16 +++++++++++-- docs/metalctl_size_reservations_list.md | 8 +++++-- go.mod | 4 ++-- go.sum | 8 +++---- 6 files changed, 72 insertions(+), 20 deletions(-) diff --git a/cmd/size.go b/cmd/size.go index 69242274..748bb1fa 100644 --- a/cmd/size.go +++ b/cmd/size.go @@ -79,6 +79,16 @@ func newSizeCmd(c *config) *cobra.Command { }, } + listReservationsCmd.Flags().String("size-id", "", "the size-id to filter") + listReservationsCmd.Flags().String("project", "", "the project to filter") + listReservationsCmd.Flags().String("tenant", "", "the tenant to filter") + listReservationsCmd.Flags().String("partition", "", "the partition to filter") + + genericcli.Must(listReservationsCmd.RegisterFlagCompletionFunc("size-id", c.comp.SizeListCompletion)) + genericcli.Must(listReservationsCmd.RegisterFlagCompletionFunc("project", c.comp.ProjectListCompletion)) + genericcli.Must(listReservationsCmd.RegisterFlagCompletionFunc("tenant", c.comp.TenantListCompletion)) + genericcli.Must(listReservationsCmd.RegisterFlagCompletionFunc("partition", c.comp.PartitionListCompletion)) + genericcli.AddSortFlag(listReservationsCmd, sorters.SizeReservationsSorter()) reservationsCmd.AddCommand(listReservationsCmd) @@ -168,10 +178,11 @@ func sizeResponseToCreate(r *models.V1SizeResponse) *models.V1SizeCreateRequest }) } return &models.V1SizeCreateRequest{ - Constraints: constraints, - Description: r.Description, - ID: r.ID, - Name: r.Name, + Constraints: constraints, + Description: r.Description, + ID: r.ID, + Name: r.Name, + Reservations: r.Reservations, } } @@ -198,7 +209,12 @@ func sizeResponseToUpdate(r *models.V1SizeResponse) *models.V1SizeUpdateRequest // non-generic command handling func (c sizeCmd) listReverations() error { - resp, err := c.client.Size().ListSizeReservations(size.NewListSizeReservationsParams().WithBody(emptyBody), nil) + resp, err := c.client.Size().ListSizeReservations(size.NewListSizeReservationsParams().WithBody(&models.V1SizeReservationListRequest{ + Projectid: viper.GetString("project"), + Sizeid: viper.GetString("size-id"), + Tenant: viper.GetString("tenant"), + Partitionid: viper.GetString("partition"), + }), nil) if err != nil { return err } diff --git a/cmd/size_test.go b/cmd/size_test.go index 49745c94..3bb29ec2 100644 --- a/cmd/size_test.go +++ b/cmd/size_test.go @@ -47,12 +47,18 @@ var ( Description: "for testing", Partitionids: []string{*partition1.ID}, Projectid: pointer.Pointer(project1.Meta.ID), + Labels: map[string]string{ + "size.metal-stack.io/reserved-by": "admin", + }, }, { Amount: pointer.Pointer(int32(2)), Description: "for testing", Partitionids: []string{*partition2.ID}, Projectid: pointer.Pointer(project2.Meta.ID), + Labels: map[string]string{ + "size.metal-stack.io/reserved-by": "admin", + }, }, }, Labels: map[string]string{ @@ -290,6 +296,7 @@ ID NAME DESCRIPTION RESERVATIONS CPU RANGE MEMORY RANGE STORAGE RA Type: size1.Constraints[0].Type, }, } + s.Reservations = nil mock.On("CreateSize", testcommon.MatchIgnoreContext(t, size.NewCreateSizeParams().WithBody(sizeResponseToCreate(size1))), nil).Return(&size.CreateSizeCreated{ Payload: size1, }, nil) @@ -376,6 +383,9 @@ func Test_SizeReservationsCmd_MultiResult(t *testing.T) { Sizeid: pointer.Pointer("size-1"), Tenant: pointer.Pointer("tenant-1"), Usedreservations: pointer.Pointer(int32(5)), + Labels: map[string]string{ + "size.metal-stack.io/reserved-by": "admin", + }, }, { Partitionid: pointer.Pointer("b"), @@ -386,6 +396,9 @@ func Test_SizeReservationsCmd_MultiResult(t *testing.T) { Sizeid: pointer.Pointer("size-2"), Tenant: pointer.Pointer("tenant-2"), Usedreservations: pointer.Pointer(int32(1)), + Labels: map[string]string{ + "size.metal-stack.io/reserved-by": "admin", + }, }, } @@ -393,11 +406,18 @@ func Test_SizeReservationsCmd_MultiResult(t *testing.T) { { name: "reservation list", cmd: func(want []*models.V1SizeReservationResponse) []string { - return []string{"size", "reservations", "list"} + args := []string{"size", "reservations", "list", "--partition", "partition-1", "--project", "project-1", "--size-id", "size-1", "--tenant", "tenant-1"} + assertExhaustiveArgs(t, args, "sort-by") + return args }, mocks: &client.MetalMockFns{ Size: func(mock *mock.Mock) { - mock.On("ListSizeReservations", testcommon.MatchIgnoreContext(t, size.NewListSizeReservationsParams().WithBody(emptyBody)), nil).Return(&size.ListSizeReservationsOK{Payload: reservations}, nil) + mock.On("ListSizeReservations", testcommon.MatchIgnoreContext(t, size.NewListSizeReservationsParams().WithBody(&models.V1SizeReservationListRequest{ + Projectid: "project-1", + Sizeid: "size-1", + Tenant: "tenant-1", + Partitionid: "partition-1", + })), nil).Return(&size.ListSizeReservationsOK{Payload: reservations}, nil) }, }, want: reservations, @@ -407,9 +427,9 @@ a size-1 tenant-1 1 project-1 5/5 10 b size-2 tenant-2 2 project-2 1/3 1 `), wantWideTable: pointer.Pointer(` -PARTITION SIZE TENANT PROJECT PROJECT NAME USED/AMOUNT PROJECT ALLOCATIONS -a size-1 tenant-1 1 project-1 5/5 10 -b size-2 tenant-2 2 project-2 1/3 1 +PARTITION SIZE TENANT PROJECT PROJECT NAME USED/AMOUNT PROJECT ALLOCATIONS LABELS +a size-1 tenant-1 1 project-1 5/5 10 size.metal-stack.io/reserved-by=admin +b size-2 tenant-2 2 project-2 1/3 1 size.metal-stack.io/reserved-by=admin `), wantMarkdown: pointer.Pointer(` | PARTITION | SIZE | TENANT | PROJECT | PROJECT NAME | USED/AMOUNT | PROJECT ALLOCATIONS | diff --git a/cmd/tableprinters/size.go b/cmd/tableprinters/size.go index cd8c34a4..6ad7e31f 100644 --- a/cmd/tableprinters/size.go +++ b/cmd/tableprinters/size.go @@ -69,10 +69,14 @@ func (t *TablePrinter) SizeReservationTable(data []*models.V1SizeReservationResp rows [][]string ) + if wide { + header = append(header, "Labels") + } + for _, d := range data { d := d - rows = append(rows, []string{ + row := []string{ pointer.SafeDeref(d.Partitionid), pointer.SafeDeref(d.Sizeid), pointer.SafeDeref(d.Tenant), @@ -80,7 +84,15 @@ func (t *TablePrinter) SizeReservationTable(data []*models.V1SizeReservationResp pointer.SafeDeref(d.Projectname), fmt.Sprintf("%d/%d", pointer.SafeDeref(d.Usedreservations), pointer.SafeDeref(d.Reservations)), strconv.Itoa(int(pointer.SafeDeref(d.Projectallocations))), - }) + } + + if wide { + labels := genericcli.MapToLabels(d.Labels) + sort.Strings(labels) + row = append(row, strings.Join(labels, "\n")) + } + + rows = append(rows, row) } return header, rows, nil diff --git a/docs/metalctl_size_reservations_list.md b/docs/metalctl_size_reservations_list.md index 1eed6cf3..13f8c8ad 100644 --- a/docs/metalctl_size_reservations_list.md +++ b/docs/metalctl_size_reservations_list.md @@ -9,8 +9,12 @@ metalctl size reservations list [flags] ### Options ``` - -h, --help help for list - --sort-by strings sort by (comma separated) column(s), sort direction can be changed by appending :asc or :desc behind the column identifier. possible values: partition|project|size|tenant + -h, --help help for list + --partition string the partition to filter + --project string the project to filter + --size-id string the size-id to filter + --sort-by strings sort by (comma separated) column(s), sort direction can be changed by appending :asc or :desc behind the column identifier. possible values: partition|project|size|tenant + --tenant string the tenant to filter ``` ### Options inherited from parent commands diff --git a/go.mod b/go.mod index c9fd8ffa..977b2eec 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,8 @@ require ( github.com/go-openapi/strfmt v0.23.0 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 - github.com/metal-stack/metal-go v0.32.0 - github.com/metal-stack/metal-lib v0.17.0 + github.com/metal-stack/metal-go v0.32.2 + github.com/metal-stack/metal-lib v0.17.1 github.com/metal-stack/updater v1.2.2 github.com/metal-stack/v v1.0.3 github.com/olekukonko/tablewriter v0.0.6-0.20230925090304-df64c4bbad77 diff --git a/go.sum b/go.sum index 25109004..3719e854 100644 --- a/go.sum +++ b/go.sum @@ -235,10 +235,10 @@ github.com/mdlayher/sdnotify v1.0.0 h1:Ma9XeLVN/l0qpyx1tNeMSeTjCPH6NtuD6/N9XdTlQ github.com/mdlayher/sdnotify v1.0.0/go.mod h1:HQUmpM4XgYkhDLtd+Uad8ZFK1T9D5+pNxnXQjCeJlGE= github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos= github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ= -github.com/metal-stack/metal-go v0.32.0 h1:8wpKkyx36qw4oKB0tt/95uENrw40DkCFbjq7bFz3KMs= -github.com/metal-stack/metal-go v0.32.0/go.mod h1:3MJTYCS4YJz8D8oteTKhjpaAKNMMjMKYDrIy9awHGtQ= -github.com/metal-stack/metal-lib v0.17.0 h1:0fCRUtYweJ5wbUwiEalFGiHkEz0mZwTWQUIIo3Npzkw= -github.com/metal-stack/metal-lib v0.17.0/go.mod h1:nyNGI4DZFOcWbSoq2Y6V3SHpFxuXBIqYBZHTb6cy//s= +github.com/metal-stack/metal-go v0.32.2 h1:vD1LtGVAeLx9vrPrguPBchXYsp7/oZ5MfTnfUO/yMz0= +github.com/metal-stack/metal-go v0.32.2/go.mod h1:3MJTYCS4YJz8D8oteTKhjpaAKNMMjMKYDrIy9awHGtQ= +github.com/metal-stack/metal-lib v0.17.1 h1:JLa4wJ62dgxtY9UOLF+QDk10/i/W5vhzrv8RsundDUY= +github.com/metal-stack/metal-lib v0.17.1/go.mod h1:nyNGI4DZFOcWbSoq2Y6V3SHpFxuXBIqYBZHTb6cy//s= github.com/metal-stack/security v0.8.0 h1:tVaSDB9m5clwYrnLyaXfPy7mQlJTnmeoHscG+RUy/xo= github.com/metal-stack/security v0.8.0/go.mod h1:7GAcQb+pOgflW30ohJygxpqc3i0dQ2ahGJK1CU5tqa0= github.com/metal-stack/updater v1.2.2 h1:gnUrnQgfT20QFMDtFBY89opKoBAkdeI/8T2iwMHNdxs=