Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 17 additions & 20 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,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 @@ -498,33 +498,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 @@ -428,10 +430,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 @@ -441,14 +443,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 @@ -466,10 +465,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
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ github.com/go-openapi/loads v0.24.0 h1:4LLorXRPTzIN9V6ngMUZbAscsBOUBk3Oa8cClu/bF
github.com/go-openapi/loads v0.24.0/go.mod h1:xQMgX+hw5xRAhGrcDXxeMw78IFqUpIzhleu3HqPhyF4=
github.com/go-openapi/runtime v0.32.4 h1:8ElGj/3goG0itt0nBPP6Cm57ehcYyuHoI3O20nxgvkw=
github.com/go-openapi/runtime v0.32.4/go.mod h1:Bz6keOZw1NX4T6f+m42OoT1MBPDt6Re13dbccHyGH/4=
github.com/go-openapi/runtime/server-middleware v0.30.0 h1:8rPoJ/xv7JL8BsovaqboKETlpWBArVh8n+0L/GyePog=
github.com/go-openapi/runtime/server-middleware v0.30.0/go.mod h1:OYNT/TxNvB/VK5oe4htM2jDTwlEXuejVJmu0DVZfAMs=
github.com/go-openapi/runtime/server-middleware v0.32.4 h1:AU6eLMq9CXwh8f6kC1pivtkz+7lfo3TmakMBbUisKME=
github.com/go-openapi/runtime/server-middleware v0.32.4/go.mod h1:fYPep4GdTwg/XqZUjR40uIM/8C12Ba5M+MrGCiwpTHo=
github.com/go-openapi/spec v0.22.6 h1:Tyy1pLaNCM8GBCFLoGYLonjJi6zykqyLCjXLc19ZPic=
Expand Down Expand Up @@ -245,12 +243,10 @@ github.com/lestrrat-go/dsig-secp256k1 v1.0.0 h1:JpDe4Aybfl0soBvoVwjqDbp+9S1Y2OM7
github.com/lestrrat-go/dsig-secp256k1 v1.0.0/go.mod h1:CxUgAhssb8FToqbL8NjSPoGQlnO4w3LG1P0qPWQm/NU=
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
github.com/lestrrat-go/httprc v1.0.6 h1:qgmgIRhpvBqexMJjA/PmwSvhNk679oqD1RbovdCGW8k=
github.com/lestrrat-go/httprc/v3 v3.0.6 h1:4FpLQ18KK/ypPbVU3NLWJNRvH3kcYiqKqWfKGqNWxxI=
github.com/lestrrat-go/httprc/v3 v3.0.6/go.mod h1:mSMtkZW92Z98M5YoNNztbRGxbXHql7tSitCvaxvo9l0=
github.com/lestrrat-go/jwx/v3 v3.1.1 h1:yd9AdPmZ4INnQ7k42IrzXYpnEG803+SrQ6hdMvzHJzw=
github.com/lestrrat-go/jwx/v3 v3.1.1/go.mod h1:uw/MN2M/Xiu4FhwcIwH11Zsh9JWx9SWzgALl7/uIEkU=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
github.com/lestrrat-go/option/v2 v2.0.0 h1:XxrcaJESE1fokHy3FpaQ/cXW8ZsIdWcdFzzLOcID3Ss=
github.com/lestrrat-go/option/v2 v2.0.0/go.mod h1:oSySsmzMoR0iRzCDCaUfsCzxQHUEuhOViQObyy7S6Vg=
github.com/mattn/go-colorable v0.1.15 h1:+u9SLTRGnXv73cEsnsmoZBom+dMU88B2M0aDcWy0/jY=
Expand All @@ -267,8 +263,6 @@ 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=
Expand All @@ -287,7 +281,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid/v2 v2.1.1 h1:suPZ4ARWLOJLegGFiZZ1dFAkqzhMjL3J1TzI+5wHz8s=
github.com/oklog/ulid/v2 v2.1.1/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 h1:zrbMGy9YXpIeTnGj4EljqMiZsIcE09mmF8XsD5AYOJc=
Expand Down