From e2bb3673094d06c0dcc76a998633994427ea19f6 Mon Sep 17 00:00:00 2001 From: minus Date: Mon, 11 May 2020 22:31:19 +0200 Subject: [PATCH 1/2] Add command to request a VNC console --- cli/server.go | 1 + cli/server_request_console.go | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 cli/server_request_console.go diff --git a/cli/server.go b/cli/server.go index cec9ec6e..6b3891a4 100644 --- a/cli/server.go +++ b/cli/server.go @@ -42,6 +42,7 @@ func newServerCommand(cli *CLI) *cobra.Command { newServerDetachFromNetworkCommand(cli), newServerChangeAliasIPsCommand(cli), newServerIPCommand(cli), + newServerRequestConsoleCommand(cli), ) return cmd } diff --git a/cli/server_request_console.go b/cli/server_request_console.go new file mode 100644 index 00000000..79820214 --- /dev/null +++ b/cli/server_request_console.go @@ -0,0 +1,45 @@ +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), + } + return cmd +} + +func runServerRequestConsole(cli *CLI, cmd *cobra.Command, args []string) error { + 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 + } + + 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 +} From 0213d4ff77c8b7ac06f8ded25d2ae4e7b007dd3c Mon Sep 17 00:00:00 2001 From: minus Date: Thu, 9 Jul 2020 19:48:32 +0200 Subject: [PATCH 2/2] Add JSON output to request-console command --- cli/server_request_console.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cli/server_request_console.go b/cli/server_request_console.go index 79820214..dd976a4b 100644 --- a/cli/server_request_console.go +++ b/cli/server_request_console.go @@ -16,10 +16,12 @@ func newServerRequestConsoleCommand(cli *CLI) *cobra.Command { 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 { @@ -38,6 +40,16 @@ func runServerRequestConsole(cli *CLI, cmd *cobra.Command, args []string) error 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)