From e83add04e99840ba88565e6f9bcce36520cf212b Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 14 Sep 2023 10:35:13 +0200 Subject: [PATCH] Add wide table for `switch connected-machines`. (#210) --- cmd/switch_test.go | 16 +++++++++----- cmd/tableprinters/switch.go | 43 ++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/cmd/switch_test.go b/cmd/switch_test.go index 4a58690..2cf856f 100644 --- a/cmd/switch_test.go +++ b/cmd/switch_test.go @@ -263,6 +263,9 @@ func Test_SwitchCmd_ConnectedMachinesResult(t *testing.T) { { ID: pointer.Pointer("machine-1"), Rackid: "rack-1", + Allocation: &models.V1MachineAllocation{ + Hostname: pointer.Pointer("alloc-1"), + }, Partition: &models.V1PartitionResponse{ ID: pointer.Pointer("1"), }, @@ -296,6 +299,9 @@ func Test_SwitchCmd_ConnectedMachinesResult(t *testing.T) { "machine-1": { ID: pointer.Pointer("machine-1"), Rackid: "rack-1", + Allocation: &models.V1MachineAllocation{ + Hostname: pointer.Pointer("alloc-1"), + }, Partition: &models.V1PartitionResponse{ ID: pointer.Pointer("1"), }, @@ -318,11 +324,11 @@ ID NIC NAME IDENTIFIER PARTITION RACK SIZE PROD └─╴machine-1 a-name a-mac 1 rack-1 n1-medium-x86 123 `), wantWideTable: pointer.Pointer(` -ID NIC NAME IDENTIFIER PARTITION RACK SIZE PRODUCT SERIAL -1 1 rack-1 -└─╴machine-1 a-name a-mac 1 rack-1 n1-medium-x86 123 -2 1 rack-1 -└─╴machine-1 a-name a-mac 1 rack-1 n1-medium-x86 123 +ID NIC NAME IDENTIFIER PARTITION RACK SIZE HOSTNAME PRODUCT SERIAL +1 1 rack-1 +└─╴machine-1 ❓ a-name a-mac 1 rack-1 n1-medium-x86 alloc-1 123 +2 1 rack-1 +└─╴machine-1 ❓ a-name a-mac 1 rack-1 n1-medium-x86 alloc-1 123 `), template: pointer.Pointer(`{{ $machines := .machines }}{{ range .switches }}{{ $switch := . }}{{ range .connections }}{{ $switch.id }},{{ $switch.rack_id }},{{ .nic.name }},{{ .machine_id }},{{ (index $machines .machine_id).ipmi.fru.product_serial }}{{ printf "\n" }}{{ end }}{{ end }}`), wantTemplate: pointer.Pointer(` diff --git a/cmd/tableprinters/switch.go b/cmd/tableprinters/switch.go index 7769119..b58019a 100644 --- a/cmd/tableprinters/switch.go +++ b/cmd/tableprinters/switch.go @@ -118,13 +118,20 @@ func (t *TablePrinter) SwitchWithConnectedMachinesTable(data *SwitchesWithMachin ) header := []string{"ID", "NIC Name", "Identifier", "Partition", "Rack", "Size", "Product Serial"} + if wide { + header = []string{"ID", "", "NIC Name", "Identifier", "Partition", "Rack", "Size", "Hostname", "Product Serial"} + } for _, s := range data.SS { id := pointer.SafeDeref(s.ID) partition := pointer.SafeDeref(pointer.SafeDeref(s.Partition).ID) rack := pointer.SafeDeref(s.RackID) - rows = append(rows, []string{id, "", "", partition, rack}) + if wide { + rows = append(rows, []string{id, "", "", "", partition, rack}) + } else { + rows = append(rows, []string{id, "", "", partition, rack}) + } conns := s.Connections if viper.IsSet("size") || viper.IsSet("machine-id") { @@ -169,15 +176,31 @@ func (t *TablePrinter) SwitchWithConnectedMachinesTable(data *SwitchesWithMachin identifier = pointer.SafeDeref(conn.Nic.Mac) } - rows = append(rows, []string{ - fmt.Sprintf("%s%s", prefix, pointer.SafeDeref(m.ID)), - pointer.SafeDeref(pointer.SafeDeref(conn.Nic).Name), - identifier, - pointer.SafeDeref(pointer.SafeDeref(m.Partition).ID), - m.Rackid, - pointer.SafeDeref(pointer.SafeDeref(m.Size).ID), - pointer.SafeDeref(pointer.SafeDeref(m.Ipmi).Fru).ProductSerial, - }) + if wide { + emojis, _ := t.getMachineStatusEmojis(m.Liveliness, m.Events, m.State, pointer.SafeDeref(m.Allocation).Vpn) + + rows = append(rows, []string{ + fmt.Sprintf("%s%s", prefix, pointer.SafeDeref(m.ID)), + emojis, + pointer.SafeDeref(pointer.SafeDeref(conn.Nic).Name), + identifier, + pointer.SafeDeref(pointer.SafeDeref(m.Partition).ID), + m.Rackid, + pointer.SafeDeref(pointer.SafeDeref(m.Size).ID), + pointer.SafeDeref(pointer.SafeDeref(m.Allocation).Hostname), + pointer.SafeDeref(pointer.SafeDeref(m.Ipmi).Fru).ProductSerial, + }) + } else { + rows = append(rows, []string{ + fmt.Sprintf("%s%s", prefix, pointer.SafeDeref(m.ID)), + pointer.SafeDeref(pointer.SafeDeref(conn.Nic).Name), + identifier, + pointer.SafeDeref(pointer.SafeDeref(m.Partition).ID), + m.Rackid, + pointer.SafeDeref(pointer.SafeDeref(m.Size).ID), + pointer.SafeDeref(pointer.SafeDeref(m.Ipmi).Fru).ProductSerial, + }) + } } }