From a01faf984d75f9851f95b3ba61a6ec3fda11ade6 Mon Sep 17 00:00:00 2001 From: Ulrich Schreiner Date: Thu, 21 Mar 2024 13:32:52 +0100 Subject: [PATCH] change of switch toggle command --- cmd/switch.go | 46 ++++++++++++++++++++++++++++------ docs/metalctl_switch_toggle.md | 7 +++--- go.mod | 4 +-- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/cmd/switch.go b/cmd/switch.go index 5684007..8d3cb93 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -154,9 +154,9 @@ Operational steps to replace a switch: ValidArgsFunction: c.comp.SwitchListCompletion, } togglePortCmd := &cobra.Command{ - Use: "toggle ", + Use: "toggle []", Short: "toggles the given switch port up or down", - Long: "this sets the port status to the desired value (up or down) so you can reconnect/disconnect a machine to/from a switch port.", + Long: "this sets the port status to the desired value (up or down) so you can reconnect/disconnect a machine to/from a switch port. if no state is given it will toggle the current state.", RunE: func(cmd *cobra.Command, args []string) error { return w.togglePort(args) }, @@ -397,16 +397,48 @@ telnet console-server 7008`) } func (c *switchCmd) togglePort(args []string) error { - if len(args) < 3 { - return fmt.Errorf("missing ") + if len(args) < 2 { + return fmt.Errorf("missing []") } - id, portid, status := args[0], args[1], args[2] + id, portid, status := args[0], args[1], "" - _, err := c.client.SwitchOperations().ToggleSwitchPort(switch_operations.NewToggleSwitchPortParams().WithID(id).WithBody(&models.V1SwitchPortToggleRequest{ + if len(args) == 2 { + resp, err := c.client.SwitchOperations().FindSwitch(switch_operations.NewFindSwitchParams().WithID(args[0]), nil) + if err != nil { + return err + } + + for _, n := range resp.Payload.Nics { + if n != nil && *n.Name == args[1] { + if n.Actual == nil { + // no actual state is set, so we assume it is down and we want to set it to up + status = models.V1SwitchNicActualUP + } else { + if *n.Actual == models.V1SwitchNicActualUP { + status = models.V1SwitchNicActualDOWN + } + if *n.Actual == models.V1SwitchNicActualDOWN { + status = models.V1SwitchNicActualUP + } + } + break + } + } + if status == "" { + return fmt.Errorf("port %q not found on switch %q", portid, id) + } + } else { + status = strings.ToUpper(args[2]) + } + + resp, err := c.client.SwitchOperations().ToggleSwitchPort(switch_operations.NewToggleSwitchPortParams().WithID(id).WithBody(&models.V1SwitchPortToggleRequest{ Nic: &portid, Status: &status, }), nil) + if err != nil { + return err + } - return err + return c.describePrinter.Print(resp.Payload.Nics) } diff --git a/docs/metalctl_switch_toggle.md b/docs/metalctl_switch_toggle.md index 41e7a2d..5f2f74b 100644 --- a/docs/metalctl_switch_toggle.md +++ b/docs/metalctl_switch_toggle.md @@ -4,17 +4,16 @@ toggles the given switch port up or down ### Synopsis -this sets the port status to the desired value (up or down) so you can reconnect/disconnect a machine to/from a switch port. +this sets the port status to the desired value (up or down) so you can reconnect/disconnect a machine to/from a switch port. if no state is given it will toggle the current state. ``` -metalctl switch toggle [flags] +metalctl switch toggle [] [flags] ``` ### Options ``` - -h, --help help for toggle - --status string the desired port status (default "up") + -h, --help help for toggle ``` ### Options inherited from parent commands diff --git a/go.mod b/go.mod index f418564..fa93073 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/go-openapi/runtime v0.27.1 github.com/go-openapi/strfmt v0.22.0 github.com/google/go-cmp v0.6.0 - github.com/metal-stack/metal-go v0.28.0 + github.com/metal-stack/metal-go v0.28.1-0.20240319084850-bee1c98c3a30 github.com/metal-stack/metal-lib v0.14.4 github.com/metal-stack/updater v1.2.1 github.com/metal-stack/v v1.0.3 @@ -179,5 +179,3 @@ require ( sigs.k8s.io/yaml v1.4.0 // indirect tailscale.com v1.54.1 // indirect ) - -replace github.com/metal-stack/metal-go v0.28.0 => github.com/metal-stack/metal-go v0.28.1-0.20240319084850-bee1c98c3a30