github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_vpn_tunnel_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  	"google.golang.org/api/compute/v1"
    11  )
    12  
    13  func TestAccComputeVpnTunnel_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckComputeVpnTunnelDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccComputeVpnTunnel_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckComputeVpnTunnelExists(
    23  						"google_compute_vpn_tunnel.foobar"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckComputeVpnTunnelDestroy(s *terraform.State) error {
    31  	config := testAccProvider.Meta().(*Config)
    32  	project := config.Project
    33  
    34  	vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
    35  
    36  	for _, rs := range s.RootModule().Resources {
    37  		if rs.Type != "google_compute_network" {
    38  			continue
    39  		}
    40  
    41  		region := rs.Primary.Attributes["region"]
    42  		name := rs.Primary.Attributes["name"]
    43  
    44  		_, err := vpnTunnelsService.Get(project, region, name).Do()
    45  
    46  		if err == nil {
    47  			return fmt.Errorf("Error, VPN Tunnel %s in region %s still exists",
    48  				name, region)
    49  		}
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func testAccCheckComputeVpnTunnelExists(n string) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[n]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", n)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return fmt.Errorf("No ID is set")
    64  		}
    65  
    66  		config := testAccProvider.Meta().(*Config)
    67  		name := rs.Primary.Attributes["name"]
    68  		region := rs.Primary.Attributes["region"]
    69  		project := config.Project
    70  
    71  		vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
    72  		_, err := vpnTunnelsService.Get(project, region, name).Do()
    73  
    74  		if err != nil {
    75  			return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err)
    76  		}
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  const testAccComputeVpnTunnel_basic = `
    83  resource "google_compute_network" "foobar" {
    84  	name = "tf-test-network"
    85  	ipv4_range = "10.0.0.0/16"
    86  }
    87  resource "google_compute_address" "foobar" {
    88  	name = "tf-test-static-ip"
    89  	region = "us-central1"
    90  }
    91  resource "google_compute_vpn_gateway" "foobar" {
    92  	name = "tf-test-vpn-gateway"
    93  	network = "${google_compute_network.foobar.self_link}"
    94  	region = "${google_compute_address.foobar.region}"
    95  }
    96  resource "google_compute_forwarding_rule" "foobar_esp" {
    97  	name = "tf-test-fr-esp"
    98  	region = "${google_compute_vpn_gateway.foobar.region}"
    99  	ip_protocol = "ESP"
   100  	ip_address = "${google_compute_address.foobar.address}"
   101  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   102  }
   103  resource "google_compute_forwarding_rule" "foobar_udp500" {
   104  	name = "tf-test-fr-udp500"
   105  	region = "${google_compute_forwarding_rule.foobar_esp.region}"
   106  	ip_protocol = "UDP"
   107  	port_range = "500"
   108  	ip_address = "${google_compute_address.foobar.address}"
   109  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   110  }
   111  resource "google_compute_forwarding_rule" "foobar_udp4500" {
   112  	name = "tf-test-fr-udp4500"
   113  	region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   114  	ip_protocol = "UDP"
   115  	port_range = "4500"
   116  	ip_address = "${google_compute_address.foobar.address}"
   117  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   118  }
   119  resource "google_compute_vpn_tunnel" "foobar" {
   120  	name = "tf-test-vpn-tunnel"
   121  	region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
   122  	target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
   123  	shared_secret = "unguessable"
   124  	peer_ip = "0.0.0.0"
   125  }`