Skip to content

Commit

Permalink
Merge branch 'civo:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sundaram2021 authored Oct 2, 2024
2 parents f763e35 + 7edb971 commit cffe841
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
39 changes: 39 additions & 0 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ type Instance struct {
ReservedIPID string `json:"reserved_ip_id,omitempty"`
ReservedIPName string `json:"reserved_ip_name,omitempty"`
ReservedIP string `json:"reserved_ip,omitempty"`
VolumeType string `json:"volume_type,omitempty"`
Subnets []Subnet `json:"subnets,omitempty"`
AttachedVolumes []AttachedVolume `json:"attached_volumes,omitempty"`
PlacementRule PlacementRule `json:"placement_rule,omitempty"`
}

//"cpu_cores":1,"ram_mb":2048,"disk_gb":25
Expand All @@ -63,6 +65,14 @@ type InstanceConsole struct {
URL string `json:"url"`
}

// InstanceVnc represents VNC information for an instances
type InstanceVnc struct {
URI string `json:"uri"`
Result string `json:"result"`
Name string `json:"name"`
Label string `json:"label"`
}

// PaginatedInstanceList returns a paginated list of Instance object
type PaginatedInstanceList struct {
Page int `json:"page"`
Expand Down Expand Up @@ -102,6 +112,20 @@ type InstanceConfig struct {
TagsList string `json:"tags"`
FirewallID string `json:"firewall_id"`
AttachedVolumes []AttachedVolume `json:"attached_volumes"`
PlacementRule PlacementRule `json:"placement_rule"`
}

// AffinityRule represents a affinity rule
type AffinityRule struct {
Type string `json:"type"`
Exclusive bool `json:"exclusive"`
Tags []string `json:"tags"`
}

// PlacementRule represents a placement rule
type PlacementRule struct {
AffinityRules []AffinityRule `json:"affinity_rules,omitempty"`
NodeSelector map[string]string `json:"node_selector,omitempty"`
}

// ListInstances returns a page of Instances owned by the calling API account
Expand Down Expand Up @@ -261,6 +285,21 @@ func (c *Client) UpdateInstance(i *Instance) (*SimpleResponse, error) {
return response, err
}

// GetInstanceVnc enables and gets the VNC information for an instance
func (c *Client) GetInstanceVnc(id string) (InstanceVnc, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/vnc", id), map[string]string{
"region": c.Region,
})
vnc := InstanceVnc{}

if err != nil {
return vnc, decodeError(err)
}

err = json.NewDecoder(bytes.NewReader(resp)).Decode(&vnc)
return vnc, err
}

// DeleteInstance deletes an instance and frees its resources
func (c *Client) DeleteInstance(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest("/v2/instances/" + id)
Expand Down
1 change: 1 addition & 0 deletions volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Volume struct {
NetworkID string `json:"network_id"`
MountPoint string `json:"mountpoint"`
Status string `json:"status"`
VolumeType string `json:"volume_type"`
SizeGigabytes int `json:"size_gb"`
Bootable bool `json:"bootable"`
CreatedAt time.Time `json:"created_at"`
Expand Down
2 changes: 1 addition & 1 deletion volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestListVolumes(t *testing.T) {
"openstack_id": "null",
"size_gb": 25,
"bootable": false
}]`,
}]`,
})
defer server.Close()
got, err := client.ListVolumes()
Expand Down
31 changes: 31 additions & 0 deletions volumetype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package civogo

import (
"encoding/json"
"fmt"
)

// VolumeType represent the storage class related to a volume
// https://www.civo.com/api/volumes
type VolumeType struct {
Name string `json:"name"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
Labels []string `json:"labels"`
}

// ListVolumeTypes returns a page of Instances owned by the calling API account
func (c *Client) ListVolumeTypes() ([]VolumeType, error) {
resp, err := c.SendGetRequest("/v2/volumetypes")
if err != nil {
return nil, err
}

volumeTypes := make([]VolumeType, 0)
fmt.Println(string(resp))
if err := json.Unmarshal(resp, &volumeTypes); err != nil {
return nil, err
}

return volumeTypes, nil
}
35 changes: 35 additions & 0 deletions volumetype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package civogo

import (
"reflect"
"testing"
)

func TestListVolumeTypes(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/volumetypes": `[{
"name": "my-volume-type",
"description": "a volume type",
"enabled": true,
"labels": ["label"]
}]`,
})
defer server.Close()

got, err := client.ListVolumeTypes()
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := []VolumeType{{
Name: "my-volume-type",
Description: "a volume type",
Enabled: true,
Labels: []string{"label"},
}}

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

0 comments on commit cffe841

Please sign in to comment.