Skip to content
Draft
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/v1/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {

adminCmd.AddCommand(newTokenCmd(c))
adminCmd.AddCommand(newImageCmd(c))
adminCmd.AddCommand(newMachineCmd(c))

cmd.AddCommand(adminCmd)
}
104 changes: 104 additions & 0 deletions cmd/admin/v1/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package v1

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/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/spf13/cobra"
)

type machine struct {
c *config.Config
}

func newMachineCmd(c *config.Config) *cobra.Command {
w := &machine{
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.Machine]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "machine",
Plural: "machines",
Description: "an machine of metal-stack.io",
Sorter: sorters.MachineSorter(),
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project from where machines should be listed")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion))
},
DescribeCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project of the machine")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion))
},
ValidArgsFn: c.Completion.MachineListCompletion,
}

return genericcli.NewCmds(cmdsConfig)
}

func (c *machine) updateFromCLI(args []string) (any, error) {

Check failure on line 47 in cmd/admin/v1/machine.go

View workflow job for this annotation

GitHub Actions / test

func (*machine).updateFromCLI is unused (unused)
panic("unimplemented")
}

func (c *machine) Create(rq any) (*apiv2.Machine, error) {
panic("unimplemented")
}

func (c *machine) Delete(id string) (*apiv2.Machine, error) {
panic("unimplemented")
}

func (c *machine) Get(id string) (*apiv2.Machine, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Adminv2().Machine().Get(ctx, &adminv2.MachineServiceGetRequest{
Uuid: id,
})
if err != nil {
return nil, err
}

return resp.Machine, nil
}

func (c *machine) List() ([]*apiv2.Machine, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Adminv2().Machine().List(ctx, &adminv2.MachineServiceListRequest{
Query: &apiv2.MachineQuery{
// FIXME implement
},
})
if err != nil {
return nil, err
}

return resp.Machines, nil
}

func (c *machine) Update(rq any) (*apiv2.Machine, error) {
panic("unimplemented")
}

func (c *machine) Convert(r *apiv2.Machine) (string, any, any, error) {
panic("unimplemented")

}

func (c *machine) MachineResponseToCreate(r *apiv2.Machine) any {
panic("unimplemented")
}

func (c *machine) MachineResponseToUpdate(desired *apiv2.Machine) (any, error) {
panic("unimplemented")
}
1 change: 1 addition & 0 deletions cmd/api/v1/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
cmd.AddCommand(newImageCmd(c))
cmd.AddCommand(newProjectCmd(c))
cmd.AddCommand(newTenantCmd(c))
cmd.AddCommand(newMachineCmd(c))
cmd.AddCommand(newMethodsCmd(c))
cmd.AddCommand(newUserCmd(c))
}
108 changes: 108 additions & 0 deletions cmd/api/v1/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package v1

import (
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/spf13/cobra"
)

type machine struct {
c *config.Config
}

func newMachineCmd(c *config.Config) *cobra.Command {
w := &machine{
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[*apiv2.MachineServiceCreateRequest, *apiv2.MachineServiceUpdateRequest, *apiv2.Machine]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "machine",
Plural: "machines",
Description: "an machine of metal-stack.io",
Sorter: sorters.MachineSorter(),
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project from where machines should be listed")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion))
},
DescribeCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("project", "p", "", "project of the machine")

genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion))
},
ValidArgsFn: c.Completion.MachineListCompletion,
}

return genericcli.NewCmds(cmdsConfig)
}

func (c *machine) updateFromCLI(args []string) (*apiv2.MachineServiceUpdateRequest, error) {

Check failure on line 47 in cmd/api/v1/machine.go

View workflow job for this annotation

GitHub Actions / test

func (*machine).updateFromCLI is unused (unused)
panic("unimplemented")
}

func (c *machine) Create(rq *apiv2.MachineServiceCreateRequest) (*apiv2.Machine, error) {
panic("unimplemented")
}

func (c *machine) Delete(id string) (*apiv2.Machine, error) {
panic("unimplemented")
}

func (c *machine) Get(id string) (*apiv2.Machine, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Apiv2().Machine().Get(ctx, &apiv2.MachineServiceGetRequest{
Project: c.c.GetProject(),
Uuid: id,
})
if err != nil {
return nil, err
}

return resp.Machine, nil
}

func (c *machine) List() ([]*apiv2.Machine, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Apiv2().Machine().List(ctx, &apiv2.MachineServiceListRequest{
Project: c.c.GetProject(),
Query: &apiv2.MachineQuery{
// FIXME implement
},
})
if err != nil {
return nil, err
}

return resp.Machines, nil
}

func (c *machine) Update(rq *apiv2.MachineServiceUpdateRequest) (*apiv2.Machine, error) {
panic("unimplemented")
}

func (c *machine) Convert(r *apiv2.Machine) (string, *apiv2.MachineServiceCreateRequest, *apiv2.MachineServiceUpdateRequest, error) {
responseToUpdate, err := c.MachineResponseToUpdate(r)
return helpers.EncodeProject(r.Uuid, r.Allocation.Project), c.MachineResponseToCreate(r), responseToUpdate, err
}

func (c *machine) MachineResponseToCreate(r *apiv2.Machine) *apiv2.MachineServiceCreateRequest {
return &apiv2.MachineServiceCreateRequest{
// FIXME
}
}

func (c *machine) MachineResponseToUpdate(desired *apiv2.Machine) (*apiv2.MachineServiceUpdateRequest, error) {
panic("unimplemented")
}
21 changes: 21 additions & 0 deletions cmd/completion/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package completion

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

func (c *Completion) MachineListCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
req := &apiv2.MachineServiceListRequest{
Project: c.Project,
}
resp, err := c.Client.Apiv2().Machine().List(c.Ctx, req)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, s := range resp.Machines {
names = append(names, s.Uuid)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
1 change: 1 addition & 0 deletions cmd/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newPrinterFromCLI(out io.Writer) (printers.Printer, error) {
}
tablePrinter := printers.NewTablePrinter(cfg).WithOut(out)
tp.SetPrinter(tablePrinter)
tp.SetLastEventErrorThreshold(viper.GetDuration("last-event-error-threshold"))
printer = tablePrinter
case "template":
printer = printers.NewTemplatePrinter(viper.GetString("template")).WithOut(out)
Expand Down
20 changes: 20 additions & 0 deletions cmd/sorters/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sorters

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

func MachineSorter() *multisort.Sorter[*apiv2.Machine] {
return multisort.New(multisort.FieldMap[*apiv2.Machine]{
"partition": func(a, b *apiv2.Machine, descending bool) multisort.CompareResult {
return multisort.Compare(a.Partition.Id, b.Partition.Id, descending)
},
"size": func(a, b *apiv2.Machine, descending bool) multisort.CompareResult {
return multisort.Compare(a.Size.Id, b.Size.Id, descending)
},
"uuid": func(a, b *apiv2.Machine, descending bool) multisort.CompareResult {
return multisort.Compare(a.Uuid, b.Uuid, descending)
},
}, multisort.Keys{{ID: "uuid"}, {ID: "size"}, {ID: "partition"}})
}
12 changes: 11 additions & 1 deletion cmd/tableprinters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
)

type TablePrinter struct {
t *printers.TablePrinter
t *printers.TablePrinter
lastEventErrorThreshold time.Duration
}

func New() *TablePrinter {
Expand All @@ -24,6 +25,10 @@ func (t *TablePrinter) SetPrinter(printer *printers.TablePrinter) {
t.t = printer
}

func (t *TablePrinter) SetLastEventErrorThreshold(threshold time.Duration) {
t.lastEventErrorThreshold = threshold
}

func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]string, error) {
switch d := data.(type) {

Expand All @@ -35,6 +40,11 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
case []*apiv2.IP:
return t.IPTable(d, wide)

case *apiv2.Machine:
return t.MachineTable(pointer.WrapInSlice(d), wide)
case []*apiv2.Machine:
return t.MachineTable(d, wide)

case *apiv2.Image:
return t.ImageTable(pointer.WrapInSlice(d), wide)
case []*apiv2.Image:
Expand Down
10 changes: 10 additions & 0 deletions cmd/tableprinters/icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tableprinters

const (
dot = "●"
halfpie = "◒"
threequarterpie = "◕"
nbr = " "
poweron = "⏻"
powersleep = "⏾"
)
Loading
Loading