|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2" |
| 7 | + apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" |
| 8 | + "github.com/metal-stack/cli/cmd/config" |
| 9 | + "github.com/metal-stack/cli/cmd/sorters" |
| 10 | + "github.com/metal-stack/metal-lib/pkg/genericcli" |
| 11 | + "github.com/metal-stack/metal-lib/pkg/genericcli/printers" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/viper" |
| 14 | +) |
| 15 | + |
| 16 | +type machine struct { |
| 17 | + c *config.Config |
| 18 | +} |
| 19 | + |
| 20 | +func newMachineCmd(c *config.Config) *cobra.Command { |
| 21 | + w := &machine{ |
| 22 | + c: c, |
| 23 | + } |
| 24 | + |
| 25 | + cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.Machine]{ |
| 26 | + BinaryName: config.BinaryName, |
| 27 | + GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs), |
| 28 | + Singular: "machine", |
| 29 | + Plural: "machines", |
| 30 | + Description: "an machine of metal-stack.io", |
| 31 | + Sorter: sorters.MachineSorter(), |
| 32 | + DescribePrinter: func() printers.Printer { return c.DescribePrinter }, |
| 33 | + ListPrinter: func() printers.Printer { return c.ListPrinter }, |
| 34 | + ListCmdMutateFn: func(cmd *cobra.Command) { |
| 35 | + cmd.Flags().StringP("project", "p", "", "project from where machines should be listed") |
| 36 | + |
| 37 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion)) |
| 38 | + }, |
| 39 | + DescribeCmdMutateFn: func(cmd *cobra.Command) { |
| 40 | + cmd.Flags().StringP("project", "p", "", "project of the machine") |
| 41 | + |
| 42 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion)) |
| 43 | + }, |
| 44 | + ValidArgsFn: c.Completion.MachineListCompletion, |
| 45 | + } |
| 46 | + |
| 47 | + bmcCommandCmd := &cobra.Command{ |
| 48 | + Use: "bmc-command", |
| 49 | + Short: "send a command to the bmc of a machine", |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + return w.bmcCommand() |
| 52 | + }, |
| 53 | + } |
| 54 | + bmcCommandCmd.Flags().String("id", "", "id of the machine where the command should be sent to") |
| 55 | + bmcCommandCmd.Flags().String("command", "", "the actual command to send to the machine") |
| 56 | + bmcCommandCmd.RegisterFlagCompletionFunc("id", c.Completion.MachineListCompletion) |
| 57 | + bmcCommandCmd.RegisterFlagCompletionFunc("command", c.Completion.BMCCommandListCompletion) |
| 58 | + genericcli.Must(bmcCommandCmd.MarkFlagRequired("id")) |
| 59 | + genericcli.Must(bmcCommandCmd.MarkFlagRequired("command")) |
| 60 | + |
| 61 | + return genericcli.NewCmds(cmdsConfig, bmcCommandCmd) |
| 62 | +} |
| 63 | + |
| 64 | +func (c *machine) bmcCommand() error { |
| 65 | + ctx, cancel := c.c.NewRequestContext() |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + commandString := viper.GetString("command") |
| 69 | + |
| 70 | + cmd, ok := apiv2.MachineBMCCommand_value[commandString] |
| 71 | + if !ok { |
| 72 | + return fmt.Errorf("unknown command: %s", commandString) |
| 73 | + } |
| 74 | + _, err := c.c.Client.Adminv2().Machine().BMCCommand(ctx, &adminv2.MachineServiceBMCCommandRequest{ |
| 75 | + Uuid: viper.GetString("id"), |
| 76 | + Command: apiv2.MachineBMCCommand(cmd), |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + return err |
| 82 | +} |
| 83 | + |
| 84 | +func (c *machine) updateFromCLI(args []string) (any, error) { |
| 85 | + panic("unimplemented") |
| 86 | +} |
| 87 | + |
| 88 | +func (c *machine) Create(rq any) (*apiv2.Machine, error) { |
| 89 | + panic("unimplemented") |
| 90 | +} |
| 91 | + |
| 92 | +func (c *machine) Delete(id string) (*apiv2.Machine, error) { |
| 93 | + panic("unimplemented") |
| 94 | +} |
| 95 | + |
| 96 | +func (c *machine) Get(id string) (*apiv2.Machine, error) { |
| 97 | + ctx, cancel := c.c.NewRequestContext() |
| 98 | + defer cancel() |
| 99 | + |
| 100 | + resp, err := c.c.Client.Adminv2().Machine().Get(ctx, &adminv2.MachineServiceGetRequest{ |
| 101 | + Uuid: id, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + return resp.Machine, nil |
| 108 | +} |
| 109 | + |
| 110 | +func (c *machine) List() ([]*apiv2.Machine, error) { |
| 111 | + ctx, cancel := c.c.NewRequestContext() |
| 112 | + defer cancel() |
| 113 | + |
| 114 | + resp, err := c.c.Client.Adminv2().Machine().List(ctx, &adminv2.MachineServiceListRequest{ |
| 115 | + Query: &apiv2.MachineQuery{ |
| 116 | + // FIXME implement |
| 117 | + }, |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return resp.Machines, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (c *machine) Update(rq any) (*apiv2.Machine, error) { |
| 127 | + panic("unimplemented") |
| 128 | +} |
| 129 | + |
| 130 | +func (c *machine) Convert(r *apiv2.Machine) (string, any, any, error) { |
| 131 | + panic("unimplemented") |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +func (c *machine) MachineResponseToCreate(r *apiv2.Machine) any { |
| 136 | + panic("unimplemented") |
| 137 | +} |
| 138 | + |
| 139 | +func (c *machine) MachineResponseToUpdate(desired *apiv2.Machine) (any, error) { |
| 140 | + panic("unimplemented") |
| 141 | +} |
0 commit comments