diff --git a/digitalocean/droplet/datasource_droplet.go b/digitalocean/droplet/datasource_droplet.go index 3023b07ab..1391932c6 100644 --- a/digitalocean/droplet/datasource_droplet.go +++ b/digitalocean/droplet/datasource_droplet.go @@ -24,6 +24,12 @@ func DataSourceDigitalOceanDroplet() *schema.Resource { recordSchema["id"].Optional = true recordSchema["name"].ExactlyOneOf = []string{"id", "tag", "name"} recordSchema["name"].Optional = true + recordSchema["gpu"] = &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: false, + ConflictsWith: []string{"tag"}, + } recordSchema["tag"] = &schema.Schema{ Type: schema.TypeString, @@ -64,7 +70,13 @@ func dataSourceDigitalOceanDropletRead(ctx context.Context, d *schema.ResourceDa foundDroplet = *droplet } else if v, ok := d.GetOk("name"); ok { - dropletList, err := getDigitalOceanDroplets(meta, nil) + gpus := d.Get("gpu").(bool) + extra := make(map[string]interface{}) + if gpus { + extra["gpus"] = true + } + + dropletList, err := getDigitalOceanDroplets(meta, extra) if err != nil { return diag.FromErr(err) } diff --git a/digitalocean/droplet/datasource_droplet_test.go b/digitalocean/droplet/datasource_droplet_test.go index 6c6779cb3..d929134cf 100644 --- a/digitalocean/droplet/datasource_droplet_test.go +++ b/digitalocean/droplet/datasource_droplet_test.go @@ -3,12 +3,14 @@ package droplet_test import ( "context" "fmt" + "os" "strconv" "testing" "github.com/digitalocean/godo" "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance" "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) @@ -52,6 +54,52 @@ data "digitalocean_droplet" "foobar" { }) } +func TestAccDataSourceDigitalOceanDroplet_GPUByName(t *testing.T) { + runGPU := os.Getenv(runGPUEnvVar) + 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) + } + + var droplet godo.Droplet + name := acceptance.RandomTestName() + resourceConfig := testAccCheckDataSourceDigitalOceanDropletConfig_gpuByName(keyName, publicKeyMaterial, name) + dataSourceConfig := ` +data "digitalocean_droplet" "foobar" { + name = digitalocean_droplet.foo.name + gpu = true +}` + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: resourceConfig, + }, + { + Config: resourceConfig + dataSourceConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckDataSourceDigitalOceanDropletExists("data.digitalocean_droplet.foobar", &droplet), + resource.TestCheckResourceAttr( + "data.digitalocean_droplet.foobar", "name", name), + resource.TestCheckResourceAttr( + "data.digitalocean_droplet.foobar", "image", gpuImage), + resource.TestCheckResourceAttr( + "data.digitalocean_droplet.foobar", "region", "tor1"), + resource.TestCheckResourceAttrSet("data.digitalocean_droplet.foobar", "urn"), + resource.TestCheckResourceAttrSet("data.digitalocean_droplet.foobar", "created_at"), + ), + }, + }, + }) +} + func TestAccDataSourceDigitalOceanDroplet_BasicById(t *testing.T) { var droplet godo.Droplet name := acceptance.RandomTestName() @@ -173,6 +221,22 @@ resource "digitalocean_droplet" "foo" { }`, acceptance.RandomTestName(), name, defaultSize, defaultImage) } +func testAccCheckDataSourceDigitalOceanDropletConfig_gpuByName(keyName, key, name string) string { + return fmt.Sprintf(` +resource "digitalocean_ssh_key" "foobar" { + name = "%s" + public_key = "%s" +} + +resource "digitalocean_droplet" "foo" { + name = "%s" + size = "%s" + image = "%s" + region = "tor1" + ssh_keys = [digitalocean_ssh_key.foobar.id] +}`, keyName, key, name, gpuSize, gpuImage) +} + func testAccCheckDataSourceDigitalOceanDropletConfig_basicById(name string) string { return fmt.Sprintf(` resource "digitalocean_droplet" "foo" { diff --git a/digitalocean/droplet/datasource_droplets_test.go b/digitalocean/droplet/datasource_droplets_test.go index 4582dd05e..936050bcb 100644 --- a/digitalocean/droplet/datasource_droplets_test.go +++ b/digitalocean/droplet/datasource_droplets_test.go @@ -61,7 +61,7 @@ data "digitalocean_droplets" "result" { } func TestAccDataSourceDigitalOceanDroplets_GPUDroplet(t *testing.T) { - runGPU := os.Getenv("DO_RUN_GPU_TESTS") + runGPU := os.Getenv(runGPUEnvVar) if runGPU == "" { t.Skip("'DO_RUN_GPU_TESTS' env var not set; Skipping tests that requires a GPU Droplet") } diff --git a/digitalocean/droplet/resource_droplet_test.go b/digitalocean/droplet/resource_droplet_test.go index 595bcc0b5..17910531a 100644 --- a/digitalocean/droplet/resource_droplet_test.go +++ b/digitalocean/droplet/resource_droplet_test.go @@ -19,8 +19,9 @@ const ( defaultSize = "s-1vcpu-1gb" defaultImage = "ubuntu-22-04-x64" - gpuSize = "gpu-h100x1-80gb" - gpuImage = "gpu-h100x1-base" + gpuSize = "gpu-h100x1-80gb" + gpuImage = "gpu-h100x1-base" + runGPUEnvVar = "DO_RUN_GPU_TESTS" ) func TestAccDigitalOceanDroplet_Basic(t *testing.T) {