Skip to content

Commit 87a6b41

Browse files
authored
Merge pull request #263 from Faizan-Alam-1/master
feat:implement GetInstanceVncStatus and DeleteInstanceVncSession funtion
2 parents bd68579 + a7f6a34 commit 87a6b41

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

instance.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ type InstanceConsole struct {
6969

7070
// InstanceVnc represents VNC information for an instances
7171
type InstanceVnc struct {
72-
URI string `json:"uri"`
73-
Result string `json:"result"`
74-
Name string `json:"name"`
75-
Label string `json:"label"`
72+
URI string `json:"uri,omitempty"`
73+
Result string `json:"result,omitempty"`
74+
Name string `json:"name,omitempty"`
75+
Label string `json:"label,omitempty"`
76+
Expiration string `json:"expiration,omitempty"`
7677
}
7778

7879
// PaginatedInstanceList returns a paginated list of Instance object
@@ -309,6 +310,30 @@ func (c *Client) GetInstanceVnc(id string, duration ...string) (InstanceVnc, err
309310
return vnc, err
310311
}
311312

313+
// GetInstanceVncStatus returns the VNC status for an instance
314+
func (c *Client) GetInstanceVncStatus(id string) (*InstanceVnc, error) {
315+
url := fmt.Sprintf("/v2/instances/%s/vnc", id)
316+
resp, err := c.SendGetRequest(url)
317+
if err != nil {
318+
return nil, decodeError(err)
319+
}
320+
321+
vnc := InstanceVnc{}
322+
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&vnc)
323+
return &vnc, err
324+
325+
}
326+
327+
// DeleteInstanceVncSession terminates the VNC session for an instance.
328+
func (c *Client) DeleteInstanceVncSession(id string) (*SimpleResponse, error) {
329+
url := fmt.Sprintf("/v2/instances/%s/vnc", id)
330+
resp, err := c.SendDeleteRequest(url)
331+
if err != nil {
332+
return nil, decodeError(err)
333+
}
334+
return c.DecodeSimpleResponse(resp)
335+
}
336+
312337
// DeleteInstance deletes an instance and frees its resources
313338
func (c *Client) DeleteInstance(id string) (*SimpleResponse, error) {
314339
resp, err := c.SendDeleteRequest("/v2/instances/" + id)

instance_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ func TestDeleteInstance(t *testing.T) {
260260
EnsureSuccessfulSimpleResponse(t, got, err)
261261
}
262262

263+
func TestDeleteInstanceVncSession(t *testing.T) {
264+
client, server, _ := NewAdvancedClientForTesting([]ConfigAdvanceClientForTesting{
265+
{
266+
Method: "DELETE",
267+
Value: []ValueAdvanceClientForTesting{
268+
{
269+
RequestBody: "",
270+
URL: "/v2/instances/12345/vnc",
271+
ResponseBody: `{"result": "ok"}`,
272+
},
273+
},
274+
},
275+
})
276+
defer server.Close()
277+
278+
got, err := client.DeleteInstanceVncSession("12345")
279+
EnsureSuccessfulSimpleResponse(t, got, err)
280+
}
281+
263282
func TestRebootInstance(t *testing.T) {
264283
client, server, _ := NewAdvancedClientForTesting([]ConfigAdvanceClientForTesting{
265284
{
@@ -393,6 +412,36 @@ func TestMovePublicIPToInstance(t *testing.T) {
393412
EnsureSuccessfulSimpleResponse(t, got, err)
394413
}
395414

415+
func TestGetInstanceVncStatus(t *testing.T) {
416+
client, server, _ := NewAdvancedClientForTesting([]ConfigAdvanceClientForTesting{
417+
{
418+
Method: "GET",
419+
Value: []ValueAdvanceClientForTesting{
420+
{
421+
RequestBody: `""`,
422+
URL: "/v2/instances/12345/vnc",
423+
ResponseBody: `{"uri": "https://vnc.example.com/12345", "expiration": "2025-06-02T12:00:00Z"}`,
424+
},
425+
},
426+
},
427+
})
428+
defer server.Close()
429+
430+
got, err := client.GetInstanceVncStatus("12345")
431+
432+
if got.URI != "https://vnc.example.com/12345" {
433+
t.Errorf("Expected URI %s, got %s", "https://vnc.example.com/12345", got.URI)
434+
}
435+
436+
if got.Expiration != "2025-06-02T12:00:00Z" {
437+
t.Errorf("Expected Expiration %s, got %s", "2025-06-02T12:00:00Z", got.Expiration)
438+
}
439+
440+
if err != nil {
441+
t.Errorf("Request returned an error: %s", err)
442+
}
443+
}
444+
396445
func TestGetInstanceConsoleURL(t *testing.T) {
397446
client, server, _ := NewAdvancedClientForTesting([]ConfigAdvanceClientForTesting{
398447
{

0 commit comments

Comments
 (0)