Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to request a VNC console #238

Merged
merged 2 commits into from
Jul 13, 2020
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
1 change: 1 addition & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func newServerCommand(cli *CLI) *cobra.Command {
newServerDetachFromNetworkCommand(cli),
newServerChangeAliasIPsCommand(cli),
newServerIPCommand(cli),
newServerRequestConsoleCommand(cli),
)
return cmd
}
Expand Down
57 changes: 57 additions & 0 deletions cli/server_request_console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"
)

func newServerRequestConsoleCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "request-console [FLAGS] SERVER",
Short: "Request a WebSocket VNC console for a server",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.ensureToken,
RunE: cli.wrap(runServerRequestConsole),
}
addOutputFlag(cmd, outputOptionJSON())
return cmd
}

func runServerRequestConsole(cli *CLI, cmd *cobra.Command, args []string) error {
outOpts := outputFlagsForCommand(cmd)
idOrName := args[0]
server, _, err := cli.Client().Server.Get(cli.Context, idOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", idOrName)
}

result, _, err := cli.Client().Server.RequestConsole(cli.Context, server)
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, result.Action); err != nil {
return err
}

if outOpts.IsSet("json") {
return describeJSON(struct {
WSSURL string
Password string
}{
WSSURL: result.WSSURL,
Password: result.Password,
})
}

fmt.Printf("Console for server %d:\n", server.ID)
fmt.Printf("WebSocket URL: %s\n", result.WSSURL)
fmt.Printf("VNC Password: %s\n", result.Password)
return nil
}