github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_vpn_gateway_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  
    11  	"google.golang.org/api/compute/v1"
    12  )
    13  
    14  func TestAccComputeVpnGateway_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckComputeVpnGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccComputeVpnGateway_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckComputeVpnGatewayExists(
    24  						"google_compute_vpn_gateway.foobar"),
    25  					testAccCheckComputeVpnGatewayExists(
    26  						"google_compute_vpn_gateway.baz"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckComputeVpnGatewayDestroy(s *terraform.State) error {
    34  	config := testAccProvider.Meta().(*Config)
    35  	project := config.Project
    36  
    37  	vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "google_compute_network" {
    41  			continue
    42  		}
    43  
    44  		region := rs.Primary.Attributes["region"]
    45  		name := rs.Primary.Attributes["name"]
    46  
    47  		_, err := vpnGatewaysService.Get(project, region, name).Do()
    48  
    49  		if err == nil {
    50  			return fmt.Errorf("Error, VPN Gateway %s in region %s still exists",
    51  				name, region)
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckComputeVpnGatewayExists(n string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", n)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  
    69  		config := testAccProvider.Meta().(*Config)
    70  		name := rs.Primary.Attributes["name"]
    71  		region := rs.Primary.Attributes["region"]
    72  		project := config.Project
    73  
    74  		vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
    75  		_, err := vpnGatewaysService.Get(project, region, name).Do()
    76  
    77  		if err != nil {
    78  			return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
    79  		}
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  var testAccComputeVpnGateway_basic = fmt.Sprintf(`
    86  resource "google_compute_network" "foobar" {
    87  	name = "gateway-test-%s"
    88  	ipv4_range = "10.0.0.0/16"
    89  }
    90  resource "google_compute_vpn_gateway" "foobar" {
    91  	name = "gateway-test-%s"
    92  	network = "${google_compute_network.foobar.self_link}"
    93  	region = "us-central1"
    94  }
    95  resource "google_compute_vpn_gateway" "baz" {
    96  	name = "gateway-test-%s"
    97  	network = "${google_compute_network.foobar.name}"
    98  	region = "us-central1"
    99  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))