-
Notifications
You must be signed in to change notification settings - Fork 0
Partition #19
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
Open
majst01
wants to merge
6
commits into
main
Choose a base branch
from
partition-capacity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+809
−23
Open
Partition #19
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| 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/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 partition struct { | ||
| c *config.Config | ||
| } | ||
|
|
||
| func newPartitionCmd(c *config.Config) *cobra.Command { | ||
| w := &partition{ | ||
| c: c, | ||
| } | ||
|
|
||
| gcli := genericcli.NewGenericCLI(w).WithFS(c.Fs) | ||
|
|
||
| cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.Partition]{ | ||
| BinaryName: config.BinaryName, | ||
| GenericCLI: gcli, | ||
| Singular: "partition", | ||
| Plural: "partitions", | ||
| Description: "manage partitions", | ||
| DescribePrinter: func() printers.Printer { return c.DescribePrinter }, | ||
| ListPrinter: func() printers.Printer { return c.ListPrinter }, | ||
| OnlyCmds: genericcli.OnlyCmds(genericcli.DescribeCmd, genericcli.ListCmd), | ||
| DescribeCmdMutateFn: func(cmd *cobra.Command) { | ||
| cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
| return gcli.DescribeAndPrint("", w.c.DescribePrinter) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| capacityCmd := &cobra.Command{ | ||
| Use: "capacity", | ||
| Short: "show partition capacity", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return w.capacity() | ||
| }, | ||
| } | ||
|
|
||
| capacityCmd.Flags().StringP("id", "", "", "filter on partition id.") | ||
| capacityCmd.Flags().StringP("size", "", "", "filter on size id.") | ||
| capacityCmd.Flags().StringP("project", "", "", "consider project-specific counts, e.g. size reservations.") | ||
| capacityCmd.Flags().StringSlice("sort-by", []string{}, fmt.Sprintf("order by (comma separated) column(s), sort direction can be changed by appending :asc or :desc behind the column identifier. possible values: %s", strings.Join(sorters.PartitionCapacitySorter().AvailableKeys(), "|"))) | ||
| genericcli.Must(capacityCmd.RegisterFlagCompletionFunc("id", c.Completion.PartitionListCompletion)) | ||
| genericcli.Must(capacityCmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion)) | ||
| genericcli.Must(capacityCmd.RegisterFlagCompletionFunc("size", c.Completion.SizeListCompletion)) | ||
| genericcli.Must(capacityCmd.RegisterFlagCompletionFunc("sort-by", cobra.FixedCompletions(sorters.PartitionCapacitySorter().AvailableKeys(), cobra.ShellCompDirectiveNoFileComp))) | ||
|
|
||
| return genericcli.NewCmds(cmdsConfig, capacityCmd) | ||
| } | ||
|
|
||
| func (c *partition) capacity() error { | ||
| ctx, cancel := c.c.NewRequestContext() | ||
| defer cancel() | ||
|
|
||
| req := &adminv2.PartitionServiceCapacityRequest{} | ||
|
|
||
| if viper.IsSet("id") { | ||
| req.Id = new(viper.GetString("id")) | ||
| } | ||
| if viper.IsSet("size") { | ||
| req.Size = new(viper.GetString("size")) | ||
| } | ||
| if viper.IsSet("project") { | ||
| req.Project = new(viper.GetString("project")) | ||
| } | ||
| resp, err := c.c.Client.Adminv2().Partition().Capacity(ctx, req) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get partition capacity: %w", err) | ||
| } | ||
|
|
||
| err = sorters.PartitionCapacitySorter().SortBy(resp.PartitionCapacity) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.c.ListPrinter.Print(resp.PartitionCapacity) | ||
| } | ||
|
|
||
| func (c *partition) Get(id string) (*apiv2.Partition, error) { | ||
| ctx, cancel := c.c.NewRequestContext() | ||
| defer cancel() | ||
|
|
||
| req := &apiv2.PartitionServiceGetRequest{Id: id} | ||
|
|
||
| resp, err := c.c.Client.Apiv2().Partition().Get(ctx, req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get partition: %w", err) | ||
| } | ||
|
|
||
| return resp.Partition, nil | ||
| } | ||
|
|
||
| func (c *partition) List() ([]*apiv2.Partition, error) { | ||
| ctx, cancel := c.c.NewRequestContext() | ||
| defer cancel() | ||
|
|
||
| req := &apiv2.PartitionServiceListRequest{Query: &apiv2.PartitionQuery{ | ||
| Id: pointer.PointerOrNil(viper.GetString("id")), | ||
| }} | ||
|
|
||
| resp, err := c.c.Client.Apiv2().Partition().List(ctx, req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get partitions: %w", err) | ||
| } | ||
|
|
||
| return resp.Partitions, nil | ||
| } | ||
|
|
||
| func (c *partition) Create(rq any) (*apiv2.Partition, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (c *partition) Delete(id string) (*apiv2.Partition, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (t *partition) Convert(r *apiv2.Partition) (string, any, any, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (t *partition) Update(rq any) (*apiv2.Partition, error) { | ||
| panic("unimplemented") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| package v2_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2" | ||
| apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" | ||
| apitests "github.com/metal-stack/api/go/tests" | ||
| "github.com/metal-stack/cli/pkg/test" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| // Generated with AI | ||
|
|
||
| var ( | ||
| testPartition1 = &apiv2.Partition{ | ||
| Id: "1", | ||
| Description: "partition 1", | ||
| MgmtServiceAddresses: []string{ | ||
| "192.168.1.1:1234", | ||
| }, | ||
| BootConfiguration: &apiv2.PartitionBootConfiguration{ | ||
| Commandline: "commandline", | ||
| ImageUrl: "imageurl", | ||
| KernelUrl: "kernelurl", | ||
| }, | ||
| Meta: &apiv2.Meta{ | ||
| Labels: &apiv2.Labels{ | ||
| Labels: map[string]string{ | ||
| "a": "b", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| testPartition2 = &apiv2.Partition{ | ||
| Id: "2", | ||
| Description: "partition 2", | ||
| MgmtServiceAddresses: []string{ | ||
| "192.168.1.2:1234", | ||
| }, | ||
| BootConfiguration: &apiv2.PartitionBootConfiguration{ | ||
| Commandline: "commandline", | ||
| ImageUrl: "imageurl", | ||
| KernelUrl: "kernelurl", | ||
| }, | ||
| Meta: &apiv2.Meta{ | ||
| Labels: &apiv2.Labels{ | ||
| Labels: nil, | ||
| }, | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| func Test_AdminPartitionCmd_List(t *testing.T) { | ||
| tests := []*test.Test[[]*apiv2.Partition]{ | ||
| { | ||
| Name: "list", | ||
| Cmd: func(want []*apiv2.Partition) []string { | ||
| return []string{"admin", "partition", "list"} | ||
| }, | ||
| ClientMocks: &apitests.ClientMockFns{ | ||
| Apiv2Mocks: &apitests.Apiv2MockFns{ | ||
| Partition: func(m *mock.Mock) { | ||
| m.On("List", mock.Anything, mock.Anything).Return(&apiv2.PartitionServiceListResponse{ | ||
| Partitions: []*apiv2.Partition{ | ||
| testPartition1, | ||
| testPartition2, | ||
| }, | ||
| }, nil) | ||
| }, | ||
| }, | ||
| }, | ||
| Want: []*apiv2.Partition{ | ||
| testPartition1, | ||
| testPartition2, | ||
| }, | ||
| WantTable: new(` | ||
| ID DESCRIPTION | ||
| 1 partition 1 | ||
| 2 partition 2 | ||
| `), | ||
| }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No template output? 😢 |
||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No markdown test? :( |
||
| for _, tt := range tests { | ||
| tt.TestCmd(t) | ||
| } | ||
| } | ||
|
|
||
| func Test_AdminPartitionCmd_Describe(t *testing.T) { | ||
| tests := []*test.Test[*apiv2.Partition]{ | ||
| { | ||
| Name: "describe", | ||
| Cmd: func(want *apiv2.Partition) []string { | ||
| return []string{"admin", "partition", "describe", want.Id} | ||
| }, | ||
| ClientMocks: &apitests.ClientMockFns{ | ||
| Apiv2Mocks: &apitests.Apiv2MockFns{ | ||
| Partition: func(m *mock.Mock) { | ||
| m.On("Get", mock.Anything, mock.Anything).Return(&apiv2.PartitionServiceGetResponse{ | ||
| Partition: testPartition1, | ||
| }, nil) | ||
| }, | ||
| }, | ||
| }, | ||
| Want: testPartition1, | ||
| WantTable: new(` | ||
| ID DESCRIPTION | ||
| 1 partition 1 | ||
| `), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| tt.TestCmd(t) | ||
| } | ||
| } | ||
|
|
||
| func Test_AdminPartitionCmd_Capacity(t *testing.T) { | ||
| tests := []*test.Test[[]*adminv2.PartitionCapacity]{ | ||
| { | ||
| Name: "capacity", | ||
| Cmd: func(want []*adminv2.PartitionCapacity) []string { | ||
| return []string{"admin", "partition", "capacity"} | ||
| }, | ||
| ClientMocks: &apitests.ClientMockFns{ | ||
| Adminv2Mocks: &apitests.Adminv2MockFns{ | ||
| Partition: func(m *mock.Mock) { | ||
| m.On("Capacity", mock.Anything, mock.Anything).Return(&adminv2.PartitionServiceCapacityResponse{ | ||
| PartitionCapacity: []*adminv2.PartitionCapacity{ | ||
| { | ||
| Partition: "partition-1", | ||
| MachineSizeCapacities: []*adminv2.MachineSizeCapacity{ | ||
| { | ||
| Size: "size-1", | ||
| Free: 3, | ||
| Allocated: 1, | ||
| Total: 5, | ||
| Faulty: 2, | ||
| Reservations: 3, | ||
| UsedReservations: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, nil) | ||
| }, | ||
| }, | ||
| }, | ||
| Want: []*adminv2.PartitionCapacity{ | ||
| { | ||
| Partition: "partition-1", | ||
| MachineSizeCapacities: []*adminv2.MachineSizeCapacity{ | ||
| { | ||
| Size: "size-1", | ||
| Free: 3, | ||
| Allocated: 1, | ||
| Total: 5, | ||
| Faulty: 2, | ||
| Reservations: 3, | ||
| UsedReservations: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| WantTable: new(` | ||
| PARTITION SIZE ALLOCATED FREE UNAVAILABLE RESERVATIONS | TOTAL | FAULTY | ||
| partition-1 size-1 1 3 0 2 (1/3 used) | 5 | 2 | ||
| Total 1 3 0 2 | 5 | 2 | ||
| `), | ||
| }, | ||
| { | ||
| Name: "capacity with filters", | ||
| Cmd: func(want []*adminv2.PartitionCapacity) []string { | ||
| return []string{"admin", "partition", "capacity", "--id", "partition-1", "--size", "size-1", "--project", "project-123", "--sort-by", "id"} | ||
| }, | ||
| ClientMocks: &apitests.ClientMockFns{ | ||
| Adminv2Mocks: &apitests.Adminv2MockFns{ | ||
| Partition: func(m *mock.Mock) { | ||
| m.On("Capacity", mock.Anything, mock.Anything).Return(&adminv2.PartitionServiceCapacityResponse{ | ||
| PartitionCapacity: []*adminv2.PartitionCapacity{ | ||
| { | ||
| Partition: "partition-1", | ||
| MachineSizeCapacities: []*adminv2.MachineSizeCapacity{ | ||
| { | ||
| Size: "size-1", | ||
| Free: 3, | ||
| Allocated: 1, | ||
| Total: 5, | ||
| Faulty: 2, | ||
| Reservations: 3, | ||
| UsedReservations: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, nil) | ||
| }, | ||
| }, | ||
| }, | ||
| Want: []*adminv2.PartitionCapacity{ | ||
| { | ||
| Partition: "partition-1", | ||
| MachineSizeCapacities: []*adminv2.MachineSizeCapacity{ | ||
| { | ||
| Size: "size-1", | ||
| Free: 3, | ||
| Allocated: 1, | ||
| Total: 5, | ||
| Faulty: 2, | ||
| Reservations: 3, | ||
| UsedReservations: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| WantTable: new(` | ||
| PARTITION SIZE ALLOCATED FREE UNAVAILABLE RESERVATIONS | TOTAL | FAULTY | ||
| partition-1 size-1 1 3 0 2 (1/3 used) | 5 | 2 | ||
| Total 1 3 0 2 | 5 | 2 | ||
| `), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| tt.TestCmd(t) | ||
| } | ||
| } | ||
|
|
||
| func Test_AdminPartitionCmd_ExhaustiveArgs(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| }{ | ||
| { | ||
| name: "list", | ||
| args: []string{"admin", "partition", "list"}, | ||
| }, | ||
| { | ||
| name: "describe", | ||
| args: []string{"admin", "partition", "describe", "1"}, | ||
| }, | ||
| { | ||
| name: "capacity", | ||
| args: []string{"admin", "partition", "capacity", "--id", "partition-1", "--size", "size-1", "--project", "project-123", "--sort-by", "id"}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| test.AssertExhaustiveArgs(t, tt.args) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No wide table?