Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Support VNC API call, to get VNC URL. #431

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions openstack/compute/v2/servers/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ func HandleRebootSuccessfully(t *testing.T) {
})
}

// HandleVncSuccessfully sets up the test server to respond to a VNC request with success.
func HandleVncSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestJSONRequest(t, r, `{ "os-getVNCConsole": { "type": "novnc" } }`)

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(`{ "console": { "type": "novnc", "url": "http://example.com/vnc_auto.html?token=f9906a48-b71e-4f18-baca-c987da3ebdb3" } }`))
})
}

// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
func HandleRebuildSuccessfully(t *testing.T, response string) {
th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
Expand Down
33 changes: 33 additions & 0 deletions openstack/compute/v2/servers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,39 @@ func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) Acti
return res
}

// VncType indicates what kind of VNC connection to request.
type VncType string

// These constants determine what kind of VNC connection to request in Vnc()
const (
NoVnc VncType = "novnc"
XvpVnc = "xvpvnc"
)

// Vnc returns the VNC URL for the given VncType.
func Vnc(client *gophercloud.ServiceClient, id string, t VncType) VncResult {
var res VncResult

if id == "" {
res.Err = fmt.Errorf("ID is required")
return res
} else if t == "" {
res.Err = fmt.Errorf("vnc type is required")
return res
}

reqBody := struct {
C map[string]string `json:"os-getVNCConsole"`
}{
map[string]string{"type": string(t)},
}

_, res.Err = client.Post(actionURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return res
}

// RebuildOptsBuilder is an interface that allows extensions to override the
// default behaviour of rebuild options
type RebuildOptsBuilder interface {
Expand Down
11 changes: 11 additions & 0 deletions openstack/compute/v2/servers/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ func TestRebootServer(t *testing.T) {
th.AssertNoErr(t, res.Err)
}

func TestVncServer(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleVncSuccessfully(t)

res := Vnc(client.ServiceClient(), "1234asdf", NoVnc)
th.AssertNoErr(t, res.Err)
url, _ := res.Extract()
th.AssertEquals(t, "http://example.com/vnc_auto.html?token=f9906a48-b71e-4f18-baca-c987da3ebdb3", url)
}

func TestRebuildServer(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
Expand Down
25 changes: 25 additions & 0 deletions openstack/compute/v2/servers/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ type ActionResult struct {
gophercloud.ErrResult
}

// VncResult represents the result of a server VNC request
type VncResult struct {
ActionResult
}

// VncConsole represents VNC call response.
type VncConsole struct {
Type string `mapstructure:"type"`
Url string `mapstructure:"url"`
}

// Extract interprets VncResult as a VNC URL if possible.
func (r VncResult) Extract() (string, error) {
if r.Err != nil {
return "", r.Err
}

var response struct {
Console VncConsole `mapstructure:"console"`
}

err := mapstructure.Decode(r.Body, &response)
return response.Console.Url, err
}

// RescueResult represents the result of a server rescue operation
type RescueResult struct {
ActionResult
Expand Down