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