2014-08-26 01:23:28 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-06 23:00:09 +01:00
|
|
|
"os"
|
2014-08-26 01:23:28 +02:00
|
|
|
"testing"
|
|
|
|
|
2016-01-05 19:26:44 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2014-08-26 01:23:28 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
2014-08-26 01:23:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeDisk_basic(t *testing.T) {
|
2016-01-05 19:26:44 +01:00
|
|
|
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
2014-08-26 01:23:28 +02:00
|
|
|
var disk compute.Disk
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeDiskDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 19:26:44 +01:00
|
|
|
Config: testAccComputeDisk_basic(diskName),
|
2014-08-26 01:23:28 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeDiskExists(
|
|
|
|
"google_compute_disk.foobar", &disk),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:00:09 +01:00
|
|
|
func TestAccComputeDisk_from_snapshot_uri(t *testing.T) {
|
|
|
|
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
2017-05-22 23:36:53 +02:00
|
|
|
firstDiskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
snapshotName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
|
|
|
|
|
2017-03-06 23:00:09 +01:00
|
|
|
var disk compute.Disk
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeDiskDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-05-22 23:36:53 +02:00
|
|
|
Config: testAccComputeDisk_from_snapshot_uri(firstDiskName, snapshotName, diskName, xpn_host),
|
2017-03-06 23:00:09 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeDiskExists(
|
2017-05-22 23:36:53 +02:00
|
|
|
"google_compute_disk.seconddisk", &disk),
|
2017-03-06 23:00:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:49:48 +01:00
|
|
|
func TestAccComputeDisk_encryption(t *testing.T) {
|
|
|
|
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var disk compute.Disk
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeDiskDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeDisk_encryption(diskName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeDiskExists(
|
|
|
|
"google_compute_disk.foobar", &disk),
|
|
|
|
testAccCheckEncryptionKey(
|
|
|
|
"google_compute_disk.foobar", &disk),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:23:28 +02:00
|
|
|
func testAccCheckComputeDiskDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-08-26 01:23:28 +02:00
|
|
|
if rs.Type != "google_compute_disk" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.Disks.Get(
|
2014-09-17 02:16:53 +02:00
|
|
|
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
|
2014-08-26 01:23:28 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Disk still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:16:53 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-08-26 01:23:28 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-08-26 01:23:28 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.Disks.Get(
|
2014-09-17 02:16:53 +02:00
|
|
|
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
|
2014-08-26 01:23:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
if found.Name != rs.Primary.ID {
|
2014-08-26 01:23:28 +02:00
|
|
|
return fmt.Errorf("Disk not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*disk = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:49:48 +01:00
|
|
|
func testAccCheckEncryptionKey(n string, disk *compute.Disk) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := rs.Primary.Attributes["disk_encryption_key_sha256"]
|
|
|
|
if disk.DiskEncryptionKey == nil && attr != "" {
|
|
|
|
return fmt.Errorf("Disk %s has mismatched encryption key.\nTF State: %+v\nGCP State: <empty>", n, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr != disk.DiskEncryptionKey.Sha256 {
|
|
|
|
return fmt.Errorf("Disk %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v",
|
|
|
|
n, attr, disk.DiskEncryptionKey.Sha256)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:26:44 +01:00
|
|
|
func testAccComputeDisk_basic(diskName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2014-08-26 01:23:28 +02:00
|
|
|
resource "google_compute_disk" "foobar" {
|
2016-01-05 19:26:44 +01:00
|
|
|
name = "%s"
|
2016-08-19 04:31:45 +02:00
|
|
|
image = "debian-8-jessie-v20160803"
|
2014-08-26 01:23:28 +02:00
|
|
|
size = 50
|
2014-10-07 06:59:09 +02:00
|
|
|
type = "pd-ssd"
|
2014-08-26 01:23:28 +02:00
|
|
|
zone = "us-central1-a"
|
2016-01-05 19:26:44 +01:00
|
|
|
}`, diskName)
|
|
|
|
}
|
2017-01-18 14:49:48 +01:00
|
|
|
|
2017-05-22 23:36:53 +02:00
|
|
|
func testAccComputeDisk_from_snapshot_uri(firstDiskName string, snapshotName string, diskName string, xpn_host string) string {
|
2017-03-06 23:00:09 +01:00
|
|
|
return fmt.Sprintf(`
|
2017-05-22 23:36:53 +02:00
|
|
|
resource "google_compute_disk" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
image = "debian-8-jessie-v20160803"
|
|
|
|
size = 50
|
|
|
|
type = "pd-ssd"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
project = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_snapshot" "snapdisk" {
|
|
|
|
name = "%s"
|
|
|
|
source_disk = "${google_compute_disk.foobar.name}"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
project = "%s"
|
|
|
|
}
|
|
|
|
resource "google_compute_disk" "seconddisk" {
|
2017-03-06 23:00:09 +01:00
|
|
|
name = "%s"
|
2017-05-22 23:36:53 +02:00
|
|
|
snapshot = "${google_compute_snapshot.snapdisk.self_link}"
|
2017-03-06 23:00:09 +01:00
|
|
|
type = "pd-ssd"
|
|
|
|
zone = "us-central1-a"
|
2017-05-22 23:36:53 +02:00
|
|
|
}`, firstDiskName, xpn_host, snapshotName, xpn_host, diskName)
|
2017-03-06 23:00:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 14:49:48 +01:00
|
|
|
func testAccComputeDisk_encryption(diskName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_compute_disk" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
image = "debian-8-jessie-v20160803"
|
|
|
|
size = 50
|
|
|
|
type = "pd-ssd"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
|
|
|
|
}`, diskName)
|
|
|
|
}
|