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
1 change: 1 addition & 0 deletions cmd/admin/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
adminCmd.AddCommand(newAuditCmd(c))
adminCmd.AddCommand(newComponentCmd(c))
adminCmd.AddCommand(newImageCmd(c))
adminCmd.AddCommand(newPartitionCmd(c))
adminCmd.AddCommand(newProjectCmd(c))
adminCmd.AddCommand(newProjectCmd(c))
adminCmd.AddCommand(newSwitchCmd(c))
Expand Down
149 changes: 149 additions & 0 deletions cmd/admin/v2/partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
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.Flags().String("id", "", "id of the partition")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
id, err := cmd.Flags().GetString("id")
if err != nil {
return err
}
if id == "" && len(args) > 0 {
id = args[0]
}
p, err := w.Get(id)
if err != nil {
return err
}
return w.c.DescribePrinter.Print(p)
}
},
}

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")
}
1 change: 1 addition & 0 deletions cmd/api/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
cmd.AddCommand(newImageCmd(c))
cmd.AddCommand(newIPCmd(c))
cmd.AddCommand(newMethodsCmd(c))
cmd.AddCommand(newPartitionCmd(c))
cmd.AddCommand(newProjectCmd(c))
cmd.AddCommand(newTenantCmd(c))
cmd.AddCommand(newTokenCmd(c))
Expand Down
87 changes: 87 additions & 0 deletions cmd/api/v2/partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package v2

import (
"fmt"

apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"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: "list and get partitions",
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
OnlyCmds: genericcli.OnlyCmds(genericcli.DescribeCmd, genericcli.ListCmd),
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("id", "", "", "image id to filter for")
},
}

return genericcli.NewCmds(cmdsConfig)
}

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")
}
19 changes: 19 additions & 0 deletions cmd/completion/partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package completion

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

func (c *Completion) PartitionListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.PartitionServiceListRequest{}
resp, err := c.Client.Apiv2().Partition().List(c.Ctx, req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, p := range resp.Partitions {
names = append(names, p.Id+"\t"+p.Description)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
19 changes: 19 additions & 0 deletions cmd/completion/size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package completion

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

func (c *Completion) SizeListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.SizeServiceListRequest{}
resp, err := c.Client.Apiv2().Size().List(c.Ctx, req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, s := range resp.Sizes {
names = append(names, s.Id)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
30 changes: 30 additions & 0 deletions cmd/sorters/partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sorters

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

func PartitionSorter() *multisort.Sorter[*apiv2.Partition] {
return multisort.New(multisort.FieldMap[*apiv2.Partition]{
"id": func(a, b *apiv2.Partition, descending bool) multisort.CompareResult {
return multisort.Compare(a.Id, b.Id, descending)
},
"description": func(a, b *apiv2.Partition, descending bool) multisort.CompareResult {
return multisort.Compare(a.Description, b.Description, descending)
},
}, multisort.Keys{{ID: "id"}, {ID: "description"}})
}

func PartitionCapacitySorter() *multisort.Sorter[*adminv2.PartitionCapacity] {
return multisort.New(multisort.FieldMap[*adminv2.PartitionCapacity]{
"id": func(a, b *adminv2.PartitionCapacity, descending bool) multisort.CompareResult {
return multisort.Compare(a.Partition, b.Partition, descending)
},
"size": func(a, b *adminv2.PartitionCapacity, descending bool) multisort.CompareResult {
// FIXME implement
return multisort.Compare(a.Partition, b.Partition, descending)
},
}, multisort.Keys{{ID: "id"}, {ID: "size"}})
}
9 changes: 9 additions & 0 deletions cmd/tableprinters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
case []*apiv2.ProjectMember:
return t.ProjectMemberTable(d, wide)

case *apiv2.Partition:
return t.PartitionTable(pointer.WrapInSlice(d), wide)
case []*apiv2.Partition:
return t.PartitionTable(d, wide)
case *adminv2.PartitionCapacity:
return t.PartitionCapacityTable(pointer.WrapInSlice(d), wide)
case []*adminv2.PartitionCapacity:
return t.PartitionCapacityTable(d, wide)

case *apiv2.Token:
return t.TokenTable(pointer.WrapInSlice(d), wide)
case []*apiv2.Token:
Expand Down
Loading
Loading