diff --git a/cmd/switch.go b/cmd/switch.go index 1540544..ba28166 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -105,13 +105,15 @@ r01leaf01,swp2,44e3a522-5f48-4f3c-9188-41025f9e401e, switchMachinesCmd.Flags().String("os-version", "", "OS version of this switch.") switchMachinesCmd.Flags().String("partition", "", "Partition of this switch.") switchMachinesCmd.Flags().String("rack", "", "Rack of this switch.") - switchMachinesCmd.Flags().String("size", "", "Size of the connectedmachines.") + switchMachinesCmd.Flags().String("size", "", "Size of the connected machines.") + switchMachinesCmd.Flags().String("machine-id", "", "The id of the connected machine, ignores size flag if set.") must(switchMachinesCmd.RegisterFlagCompletionFunc("id", c.comp.SwitchListCompletion)) must(switchMachinesCmd.RegisterFlagCompletionFunc("name", c.comp.SwitchNameListCompletion)) must(switchMachinesCmd.RegisterFlagCompletionFunc("partition", c.comp.PartitionListCompletion)) must(switchMachinesCmd.RegisterFlagCompletionFunc("rack", c.comp.SwitchRackListCompletion)) must(switchMachinesCmd.RegisterFlagCompletionFunc("size", c.comp.SizeListCompletion)) + must(switchMachinesCmd.RegisterFlagCompletionFunc("machine-id", c.comp.MachineListCompletion)) switchReplaceCmd := &cobra.Command{ Use: "replace ", @@ -256,29 +258,43 @@ func (c *switchCmd) switchMachines() error { return err } - resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{ - PartitionID: viper.GetString("partition"), - Rackid: viper.GetString("rack"), - Sizeid: viper.GetString("size"), - }), nil) - if err != nil { - return err + var machines []*models.V1MachineIPMIResponse + + if viper.IsSet("machine-id") { + resp, err := c.client.Machine().FindIPMIMachine(machine.NewFindIPMIMachineParams().WithID(viper.GetString("machine-id")), nil) + if err != nil { + return err + } + + machines = append(machines, resp.Payload) + } else { + resp, err := c.client.Machine().FindIPMIMachines(machine.NewFindIPMIMachinesParams().WithBody(&models.V1MachineFindRequest{ + ID: viper.GetString("machine-id"), + PartitionID: viper.GetString("partition"), + Rackid: viper.GetString("rack"), + Sizeid: viper.GetString("size"), + }), nil) + if err != nil { + return err + } + + machines = append(machines, resp.Payload...) } - machines := map[string]*models.V1MachineIPMIResponse{} - for _, m := range resp.Payload { + ms := map[string]*models.V1MachineIPMIResponse{} + for _, m := range machines { m := m if m.ID == nil { continue } - machines[*m.ID] = m + ms[*m.ID] = m } return c.listPrinter.Print(&tableprinters.SwitchesWithMachines{ SS: switches, - MS: machines, + MS: ms, }) } diff --git a/cmd/tableprinters/switch.go b/cmd/tableprinters/switch.go index 3387d66..7769119 100644 --- a/cmd/tableprinters/switch.go +++ b/cmd/tableprinters/switch.go @@ -127,8 +127,9 @@ func (t *TablePrinter) SwitchWithConnectedMachinesTable(data *SwitchesWithMachin rows = append(rows, []string{id, "", "", partition, rack}) conns := s.Connections - if viper.IsSet("size") { - conns = []*models.V1SwitchConnection{} + if viper.IsSet("size") || viper.IsSet("machine-id") { + filteredConns := []*models.V1SwitchConnection{} + for _, conn := range s.Connections { conn := conn @@ -137,10 +138,16 @@ func (t *TablePrinter) SwitchWithConnectedMachinesTable(data *SwitchesWithMachin continue } - if pointer.SafeDeref(m.Size.ID) == viper.GetString("size") { - conns = append(conns, conn) + if viper.IsSet("machine-id") && pointer.SafeDeref(m.ID) == viper.GetString("machine-id") { + filteredConns = append(filteredConns, conn) + } + + if viper.IsSet("size") && pointer.SafeDeref(m.Size.ID) == viper.GetString("size") { + filteredConns = append(filteredConns, conn) } } + + conns = filteredConns } sort.Slice(conns, switchInterfaceNameLessFunc(conns)) diff --git a/docs/metalctl_switch_connected-machines.md b/docs/metalctl_switch_connected-machines.md index 8d6619f..1a937e3 100644 --- a/docs/metalctl_switch_connected-machines.md +++ b/docs/metalctl_switch_connected-machines.md @@ -25,12 +25,13 @@ r01leaf01,swp2,44e3a522-5f48-4f3c-9188-41025f9e401e, ``` -h, --help help for connected-machines --id string ID of the switch. + --machine-id string The id of the connected machine, ignores size flag if set. --name string Name of the switch. --os-vendor string OS vendor of this switch. --os-version string OS version of this switch. --partition string Partition of this switch. --rack string Rack of this switch. - --size string Size of the connectedmachines. + --size string Size of the connected machines. ``` ### Options inherited from parent commands