Skip to content

Commit

Permalink
Allow filtering machine id in `switch connected-machines --machine-id…
Browse files Browse the repository at this point in the history
…`. (#209)
  • Loading branch information
Gerrit91 authored Sep 6, 2023
1 parent 938108b commit a4907b7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
40 changes: 28 additions & 12 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ r01leaf01,swp2,44e3a522-5f48-4f3c-9188-41025f9e401e,<b-serial>
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 <switchID>",
Expand Down Expand Up @@ -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,
})
}

Expand Down
15 changes: 11 additions & 4 deletions cmd/tableprinters/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion docs/metalctl_switch_connected-machines.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ r01leaf01,swp2,44e3a522-5f48-4f3c-9188-41025f9e401e,<b-serial>
```
-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
Expand Down

0 comments on commit a4907b7

Please sign in to comment.