Repository navigation
Size Reservations #80
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
3
commits into
main
Choose a base branch
from
size-reservations
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.
Open
Changes from all commits
Commits
Show all changes
3 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
| 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. | ||
| 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 | ||
| } | ||
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,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 | ||
| } |
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,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 { | ||
|
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. Filter by amount might be interesting?
Contributor
Author
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. 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"}}) | ||
| } | ||
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,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 | ||
| } |
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.
The question is why... in the repository the get function is implemented. 😅
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.
Wait... I think I found a get in the normal API and not the Admin API?