Skip to content

Commit

Permalink
datasource_droplet: support finding GPU Droplets by name.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Oct 25, 2024
1 parent df7729b commit eddc4da
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
14 changes: 13 additions & 1 deletion digitalocean/droplet/datasource_droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
64 changes: 64 additions & 0 deletions digitalocean/droplet/datasource_droplet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/droplet/datasource_droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions digitalocean/droplet/resource_droplet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit eddc4da

Please sign in to comment.