github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_target_pool_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccComputeTargetPool_basic(t *testing.T) {
    12  
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckComputeTargetPoolDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccComputeTargetPool_basic,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckComputeTargetPoolExists(
    22  						"google_compute_target_pool.foobar"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckComputeTargetPoolDestroy(s *terraform.State) error {
    30  	config := testAccProvider.Meta().(*Config)
    31  
    32  	for _, rs := range s.RootModule().Resources {
    33  		if rs.Type != "google_compute_target_pool" {
    34  			continue
    35  		}
    36  
    37  		_, err := config.clientCompute.TargetPools.Get(
    38  			config.Project, config.Region, rs.Primary.ID).Do()
    39  		if err == nil {
    40  			return fmt.Errorf("TargetPool still exists")
    41  		}
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func testAccCheckComputeTargetPoolExists(n string) resource.TestCheckFunc {
    48  	return func(s *terraform.State) error {
    49  		rs, ok := s.RootModule().Resources[n]
    50  		if !ok {
    51  			return fmt.Errorf("Not found: %s", n)
    52  		}
    53  
    54  		if rs.Primary.ID == "" {
    55  			return fmt.Errorf("No ID is set")
    56  		}
    57  
    58  		config := testAccProvider.Meta().(*Config)
    59  
    60  		found, err := config.clientCompute.TargetPools.Get(
    61  			config.Project, config.Region, rs.Primary.ID).Do()
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  		if found.Name != rs.Primary.ID {
    67  			return fmt.Errorf("TargetPool not found")
    68  		}
    69  
    70  		return nil
    71  	}
    72  }
    73  
    74  const testAccComputeTargetPool_basic = `
    75  resource "google_compute_target_pool" "foobar" {
    76  	description = "Resource created for Terraform acceptance testing"
    77  	instances = ["us-central1-a/foo", "us-central1-b/bar"]
    78  	name = "terraform-test"
    79  	session_affinity = "CLIENT_IP_PROTO"
    80  }`