terraform-provider-greenhost/digitalocean/resource_digitalocean_dropl...

134 lines
3.3 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"log"
"strings"
"testing"
"github.com/digitalocean/godo"
Upgrade to terraform-plugin-sdk (#321) * Upgrade to terraform-plugin-sdk After following the [guide](https://www.terraform.io/docs/extend/plugin-sdk.html#using-tf-sdk-migrator) Check ``` ▶ GO111MODULE=on tf-sdk-migrator check Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. ``` Migrate ``` ▶ GO111MODULE=on tf-sdk-migrator migrate Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. Rewriting provider go.mod file... Rewriting SDK package imports... Running `go mod tidy`... Success! Provider is migrated to github.com/hashicorp/terraform-plugin-sdk v1.1.0. It looks like this provider vendors dependencies. Don't forget to run `go mod vendor`. Make sure to review all changes and run all tests. ``` * Fix build under go 1.13.x.
2019-10-22 21:44:03 +00:00
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
func init() {
resource.AddTestSweepers("digitalocean_droplet_snapshot", &resource.Sweeper{
Name: "digitalocean_droplet_snapshot",
F: testSweepDropletSnapshots,
Dependencies: []string{"digitalocean_droplet"},
})
}
func testSweepDropletSnapshots(region string) error {
meta, err := sharedConfigForRegion(region)
if err != nil {
return err
}
client := meta.(*CombinedConfig).godoClient()
2018-11-28 22:49:31 +00:00
opt := &godo.ListOptions{PerPage: 200}
snapshots, _, err := client.Snapshots.ListDroplet(context.Background(), opt)
if err != nil {
return err
}
for _, s := range snapshots {
if strings.HasPrefix(s.Name, "snapshot-") || strings.HasPrefix(s.Name, "snap-") {
log.Printf("Destroying Droplet Snapshot %s", s.Name)
if _, err := client.Snapshots.Delete(context.Background(), s.ID); err != nil {
return err
}
}
}
return nil
}
func TestAccDigitalOceanDropletSnapshot_Basic(t *testing.T) {
var snapshot godo.Snapshot
rInt1 := acctest.RandInt()
rInt2 := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanDropletSnapshotDestroy,
Steps: []resource.TestStep{
{
2018-11-08 08:01:38 +00:00
Config: fmt.Sprintf(testAccCheckDigitalOceanDropletSnapshotConfig_basic, rInt1, rInt2),
Check: resource.ComposeTestCheckFunc(
2018-11-08 08:01:38 +00:00
testAccCheckDigitalOceanDropletSnapshotExists("digitalocean_droplet_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr(
"digitalocean_droplet_snapshot.foobar", "name", fmt.Sprintf("snapshot-one-%d", rInt2)),
),
},
},
})
}
func testAccCheckDigitalOceanDropletSnapshotExists(n string, snapshot *godo.Snapshot) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*CombinedConfig).godoClient()
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Droplet Snapshot ID is set")
}
foundSnapshot, _, err := client.Snapshots.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
}
if foundSnapshot.ID != rs.Primary.ID {
return fmt.Errorf("Droplet Snapshot not found")
}
*snapshot = *foundSnapshot
return nil
}
}
func testAccCheckDigitalOceanDropletSnapshotDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*CombinedConfig).godoClient()
for _, rs := range s.RootModule().Resources {
if rs.Type != "digitalocean_droplet_snapshot" {
continue
}
// Try to find the snapshot
_, _, err := client.Snapshots.Get(context.Background(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("Droplet Snapshot still exists")
}
}
return nil
}
const testAccCheckDigitalOceanDropletSnapshotConfig_basic = `
resource "digitalocean_droplet" "foo" {
name = "foo-%d"
size = "512mb"
image = "centos-7-x64"
region = "nyc3"
user_data = "foobar"
}
2018-11-08 08:01:38 +00:00
resource "digitalocean_droplet_snapshot" "foobar" {
droplet_id = "${digitalocean_droplet.foo.id}"
name = "snapshot-one-%d"
}
`