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 fetch instance vnc details api in civigo library #213

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ type InstanceConfig struct {
AttachedVolume []AttachedVolume `json:"attached_volume"`
}

// Instance VNC details
type InstanceVNCDetails struct {
URI string `json:"uri"`
Result string `json:"result"`
Name string `json:"name"`
Label string `json:"label"`
// Adding code and reason to the struct to handle the error response as well
Code string `json:"code"`
Reason string `json:"reason"`
}

// ListInstances returns a page of Instances owned by the calling API account
func (c *Client) ListInstances(page int, perPage int) (*PaginatedInstanceList, error) {
url := "/v2/instances"
Expand Down Expand Up @@ -380,3 +391,15 @@ func (c *Client) SetInstanceFirewall(id, firewallID string) (*SimpleResponse, er
response, err := c.DecodeSimpleResponse(resp)
return response, err
}

// GetInstanceVNCDetails gets the VNC details for an instance
func (c *Client) GetInstanceVNCDetails(id string) (*InstanceVNCDetails, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we name it like InitInstanceVNC?
Because it's a PUT and not really a get

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes more sense now.
I was intrigued by the naming as it felt like it's an API that gives you the VNC details. (A fetch API hence the naming as GET)
But I guess in backend, it initiates -> then gives back the result I guess.
I will make these changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the comment 👍

resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/vnc", id), "")
if err != nil {
return nil, decodeError(err)
}
Comment on lines +397 to +400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you tested it locally without mocking the API?
I don't think it will work rather it would throw that instance not found in this region error i assume


instance := InstanceVNCDetails{}
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&instance)
return &instance, err
}
68 changes: 68 additions & 0 deletions instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,71 @@ func TestSetInstanceFirewall(t *testing.T) {
got, err := client.SetInstanceFirewall("12345", "67890")
EnsureSuccessfulSimpleResponse(t, got, err)
}

func TestGetInstanceVNCDetailsSuccessful(t *testing.T) {
client, server, _ := NewAdvancedClientForTesting([]ConfigAdvanceClientForTesting{
{
Method: "PUT",
Value: []ValueAdvanceClientForTesting{
{
RequestBody: `""`,
URL: "/v2/instances/12345/vnc",
ResponseBody: `{"uri": "https://vnc.example.com/12345", "result": "success", "name": "vnc.randomnumber.nyc1", "label": "VNC Access details"}`,
},
},
},
})
defer server.Close()

got, _ := client.GetInstanceVNCDetails("12345")

tests := []struct {
field, expected, actual string
}{
{"URI", "https://vnc.example.com/12345", got.URI},
{"Name", "vnc.randomnumber.nyc1", got.Name},
{"Label", "VNC Access details", got.Label},
{"Result", "success", got.Result},
}

for _, tt := range tests {
if tt.expected != tt.actual {
t.Errorf("Expected %s: %s, got %s", tt.field, tt.expected, tt.actual)
}
}
}

func TestGetInstanceVNCDetailsInvalid(t *testing.T) {
client, server, _ := NewAdvancedClientForTesting([]ConfigAdvanceClientForTesting{
{
Method: "PUT",
Value: []ValueAdvanceClientForTesting{
{
RequestBody: `""`,
URL: "/v2/instances/invalidid/vnc",
ResponseBody: `{"code": "database_instance_not_found", "reason": "The requested instance doesn't exist"}`,
},
},
},
})
defer server.Close()

got, _ := client.GetInstanceVNCDetails("invalidid")

tests := []struct {
field, expected, actual string
}{
{"URI", "", got.URI},
{"Name", "", got.Name},
{"Label", "", got.Label},
{"Result", "", got.Result},
{"Code", "database_instance_not_found", got.Code},
{"Reason", "The requested instance doesn't exist", got.Reason},
}

for _, tt := range tests {
if tt.expected != tt.actual {
t.Errorf("Expected %s: %s, got %s", tt.field, tt.expected, tt.actual)
}
}
}