Skip to content

Commit

Permalink
Add wide table for switch connected-machines. (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Sep 14, 2023
1 parent 8c27c9a commit e83add0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
16 changes: 11 additions & 5 deletions cmd/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand Down Expand Up @@ -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"),
},
Expand All @@ -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(`
Expand Down
43 changes: 33 additions & 10 deletions cmd/tableprinters/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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,
})
}
}
}

Expand Down

0 comments on commit e83add0

Please sign in to comment.