github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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_router(t *testing.T) {
    36  	router := fmt.Sprintf("tunnel-test-router-%s", acctest.RandString(10))
    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: testAccComputeVpnTunnelRouter(router),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckComputeVpnTunnelExists(
    46  						"google_compute_vpn_tunnel.foobar"),
    47  					resource.TestCheckResourceAttr(
    48  						"google_compute_vpn_tunnel.foobar", "router", router),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccComputeVpnTunnel_defaultTrafficSelectors(t *testing.T) {
    56  
    57  	resource.Test(t, resource.TestCase{
    58  		PreCheck:     func() { testAccPreCheck(t) },
    59  		Providers:    testAccProviders,
    60  		CheckDestroy: testAccCheckComputeVpnTunnelDestroy,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccComputeVpnTunnelDefaultTrafficSelectors,
    64  				Check: testAccCheckComputeVpnTunnelExists(
    65  					"google_compute_vpn_tunnel.foobar"),
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func testAccCheckComputeVpnTunnelDestroy(s *terraform.State) error {
    72  	config := testAccProvider.Meta().(*Config)
    73  	project := config.Project
    74  
    75  	vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
    76  
    77  	for _, rs := range s.RootModule().Resources {
    78  		if rs.Type != "google_compute_network" {
    79  			continue
    80  		}
    81  
    82  		region := rs.Primary.Attributes["region"]
    83  		name := rs.Primary.Attributes["name"]
    84  
    85  		_, err := vpnTunnelsService.Get(project, region, name).Do()
    86  
    87  		if err == nil {
    88  			return fmt.Errorf("Error, VPN Tunnel %s in region %s still exists",
    89  				name, region)
    90  		}
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  func testAccCheckComputeVpnTunnelExists(n string) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  		rs, ok := s.RootModule().Resources[n]
    99  		if !ok {
   100  			return fmt.Errorf("Not found: %s", n)
   101  		}
   102  
   103  		if rs.Primary.ID == "" {
   104  			return fmt.Errorf("No ID is set")
   105  		}
   106  
   107  		config := testAccProvider.Meta().(*Config)
   108  		name := rs.Primary.Attributes["name"]
   109  		region := rs.Primary.Attributes["region"]
   110  		project := config.Project
   111  
   112  		vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
   113  		_, err := vpnTunnelsService.Get(project, region, name).Do()
   114  
   115  		if err != nil {
   116  			return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err)
   117  		}
   118  
   119  		return nil
   120  	}
   121  }
   122  
   123  var testAccComputeVpnTunnel_basic = fmt.Sprintf(`
   124  resource "google_compute_network" "foobar" {
   125  	name = "tunnel-test-%s"
   126  }
   127  resource "google_compute_subnetwork" "foobar" {
   128  	name = "tunnel-test-%s"
   129  	network = "${google_compute_network.foobar.self_link}"
   130  	ip_cidr_range = "10.0.0.0/16"
   131  	region = "us-central1"
   132  }
   133  resource "google_compute_address" "foobar" {
   134  	name = "tunnel-test-%s"
   135  	region = "${google_compute_subnetwork.foobar.region}"
   136  }
   137  resource "google_compute_vpn_gateway" "foobar" {
   138  	name = "tunnel-test-%s"
   139  	network = "${google_compute_network.foobar.self_link}"
   140  	region = "${google_compute_subnetwork.foobar.region}"
   141  }
   142  resource "google_compute_forwarding_rule" "foobar_esp" {
   143  	name = "tunnel-test-%s"
   144  	region = "${google_compute_vpn_gateway.foobar.region}"
   145  	ip_protocol = "ESP"
   146  	ip_address = "${google_compute_address.foobar.address}"
   147  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   148  }
   149  resource "google_compute_forwarding_rule" "foobar_udp500" {
   150  	name = "tunnel-test-%s"
   151  	region = "${google_compute_forwarding_rule.foobar_esp.region}"
   152  	ip_protocol = "UDP"
   153  	port_range = "500-500"
   154  	ip_address = "${google_compute_address.foobar.address}"
   155  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   156  }
   157  resource "google_compute_forwarding_rule" "foobar_udp4500" {
   158  	name = "tunnel-test-%s"
   159  	region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   160  	ip_protocol = "UDP"
   161  	port_range = "4500-4500"
   162  	ip_address = "${google_compute_address.foobar.address}"
   163  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   164  }
   165  resource "google_compute_vpn_tunnel" "foobar" {
   166  	name = "tunnel-test-%s"
   167  	region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
   168  	target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
   169  	shared_secret = "unguessable"
   170  	peer_ip = "8.8.8.8"
   171  	local_traffic_selector = ["${google_compute_subnetwork.foobar.ip_cidr_range}"]
   172  	remote_traffic_selector = ["192.168.0.0/24", "192.168.1.0/24"]
   173  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   174  	acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   175  	acctest.RandString(10), acctest.RandString(10))
   176  
   177  func testAccComputeVpnTunnelRouter(router string) string {
   178  	testId := acctest.RandString(10)
   179  	return fmt.Sprintf(`
   180  		resource "google_compute_network" "foobar" {
   181  			name = "tunnel-test-%s"
   182  		}
   183  		resource "google_compute_subnetwork" "foobar" {
   184  			name = "tunnel-test-%s"
   185  			network = "${google_compute_network.foobar.self_link}"
   186  			ip_cidr_range = "10.0.0.0/16"
   187  			region = "us-central1"
   188  		}
   189  		resource "google_compute_address" "foobar" {
   190  			name = "tunnel-test-%s"
   191  			region = "${google_compute_subnetwork.foobar.region}"
   192  		}
   193  		resource "google_compute_vpn_gateway" "foobar" {
   194  			name = "tunnel-test-%s"
   195  			network = "${google_compute_network.foobar.self_link}"
   196  			region = "${google_compute_subnetwork.foobar.region}"
   197  		}
   198  		resource "google_compute_forwarding_rule" "foobar_esp" {
   199  			name = "tunnel-test-%s-1"
   200  			region = "${google_compute_vpn_gateway.foobar.region}"
   201  			ip_protocol = "ESP"
   202  			ip_address = "${google_compute_address.foobar.address}"
   203  			target = "${google_compute_vpn_gateway.foobar.self_link}"
   204  		}
   205  		resource "google_compute_forwarding_rule" "foobar_udp500" {
   206  			name = "tunnel-test-%s-2"
   207  			region = "${google_compute_forwarding_rule.foobar_esp.region}"
   208  			ip_protocol = "UDP"
   209  			port_range = "500-500"
   210  			ip_address = "${google_compute_address.foobar.address}"
   211  			target = "${google_compute_vpn_gateway.foobar.self_link}"
   212  		}
   213  		resource "google_compute_forwarding_rule" "foobar_udp4500" {
   214  			name = "tunnel-test-%s-3"
   215  			region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   216  			ip_protocol = "UDP"
   217  			port_range = "4500-4500"
   218  			ip_address = "${google_compute_address.foobar.address}"
   219  			target = "${google_compute_vpn_gateway.foobar.self_link}"
   220  		}
   221  		resource "google_compute_router" "foobar"{
   222  			name = "%s"
   223  			region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   224  			network = "${google_compute_network.foobar.self_link}"
   225  			bgp {
   226  				asn = 64514
   227  			}
   228  		}
   229  		resource "google_compute_vpn_tunnel" "foobar" {
   230  			name = "tunnel-test-%s"
   231  			region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
   232  			target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
   233  			shared_secret = "unguessable"
   234  			peer_ip = "8.8.8.8"
   235  			router = "${google_compute_router.foobar.name}"
   236  		}
   237  	`, testId, testId, testId, testId, testId, testId, testId, router, testId)
   238  }
   239  
   240  var testAccComputeVpnTunnelDefaultTrafficSelectors = fmt.Sprintf(`
   241  resource "google_compute_network" "foobar" {
   242  	name = "tunnel-test-%s"
   243  	auto_create_subnetworks = "true"
   244  }
   245  resource "google_compute_address" "foobar" {
   246  	name = "tunnel-test-%s"
   247  	region = "us-central1"
   248  }
   249  resource "google_compute_vpn_gateway" "foobar" {
   250  	name = "tunnel-test-%s"
   251  	network = "${google_compute_network.foobar.self_link}"
   252  	region = "${google_compute_address.foobar.region}"
   253  }
   254  resource "google_compute_forwarding_rule" "foobar_esp" {
   255  	name = "tunnel-test-%s"
   256  	region = "${google_compute_vpn_gateway.foobar.region}"
   257  	ip_protocol = "ESP"
   258  	ip_address = "${google_compute_address.foobar.address}"
   259  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   260  }
   261  resource "google_compute_forwarding_rule" "foobar_udp500" {
   262  	name = "tunnel-test-%s"
   263  	region = "${google_compute_forwarding_rule.foobar_esp.region}"
   264  	ip_protocol = "UDP"
   265  	port_range = "500-500"
   266  	ip_address = "${google_compute_address.foobar.address}"
   267  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   268  }
   269  resource "google_compute_forwarding_rule" "foobar_udp4500" {
   270  	name = "tunnel-test-%s"
   271  	region = "${google_compute_forwarding_rule.foobar_udp500.region}"
   272  	ip_protocol = "UDP"
   273  	port_range = "4500-4500"
   274  	ip_address = "${google_compute_address.foobar.address}"
   275  	target = "${google_compute_vpn_gateway.foobar.self_link}"
   276  }
   277  resource "google_compute_vpn_tunnel" "foobar" {
   278  	name = "tunnel-test-%s"
   279  	region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
   280  	target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
   281  	shared_secret = "unguessable"
   282  	peer_ip = "8.8.8.8"
   283  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   284  	acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
   285  	acctest.RandString(10))