github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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/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 TestAccComputeVpnTunnel_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckComputeVpnTunnelDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccComputeVpnTunnel_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckComputeVpnTunnelExists(
    24  						"google_compute_vpn_tunnel.foobar"),
    25  					resource.TestCheckResourceAttr(
    26  						"google_compute_vpn_tunnel.foobar", "local_traffic_selector.#", "1"),
    27  					resource.TestCheckResourceAttr(
    28  						"google_compute_vpn_tunnel.foobar", "remote_traffic_selector.#", "2"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccComputeVpnTunnel_defaultTrafficSelectors(t *testing.T) {
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckComputeVpnTunnelDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccComputeVpnTunnelDefaultTrafficSelectors,
    44  				Check: testAccCheckComputeVpnTunnelExists(
    45  					"google_compute_vpn_tunnel.foobar"),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckComputeVpnTunnelDestroy(s *terraform.State) error {
    52  	config := testAccProvider.Meta().(*Config)
    53  	project := config.Project
    54  
    55  	vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
    56  
    57  	for _, rs := range s.RootModule().Resources {
    58  		if rs.Type != "google_compute_network" {
    59  			continue
    60  		}
    61  
    62  		region := rs.Primary.Attributes["region"]
    63  		name := rs.Primary.Attributes["name"]
    64  
    65  		_, err := vpnTunnelsService.Get(project, region, name).Do()
    66  
    67  		if err == nil {
    68  			return fmt.Errorf("Error, VPN Tunnel %s in region %s still exists",
    69  				name, region)
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func testAccCheckComputeVpnTunnelExists(n string) resource.TestCheckFunc {
    77  	return func(s *terraform.State) error {
    78  		rs, ok := s.RootModule().Resources[n]
    79  		if !ok {
    80  			return fmt.Errorf("Not found: %s", n)
    81  		}
    82  
    83  		if rs.Primary.ID == "" {
    84  			return fmt.Errorf("No ID is set")
    85  		}
    86  
    87  		config := testAccProvider.Meta().(*Config)
    88  		name := rs.Primary.Attributes["name"]
    89  		region := rs.Primary.Attributes["region"]
    90  		project := config.Project
    91  
    92  		vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
    93  		_, err := vpnTunnelsService.Get(project, region, name).Do()
    94  
    95  		if err != nil {
    96  			return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err)
    97  		}
    98  
    99  		return nil
   100  	}
   101  }
   102  
   103  var testAccComputeVpnTunnel_basic = fmt.Sprintf(`
   104  resource "google_compute_network" "foobar" {
   105  	name = "tunnel-test-%s"
   106  }
   107  resource "google_compute_subnetwork" "foobar" {
   108  	name = "tunnel-test-%s"
   109  	network = "${google_compute_network.foobar.self_link}"
   110  	ip_cidr_range = "10.0.0.0/16"
   111  	region = "us-central1"
   112  }
   113  resource "google_compute_address" "foobar" {
   114  	name = "tunnel-test-%s"
   115  	region = "${google_compute_subnetwork.foobar.region}"
   116  }
   117  resource "google_compute_vpn_gateway" "foobar" {
   118  	name = "tunnel-test-%s"
   119  	network = "${google_compute_network.foobar.self_link}"
   120  	region = "${google_compute_subnetwork.foobar.region}"
   121  }
   122  resource "google_compute_forwarding_rule" "foobar_esp" {
   123  	name = "tunnel-test-%s"
   124  	region = "${google_compute_vpn_gateway.foobar.region}"
   125  	ip_protocol = "ESP"
   126  	ip_address = "${google_compute_address.foobar.address}"
   127  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   128  }
   129  resource "google_compute_forwarding_rule" "foobar_udp500" {
   130  	name = "tunnel-test-%s"
   131  	region = "${google_compute_forwarding_rule.foobar_esp.region}"
   132  	ip_protocol = "UDP"
   133  	port_range = "500-500"
   134  	ip_address = "${google_compute_address.foobar.address}"
   135  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   136  }
   137  resource "google_compute_forwarding_rule" "foobar_udp4500" {
   138  	name = "tunnel-test-%s"
   139  	region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   140  	ip_protocol = "UDP"
   141  	port_range = "4500-4500"
   142  	ip_address = "${google_compute_address.foobar.address}"
   143  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   144  }
   145  resource "google_compute_vpn_tunnel" "foobar" {
   146  	name = "tunnel-test-%s"
   147  	region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
   148  	target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
   149  	shared_secret = "unguessable"
   150  	peer_ip = "8.8.8.8"
   151  	local_traffic_selector = ["${google_compute_subnetwork.foobar.ip_cidr_range}"]
   152  	remote_traffic_selector = ["192.168.0.0/24", "192.168.1.0/24"]
   153  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   154  	acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   155  	acctest.RandString(10), acctest.RandString(10))
   156  
   157  var testAccComputeVpnTunnelDefaultTrafficSelectors = fmt.Sprintf(`
   158  resource "google_compute_network" "foobar" {
   159  	name = "tunnel-test-%s"
   160  	auto_create_subnetworks = "true"
   161  }
   162  resource "google_compute_address" "foobar" {
   163  	name = "tunnel-test-%s"
   164  	region = "us-central1"
   165  }
   166  resource "google_compute_vpn_gateway" "foobar" {
   167  	name = "tunnel-test-%s"
   168  	network = "${google_compute_network.foobar.self_link}"
   169  	region = "${google_compute_address.foobar.region}"
   170  }
   171  resource "google_compute_forwarding_rule" "foobar_esp" {
   172  	name = "tunnel-test-%s"
   173  	region = "${google_compute_vpn_gateway.foobar.region}"
   174  	ip_protocol = "ESP"
   175  	ip_address = "${google_compute_address.foobar.address}"
   176  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   177  }
   178  resource "google_compute_forwarding_rule" "foobar_udp500" {
   179  	name = "tunnel-test-%s"
   180  	region = "${google_compute_forwarding_rule.foobar_esp.region}"
   181  	ip_protocol = "UDP"
   182  	port_range = "500-500"
   183  	ip_address = "${google_compute_address.foobar.address}"
   184  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   185  }
   186  resource "google_compute_forwarding_rule" "foobar_udp4500" {
   187  	name = "tunnel-test-%s"
   188  	region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   189  	ip_protocol = "UDP"
   190  	port_range = "4500-4500"
   191  	ip_address = "${google_compute_address.foobar.address}"
   192  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   193  }
   194  resource "google_compute_vpn_tunnel" "foobar" {
   195  	name = "tunnel-test-%s"
   196  	region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
   197  	target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
   198  	shared_secret = "unguessable"
   199  	peer_ip = "8.8.8.8"
   200  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   201  	acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   202  	acctest.RandString(10))