Skip to content

Commit

Permalink
extract vpn tunnel region/project from vpn gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow authored and modular-magician committed Dec 12, 2018
1 parent 1b753f2 commit 152938e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
20 changes: 20 additions & 0 deletions google-beta/resource_compute_vpn_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) er
obj["region"] = regionProp
}

obj, err = resourceComputeVpnTunnelEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/regions/{{region}}/vpnTunnels")
if err != nil {
return err
Expand Down Expand Up @@ -694,3 +699,18 @@ func expandComputeVpnTunnelRegion(v interface{}, d *schema.ResourceData, config
}
return f.RelativeLink(), nil
}

func resourceComputeVpnTunnelEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
config := meta.(*Config)
f, err := parseRegionalFieldValue("targetVpnGateways", d.Get("target_vpn_gateway").(string), "project", "region", "zone", d, config, true)
if err != nil {
return nil, err
}
if _, ok := d.GetOk("project"); !ok {
d.Set("project", f.Project)
}
if _, ok := d.GetOk("region"); !ok {
d.Set("region", f.Region)
}
return obj, nil
}
85 changes: 85 additions & 0 deletions google-beta/resource_compute_vpn_tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ func TestAccComputeVpnTunnel_basic(t *testing.T) {
})
}

func TestAccComputeVpnTunnel_regionFromGateway(t *testing.T) {
t.Parallel()
region := "us-central1"
if getTestRegionFromEnv() == region {
// Make sure we choose a region that isn't the provider default
// in order to test getting the region from the gateway and not the
// provider.
region = "us-west1"
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeVpnTunnelDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeVpnTunnel_regionFromGateway(region),
},
resource.TestStep{
ResourceName: "google_compute_vpn_tunnel.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"shared_secret"},
},
},
})
}

func TestAccComputeVpnTunnel_router(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -128,6 +156,63 @@ resource "google_compute_vpn_tunnel" "foobar" {
acctest.RandString(10), acctest.RandString(10))
}

func testAccComputeVpnTunnel_regionFromGateway(region string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "tunnel-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "tunnel-test-subnetwork-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "%s"
}
resource "google_compute_address" "foobar" {
name = "tunnel-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_vpn_gateway" "foobar" {
name = "tunnel-test-%s"
network = "${google_compute_network.foobar.self_link}"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_forwarding_rule" "foobar_esp" {
name = "tunnel-test-%s"
region = "${google_compute_vpn_gateway.foobar.region}"
ip_protocol = "ESP"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp500" {
name = "tunnel-test-%s"
region = "${google_compute_forwarding_rule.foobar_esp.region}"
ip_protocol = "UDP"
port_range = "500-500"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp4500" {
name = "tunnel-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
ip_protocol = "UDP"
port_range = "4500-4500"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_vpn_tunnel" "foobar" {
name = "tunnel-test-%s"
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
shared_secret = "unguessable"
peer_ip = "8.8.8.8"
local_traffic_selector = ["${google_compute_subnetwork.foobar.ip_cidr_range}"]
remote_traffic_selector = ["192.168.0.0/24", "192.168.1.0/24"]
depends_on = ["google_compute_forwarding_rule.foobar_udp4500"]
}`, acctest.RandString(10), acctest.RandString(10), region, acctest.RandString(10),
acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
acctest.RandString(10), acctest.RandString(10))
}

func testAccComputeVpnTunnelRouter(router string) string {
testId := acctest.RandString(10)
return fmt.Sprintf(`
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/compute_vpn_tunnel.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ The following arguments are supported:

* `region` -
(Optional)
The region where the tunnel is located.
The region where the tunnel is located. If unset, is set to the region of `target_vpn_gateway`.
* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand Down

0 comments on commit 152938e

Please sign in to comment.