Skip to content

Commit

Permalink
change of switch toggle command
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichSchreiner committed Mar 21, 2024
1 parent 9c22e73 commit a01faf9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
46 changes: 39 additions & 7 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ Operational steps to replace a switch:
ValidArgsFunction: c.comp.SwitchListCompletion,
}
togglePortCmd := &cobra.Command{
Use: "toggle <switchID> <portID> <desiredstate>",
Use: "toggle <switchID> <portID> [<desiredstate>]",
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)
},
Expand Down Expand Up @@ -397,16 +397,48 @@ telnet console-server 7008`)
}

func (c *switchCmd) togglePort(args []string) error {
if len(args) < 3 {
return fmt.Errorf("missing <switch-id> <port-id> <port-state>")
if len(args) < 2 {
return fmt.Errorf("missing <switch-id> <port-id> [<port-state>]")
}

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)
}
7 changes: 3 additions & 4 deletions docs/metalctl_switch_toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <switchID> <portID> [flags]
metalctl switch toggle <switchID> <portID> [<desiredstate>] [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
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit a01faf9

Please sign in to comment.