github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccCheckComputeVpnTunnelDestroy(s *terraform.State) error { 32 config := testAccProvider.Meta().(*Config) 33 project := config.Project 34 35 vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute) 36 37 for _, rs := range s.RootModule().Resources { 38 if rs.Type != "google_compute_network" { 39 continue 40 } 41 42 region := rs.Primary.Attributes["region"] 43 name := rs.Primary.Attributes["name"] 44 45 _, err := vpnTunnelsService.Get(project, region, name).Do() 46 47 if err == nil { 48 return fmt.Errorf("Error, VPN Tunnel %s in region %s still exists", 49 name, region) 50 } 51 } 52 53 return nil 54 } 55 56 func testAccCheckComputeVpnTunnelExists(n string) resource.TestCheckFunc { 57 return func(s *terraform.State) error { 58 rs, ok := s.RootModule().Resources[n] 59 if !ok { 60 return fmt.Errorf("Not found: %s", n) 61 } 62 63 if rs.Primary.ID == "" { 64 return fmt.Errorf("No ID is set") 65 } 66 67 config := testAccProvider.Meta().(*Config) 68 name := rs.Primary.Attributes["name"] 69 region := rs.Primary.Attributes["region"] 70 project := config.Project 71 72 vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute) 73 _, err := vpnTunnelsService.Get(project, region, name).Do() 74 75 if err != nil { 76 return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err) 77 } 78 79 return nil 80 } 81 } 82 83 var testAccComputeVpnTunnel_basic = fmt.Sprintf(` 84 resource "google_compute_network" "foobar" { 85 name = "tunnel-test-%s" 86 ipv4_range = "10.0.0.0/16" 87 } 88 resource "google_compute_address" "foobar" { 89 name = "tunnel-test-%s" 90 region = "us-central1" 91 } 92 resource "google_compute_vpn_gateway" "foobar" { 93 name = "tunnel-test-%s" 94 network = "${google_compute_network.foobar.self_link}" 95 region = "${google_compute_address.foobar.region}" 96 } 97 resource "google_compute_forwarding_rule" "foobar_esp" { 98 name = "tunnel-test-%s" 99 region = "${google_compute_vpn_gateway.foobar.region}" 100 ip_protocol = "ESP" 101 ip_address = "${google_compute_address.foobar.address}" 102 target = "${google_compute_vpn_gateway.foobar.self_link}" 103 } 104 resource "google_compute_forwarding_rule" "foobar_udp500" { 105 name = "tunnel-test-%s" 106 region = "${google_compute_forwarding_rule.foobar_esp.region}" 107 ip_protocol = "UDP" 108 port_range = "500-500" 109 ip_address = "${google_compute_address.foobar.address}" 110 target = "${google_compute_vpn_gateway.foobar.self_link}" 111 } 112 resource "google_compute_forwarding_rule" "foobar_udp4500" { 113 name = "tunnel-test-%s" 114 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 115 ip_protocol = "UDP" 116 port_range = "4500-4500" 117 ip_address = "${google_compute_address.foobar.address}" 118 target = "${google_compute_vpn_gateway.foobar.self_link}" 119 } 120 resource "google_compute_vpn_tunnel" "foobar" { 121 name = "tunnel-test-%s" 122 region = "${google_compute_forwarding_rule.foobar_udp4500.region}" 123 target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}" 124 shared_secret = "unguessable" 125 peer_ip = "8.8.8.8" 126 }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), 127 acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), 128 acctest.RandString(10))