Skip to content

Commit

Permalink
datasource_droplets: support listing GPU Droplets.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Oct 25, 2024
1 parent f3c86d6 commit df7729b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
7 changes: 7 additions & 0 deletions digitalocean/droplet/datasource_droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ func DataSourceDigitalOceanDroplets() *schema.Resource {
ResultAttributeName: "droplets",
GetRecords: getDigitalOceanDroplets,
FlattenRecord: flattenDigitalOceanDroplet,
ExtraQuerySchema: map[string]*schema.Schema{
"gpus": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}

return datalist.NewResource(dataListConfig)
Expand Down
66 changes: 66 additions & 0 deletions digitalocean/droplet/datasource_droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package droplet_test

import (
"fmt"
"os"
"testing"

"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

Expand Down Expand Up @@ -57,3 +59,67 @@ data "digitalocean_droplets" "result" {
},
})
}

func TestAccDataSourceDigitalOceanDroplets_GPUDroplet(t *testing.T) {
runGPU := os.Getenv("DO_RUN_GPU_TESTS")
if runGPU == "" {
t.Skip("'DO_RUN_GPU_TESTS' env var not set; Skipping tests that requires a GPU Droplet")
}

keyName := acceptance.RandomTestName()
publicKeyMaterial, _, err := acctest.RandSSHKeyPair("digitalocean@ssh-acceptance-test")
if err != nil {
t.Fatalf("Cannot generate test SSH key pair: %s", err)
}

name1 := acceptance.RandomTestName("gpu")
name2 := acceptance.RandomTestName("regular")

resourcesConfig := fmt.Sprintf(`
resource "digitalocean_ssh_key" "foobar" {
name = "%s"
public_key = "%s"
}
resource "digitalocean_droplet" "gpu" {
name = "%s"
size = "%s"
image = "%s"
region = "nyc2"
ssh_keys = [digitalocean_ssh_key.foobar.id]
}
resource "digitalocean_droplet" "regular" {
name = "%s"
size = "%s"
image = "%s"
region = "nyc2"
}
`, keyName, publicKeyMaterial, name1, gpuSize, gpuImage, name2, defaultSize, defaultImage)

datasourceConfig := `
data "digitalocean_droplets" "result" {
gpus = true
}
`
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: resourcesConfig,
},
{
Config: resourcesConfig + datasourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.digitalocean_droplets.result", "droplets.#", "1"),
resource.TestCheckResourceAttr("data.digitalocean_droplets.result", "droplets.0.name", name1),
resource.TestCheckResourceAttrPair("data.digitalocean_droplets.result", "droplets.0.id", "digitalocean_droplet.gpu", "id"),
),
},
{
Config: resourcesConfig,
},
},
})
}
13 changes: 12 additions & 1 deletion digitalocean/droplet/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func dropletSchema() map[string]*schema.Schema {
func getDigitalOceanDroplets(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*config.CombinedConfig).GodoClient()

gpus, _ := extra["gpus"].(bool)

opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
Expand All @@ -126,7 +128,16 @@ func getDigitalOceanDroplets(meta interface{}, extra map[string]interface{}) ([]
var dropletList []interface{}

for {
droplets, resp, err := client.Droplets.List(context.Background(), opts)
var (
droplets []godo.Droplet
resp *godo.Response
err error
)
if gpus {
droplets, resp, err = client.Droplets.ListWithGPUs(context.Background(), opts)
} else {
droplets, resp, err = client.Droplets.List(context.Background(), opts)
}

if err != nil {
return nil, fmt.Errorf("Error retrieving droplets: %s", err)
Expand Down
3 changes: 3 additions & 0 deletions digitalocean/droplet/resource_droplet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
const (
defaultSize = "s-1vcpu-1gb"
defaultImage = "ubuntu-22-04-x64"

gpuSize = "gpu-h100x1-80gb"
gpuImage = "gpu-h100x1-base"
)

func TestAccDigitalOceanDroplet_Basic(t *testing.T) {
Expand Down

0 comments on commit df7729b

Please sign in to comment.