Skip to content

Commit

Permalink
droplets: support listing GPU Droplets.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Oct 24, 2024
1 parent 82ca0f9 commit e99f22a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
12 changes: 12 additions & 0 deletions droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var errNoNetworks = errors.New("no networks have been defined")
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Droplets
type DropletsService interface {
List(context.Context, *ListOptions) ([]Droplet, *Response, error)
ListWithGPUs(context.Context, *ListOptions) ([]Droplet, *Response, error)
ListByName(context.Context, string, *ListOptions) ([]Droplet, *Response, error)
ListByTag(context.Context, string, *ListOptions) ([]Droplet, *Response, error)
Get(context.Context, int) (*Droplet, *Response, error)
Expand Down Expand Up @@ -321,6 +322,17 @@ func (s *DropletsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Dropl
return s.list(ctx, path)
}

// ListWithGPUs lists all Droplets with GPUs.
func (s *DropletsServiceOp) ListWithGPUs(ctx context.Context, opt *ListOptions) ([]Droplet, *Response, error) {
path := fmt.Sprintf("%s?type=gpus", dropletBasePath)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}

return s.list(ctx, path)
}

// ListByName lists all Droplets filtered by name returning only exact matches.
// It is case-insensitive
func (s *DropletsServiceOp) ListByName(ctx context.Context, name string, opt *ListOptions) ([]Droplet, *Response, error) {
Expand Down
92 changes: 92 additions & 0 deletions droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,98 @@ func TestDroplets_ListDroplets(t *testing.T) {
}
}

func TestDroplets_ListDropletsWithGPUs(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/droplets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
if r.URL.Query().Get("type") != "gpus" {
t.Errorf("Droplets.ListWithGPUs did not request with a type parameter")
}
fmt.Fprint(w, `{
"droplets": [
{
"id": 1,
"size": {
"gpu_info": {
"count": 1,
"vram": {
"amount": 8,
"unit": "gib"
},
"model": "nvidia_tesla_v100"
},
"disk_info": [
{
"type": "local",
"size": {
"amount": 200,
"unit": "gib"
}
},
{
"type": "scratch",
"size": {
"amount": 40960,
"unit": "gib"
}
}
]
}
}
],
"meta": {
"total": 1
}
}`)
})

droplets, resp, err := client.Droplets.ListWithGPUs(ctx, nil)
if err != nil {
t.Errorf("Droplets.List returned error: %v", err)
}

expectedDroplets := []Droplet{
{
ID: 1,
Size: &Size{
GPUInfo: &GPUInfo{
Count: 1,
VRAM: &VRAM{
Amount: 8,
Unit: "gib",
},
Model: "nvidia_tesla_v100",
},
DiskInfo: []DiskInfo{
{
Type: "local",
Size: &DiskSize{
Amount: 200,
Unit: "gib",
},
},
{
Type: "scratch",
Size: &DiskSize{
Amount: 40960,
Unit: "gib",
},
},
},
},
},
}
if !reflect.DeepEqual(droplets, expectedDroplets) {
t.Errorf("Droplets.List\nDroplets: got=%#v\nwant=%#v", droplets, expectedDroplets)
}
expectedMeta := &Meta{Total: 1}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Droplets.List\nMeta: got=%#v\nwant=%#v", resp.Meta, expectedMeta)
}
}

func TestDroplets_ListDropletsByTag(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit e99f22a

Please sign in to comment.