diff --git a/digitalocean/droplet/datasource_droplets.go b/digitalocean/droplet/datasource_droplets.go index 152e5448e..386b7d3e7 100644 --- a/digitalocean/droplet/datasource_droplets.go +++ b/digitalocean/droplet/datasource_droplets.go @@ -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) diff --git a/digitalocean/droplet/datasource_droplets_test.go b/digitalocean/droplet/datasource_droplets_test.go index efba5f196..4582dd05e 100644 --- a/digitalocean/droplet/datasource_droplets_test.go +++ b/digitalocean/droplet/datasource_droplets_test.go @@ -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" ) @@ -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, + }, + }, + }) +} diff --git a/digitalocean/droplet/droplets.go b/digitalocean/droplet/droplets.go index cabe98616..b9252f221 100644 --- a/digitalocean/droplet/droplets.go +++ b/digitalocean/droplet/droplets.go @@ -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, @@ -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) diff --git a/digitalocean/droplet/resource_droplet_test.go b/digitalocean/droplet/resource_droplet_test.go index b55e3a355..595bcc0b5 100644 --- a/digitalocean/droplet/resource_droplet_test.go +++ b/digitalocean/droplet/resource_droplet_test.go @@ -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) {