Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func switchResponseToUpdate(r *models.V1SwitchResponse) *models.V1SwitchUpdateRe
Name: r.Name,
Os: switchOS,
RackID: r.RackID,
RoomID: r.RoomID,
}
}

Expand Down Expand Up @@ -373,6 +374,7 @@ func (c *switchCmd) switchReplace(args []string) error {
Name: resp.Name,
Os: switchOS,
RackID: resp.RackID,
RoomID: resp.RoomID,
})
if err != nil {
return err
Expand Down Expand Up @@ -466,7 +468,7 @@ func (c *switchCmd) describePort(args []string) error {
if err != nil {
return err
}
return c.dumpPortState(resp.Payload, portid)
return c.printNicWithConnectedMachine(resp.Payload, portid)
}

func (c *switchCmd) togglePort(args []string, status string) error {
Expand All @@ -487,33 +489,30 @@ func (c *switchCmd) togglePort(args []string, status string) error {
if err != nil {
return err
}
return c.dumpPortState(resp.Payload, portid)
return c.printNicWithConnectedMachine(resp.Payload, portid)
}

func (c *switchCmd) dumpPortState(rsp *models.V1SwitchResponse, portid string) error {
var state currentSwitchPortStateDump
func (c *switchCmd) printNicWithConnectedMachine(s *models.V1SwitchResponse, portid string) error {
connection := models.V1SwitchConnection{}

for _, con := range rsp.Connections {
for _, con := range s.Connections {
if con == nil || con.Nic == nil || con.Nic.Name == nil {
continue
}
if *con.Nic.Name == portid {
state.Actual = *con
break
return c.describePrinter.Print(con)
}
}
for _, desired := range rsp.Nics {
if *desired.Name == portid {
state.Desired = *desired

for _, nic := range s.Nics {
if nic == nil || nic.Name == nil {
continue
}
if *nic.Name == portid {
connection.Nic = nic
break
}
}

if state.Actual.Nic == nil {
return fmt.Errorf("no machine connected to port %s on switch %s", portid, *rsp.ID)
}

return c.describePrinter.Print(state)
}

type currentSwitchPortStateDump struct {
Actual models.V1SwitchConnection `json:"actual" yaml:"actual"`
Desired models.V1SwitchNic `json:"desired" yaml:"desired"`
return c.describePrinter.Print(connection)
}
18 changes: 7 additions & 11 deletions cmd/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/metal-stack/metal-go/api/client/switch_operations"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-go/test/client"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/metal-stack/metal-lib/pkg/testcommon"
"github.com/metal-stack/metalctl/cmd/tableprinters"
"github.com/spf13/afero"
Expand Down Expand Up @@ -70,6 +71,7 @@ var (
},
ManagementIP: "1.2.3.4",
ManagementUser: "root",
RoomID: "room01",
}
switch2 = &models.V1SwitchResponse{
Connections: []*models.V1SwitchConnection{
Expand Down Expand Up @@ -427,10 +429,10 @@ func Test_SwitchCmd_ToggleResult(t *testing.T) {
require.NoError(t, err)
sw1Down.Nics[0].Actual = new("DOWN")

tests := []*test[currentSwitchPortStateDump]{
tests := []*test[models.V1SwitchConnection]{
{
name: "query state",
cmd: func(want currentSwitchPortStateDump) []string {
cmd: func(want models.V1SwitchConnection) []string {
return []string{"switch", "port", "describe", *switch1.ID, "--port", *switch1.Nics[0].Name}
},
mocks: &client.MetalMockFns{
Expand All @@ -440,14 +442,11 @@ func Test_SwitchCmd_ToggleResult(t *testing.T) {
}, nil)
},
},
want: currentSwitchPortStateDump{
Actual: *switch1.Connections[0],
Desired: *switch1.Nics[0],
},
want: pointer.SafeDeref(pointer.SafeDeref(switch1).Connections[0]),
},
{
name: "toggle down",
cmd: func(want currentSwitchPortStateDump) []string {
cmd: func(want models.V1SwitchConnection) []string {
return []string{"switch", "port", "down", *switch1.ID, "--port", *switch1.Nics[0].Name}
},
mocks: &client.MetalMockFns{
Expand All @@ -465,10 +464,7 @@ func Test_SwitchCmd_ToggleResult(t *testing.T) {
}, nil)
},
},
want: currentSwitchPortStateDump{
Actual: *sw1Down.Connections[0],
Desired: *sw1Down.Nics[0],
},
want: pointer.SafeDeref(sw1Down.Connections[0]),
},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-openapi/strfmt v0.26.4
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/metal-stack/metal-go v0.44.1
github.com/metal-stack/metal-go v0.45.0
github.com/metal-stack/metal-lib v0.25.2
github.com/metal-stack/updater v1.3.1
github.com/metal-stack/v v1.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ github.com/mdlayher/sdnotify v1.0.0 h1:Ma9XeLVN/l0qpyx1tNeMSeTjCPH6NtuD6/N9XdTlQ
github.com/mdlayher/sdnotify v1.0.0/go.mod h1:HQUmpM4XgYkhDLtd+Uad8ZFK1T9D5+pNxnXQjCeJlGE=
github.com/mdlayher/socket v0.6.1 h1:M7uj2NtuujUY4mYr1C57NmfNiRHbkKpnBxO856lsc3A=
github.com/mdlayher/socket v0.6.1/go.mod h1:+/SGtqc9V+5dAuRgQsU0fGBI+oRDiW7O2Obx10OIWfg=
github.com/metal-stack/metal-go v0.44.1 h1:tu6TMEsAccA9i/Ipte1RV9EcQ3APX4m9PdvvYCB10uQ=
github.com/metal-stack/metal-go v0.44.1/go.mod h1:GSfXrAj55LGsUSMHWGDsmq5n056NG0yb1JM8bgfvKOw=
github.com/metal-stack/metal-go v0.45.0 h1:X5BCppdE3RYNScfwQxv9JTehyFIv6LhVzhbG25k27KQ=
github.com/metal-stack/metal-go v0.45.0/go.mod h1:GSfXrAj55LGsUSMHWGDsmq5n056NG0yb1JM8bgfvKOw=
github.com/metal-stack/metal-lib v0.25.2 h1:OW8y6PtlV5lv3bXSDU1MaNGvtbroiDYjuhSxqKHKAgw=
github.com/metal-stack/metal-lib v0.25.2/go.mod h1:tnx4MM5oml10EMN6Nq6oFSbjOKB9B27WUZQUyZ4MywA=
github.com/metal-stack/security v0.9.6 h1:q+JbfFis/G2IuyKky2btmDmO0Y2GQ47p7lmzOi80kMY=
Expand Down