github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_global_address_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"google.golang.org/api/compute/v1"
    11  )
    12  
    13  func TestAccComputeGlobalAddress_basic(t *testing.T) {
    14  	var addr compute.Address
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeGlobalAddressDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeGlobalAddress_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeGlobalAddressExists(
    25  						"google_compute_global_address.foobar", &addr),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckComputeGlobalAddressDestroy(s *terraform.State) error {
    33  	config := testAccProvider.Meta().(*Config)
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "google_compute_global_address" {
    37  			continue
    38  		}
    39  
    40  		_, err := config.clientCompute.GlobalAddresses.Get(
    41  			config.Project, rs.Primary.ID).Do()
    42  		if err == nil {
    43  			return fmt.Errorf("Address still exists")
    44  		}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func testAccCheckComputeGlobalAddressExists(n string, addr *compute.Address) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("No ID is set")
    59  		}
    60  
    61  		config := testAccProvider.Meta().(*Config)
    62  
    63  		found, err := config.clientCompute.GlobalAddresses.Get(
    64  			config.Project, rs.Primary.ID).Do()
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		if found.Name != rs.Primary.ID {
    70  			return fmt.Errorf("Addr not found")
    71  		}
    72  
    73  		*addr = *found
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  var testAccComputeGlobalAddress_basic = fmt.Sprintf(`
    80  resource "google_compute_global_address" "foobar" {
    81  	name = "address-test-%s"
    82  }`, acctest.RandString(10))