2016-08-12 04:35:33 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeImage_basic(t *testing.T) {
|
|
|
|
var image compute.Image
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeImageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeImage_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeImageExists(
|
|
|
|
"google_compute_image.foobar", &image),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-01 11:32:47 +01:00
|
|
|
func TestAccComputeImage_basedondisk(t *testing.T) {
|
|
|
|
var image compute.Image
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeImageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeImage_basedondisk,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeImageExists(
|
|
|
|
"google_compute_image.foobar", &image),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-12 04:35:33 +02:00
|
|
|
func testAccCheckComputeImageDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_image" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.Images.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Image still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeImageExists(n string, image *compute.Image) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.Images.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Image not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*image = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccComputeImage_basic = fmt.Sprintf(`
|
|
|
|
resource "google_compute_image" "foobar" {
|
|
|
|
name = "image-test-%s"
|
|
|
|
raw_disk {
|
|
|
|
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
|
|
|
|
}
|
|
|
|
}`, acctest.RandString(10))
|
2016-11-01 11:32:47 +01:00
|
|
|
|
|
|
|
var testAccComputeImage_basedondisk = fmt.Sprintf(`
|
|
|
|
resource "google_compute_disk" "foobar" {
|
|
|
|
name = "disk-test-%s"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
image = "debian-8-jessie-v20160803"
|
|
|
|
}
|
|
|
|
resource "google_compute_image" "foobar" {
|
|
|
|
name = "image-test-%s"
|
|
|
|
source_disk = "${google_compute_disk.foobar.self_link}"
|
|
|
|
}`, acctest.RandString(10), acctest.RandString(10))
|