mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 00:56:43 +00:00
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
![]() |
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),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
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))
|