Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Size reservation labels and dependency updates. #247

Merged
merged 8 commits into from
Jul 9, 2024
Merged
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
26 changes: 21 additions & 5 deletions cmd/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down
30 changes: 25 additions & 5 deletions cmd/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
Expand All @@ -386,18 +396,28 @@ 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",
},
},
}

tests := []*test[[]*models.V1SizeReservationResponse]{
{
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,
Expand All @@ -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 |
Expand Down
16 changes: 14 additions & 2 deletions cmd/tableprinters/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,30 @@ 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),
pointer.SafeDeref(d.Projectid),
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
Expand Down
8 changes: 6 additions & 2 deletions docs/metalctl_size_reservations_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading