terraform-provider-greenhost/digitalocean/resource_digitalocean_volum...

185 lines
5.1 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"log"
"testing"
"strings"
"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_volume_snapshot", &resource.Sweeper{
Name: "digitalocean_volume_snapshot",
F: testSweepVolumeSnapshots,
Dependencies: []string{"digitalocean_volume"},
})
}
func testSweepVolumeSnapshots(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.ListVolume(context.Background(), opt)
if err != nil {
return err
}
for _, s := range snapshots {
if strings.HasPrefix(s.Name, "snapshot-") {
log.Printf("Destroying Volume Snapshot %s", s.Name)
if _, err := client.Snapshots.Delete(context.Background(), s.ID); err != nil {
return err
}
}
}
return nil
}
func TestAccDigitalOceanVolumeSnapshot_Basic(t *testing.T) {
var snapshot godo.Snapshot
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanVolumeSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeSnapshotConfig_basic, rInt, rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanVolumeSnapshotExists("digitalocean_volume_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr(
"digitalocean_volume_snapshot.foobar", "name", fmt.Sprintf("snapshot-%d", rInt)),
resource.TestCheckResourceAttr(
"digitalocean_volume_snapshot.foobar", "size", "0"),
resource.TestCheckResourceAttr(
"digitalocean_volume_snapshot.foobar", "regions.#", "1"),
resource.TestCheckResourceAttr(
"digitalocean_volume_snapshot.foobar", "min_disk_size", "100"),
resource.TestCheckResourceAttr(
"digitalocean_volume_snapshot.foobar", "tags.#", "2"),
resource.TestCheckResourceAttrSet(
"digitalocean_volume_snapshot.foobar", "volume_id"),
),
},
},
})
}
func testAccCheckDigitalOceanVolumeSnapshotExists(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 Volume 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("Volume Snapshot not found")
}
*snapshot = *foundSnapshot
return nil
}
}
func testAccCheckDigitalOceanVolumeSnapshotDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*CombinedConfig).godoClient()
for _, rs := range s.RootModule().Resources {
if rs.Type != "digitalocean_volume_snapshot" {
continue
}
// Try to find the snapshot
_, _, err := client.Snapshots.Get(context.Background(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("Volume Snapshot still exists")
}
}
return nil
}
const testAccCheckDigitalOceanVolumeSnapshotConfig_basic = `
resource "digitalocean_volume" "foo" {
region = "nyc1"
name = "volume-%d"
size = 100
description = "peace makes plenty"
}
resource "digitalocean_volume_snapshot" "foobar" {
name = "snapshot-%d"
volume_id = "${digitalocean_volume.foo.id}"
tags = ["foo","bar"]
}`
func TestAccDigitalOceanVolumeSnapshot_UpdateTags(t *testing.T) {
var snapshot godo.Snapshot
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanVolumeSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeSnapshotConfig_basic, rInt, rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanVolumeSnapshotExists("digitalocean_volume_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr("digitalocean_volume_snapshot.foobar", "tags.#", "2"),
),
},
{
Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeSnapshotConfig_basic_tag_update, rInt, rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanVolumeSnapshotExists("digitalocean_volume_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr("digitalocean_volume_snapshot.foobar", "tags.#", "3"),
),
},
},
})
}
const testAccCheckDigitalOceanVolumeSnapshotConfig_basic_tag_update = `
resource "digitalocean_volume" "foo" {
region = "nyc1"
name = "volume-%d"
size = 100
description = "peace makes plenty"
}
resource "digitalocean_volume_snapshot" "foobar" {
name = "snapshot-%d"
volume_id = "${digitalocean_volume.foo.id}"
tags = ["foo","bar","baz"]
}`