Skip to content

Commit

Permalink
Add API for requesting a VNC console (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
minus7 authored May 13, 2020
1 parent b520d9f commit f401905
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add `Status` field to `Volume`
* Add subnet type `cloud`
* Add `WithHTTPClient` option to specify a custom `http.Client`
* Add API for requesting a VNC console

## v1.17.0

Expand Down
12 changes: 12 additions & 0 deletions hcloud/schema/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ type ServerActionChangeProtectionResponse struct {
Action Action `json:"action"`
}

// ServerActionRequestConsoleRequest defines the schema of the request to
// request a WebSocket VNC console.
type ServerActionRequestConsoleRequest struct{}

// ServerActionRequestConsoleResponse defines the schema of the response when
// requesting a WebSocket VNC console.
type ServerActionRequestConsoleResponse struct {
Action Action `json:"action"`
WSSURL string `json:"wss_url"`
Password string `json:"password"`
}

// ServerActionAttachToNetworkRequest defines the schema for the request to
// attach a network to a server.
type ServerActionAttachToNetworkRequest struct {
Expand Down
27 changes: 27 additions & 0 deletions hcloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,33 @@ func (c *ServerClient) ChangeProtection(ctx context.Context, server *Server, opt
return ActionFromSchema(respBody.Action), resp, err
}

// ServerRequestConsoleResult is the result of requesting a WebSocket VNC console.
type ServerRequestConsoleResult struct {
Action *Action
WSSURL string
Password string
}

// RequestConsole requests a WebSocket VNC console.
func (c *ServerClient) RequestConsole(ctx context.Context, server *Server) (ServerRequestConsoleResult, *Response, error) {
path := fmt.Sprintf("/servers/%d/actions/request_console", server.ID)
req, err := c.client.NewRequest(ctx, "POST", path, nil)
if err != nil {
return ServerRequestConsoleResult{}, nil, err
}

respBody := schema.ServerActionRequestConsoleResponse{}
resp, err := c.client.Do(req, &respBody)
if err != nil {
return ServerRequestConsoleResult{}, resp, err
}
return ServerRequestConsoleResult{
Action: ActionFromSchema(respBody.Action),
WSSURL: respBody.WSSURL,
Password: respBody.Password,
}, resp, nil
}

// ServerAttachToNetworkOpts specifies options for attaching a server to a network.
type ServerAttachToNetworkOpts struct {
Network *Network
Expand Down
30 changes: 30 additions & 0 deletions hcloud/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,36 @@ func TestServerClientChangeProtection(t *testing.T) {
})
}

func TestServerClientRequestConsole(t *testing.T) {
env := newTestEnv()
defer env.Teardown()

env.Mux.HandleFunc("/servers/1/actions/request_console", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(schema.ServerActionRequestConsoleResponse{
Action: schema.Action{
ID: 1,
},
WSSURL: "wss://console.hetzner.cloud/?server_id=1&token=3db32d15-af2f-459c-8bf8-dee1fd05f49c",
Password: "9MQaTg2VAGI0FIpc10k3UpRXcHj2wQ6x",
})
})

ctx := context.Background()
result, _, err := env.Client.Server.RequestConsole(ctx, &Server{ID: 1})
if err != nil {
t.Fatal(err)
}
if result.Action.ID != 1 {
t.Errorf("unexpected action ID: %d", result.Action.ID)
}
if result.WSSURL != "wss://console.hetzner.cloud/?server_id=1&token=3db32d15-af2f-459c-8bf8-dee1fd05f49c" {
t.Errorf("unexpected WebSocket URL: %v", result.WSSURL)
}
if result.Password != "9MQaTg2VAGI0FIpc10k3UpRXcHj2wQ6x" {
t.Errorf("unexpected password: %v", result.Password)
}
}

func TestServerClientAttachToNetwork(t *testing.T) {
var (
ctx = context.Background()
Expand Down

0 comments on commit f401905

Please sign in to comment.