Skip to content

Commit 9f96a31

Browse files
authored
add advanced routing options to router peer
2 parents cd7dd35 + d3b48f5 commit 9f96a31

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

google/resource_compute_router_peer.go

+98
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/hashicorp/terraform/helper/schema"
10+
"github.com/hashicorp/terraform/helper/validation"
1011
"google.golang.org/api/compute/v1"
1112
"google.golang.org/api/googleapi"
1213
)
@@ -55,6 +56,41 @@ func resourceComputeRouterPeer() *schema.Resource {
5556
ForceNew: true,
5657
},
5758

59+
"advertise_mode": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
ForceNew: true,
63+
ValidateFunc: validation.StringInSlice([]string{"DEFAULT", "CUSTOM", ""}, false),
64+
Default: "DEFAULT",
65+
},
66+
67+
"advertised_groups": {
68+
Type: schema.TypeList,
69+
Optional: true,
70+
ForceNew: true,
71+
Elem: &schema.Schema{
72+
Type: schema.TypeString,
73+
},
74+
},
75+
76+
"advertised_ip_ranges": {
77+
Type: schema.TypeList,
78+
Optional: true,
79+
ForceNew: true,
80+
Elem: &schema.Resource{
81+
Schema: map[string]*schema.Schema{
82+
"description": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
},
86+
"range": {
87+
Type: schema.TypeString,
88+
Optional: true,
89+
},
90+
},
91+
},
92+
},
93+
5894
"ip_address": {
5995
Type: schema.TypeString,
6096
Computed: true,
@@ -136,6 +172,18 @@ func resourceComputeRouterPeerCreate(d *schema.ResourceData, meta interface{}) e
136172
peer.AdvertisedRoutePriority = int64(v.(int))
137173
}
138174

175+
if v, ok := d.GetOk("advertise_mode"); ok {
176+
peer.AdvertiseMode = v.(string)
177+
}
178+
179+
if v, ok := d.GetOk("advertised_groups"); ok {
180+
peer.AdvertisedGroups = expandAdvertisedGroups(v.([]interface{}))
181+
}
182+
183+
if v, ok := d.GetOk("advertised_ip_ranges"); ok {
184+
peer.AdvertisedIpRanges = expandAdvertisedIpRanges(v.([]interface{}))
185+
}
186+
139187
log.Printf("[INFO] Adding peer %s", peerName)
140188
peers = append(peers, peer)
141189
patchRouter := &compute.Router{
@@ -195,6 +243,9 @@ func resourceComputeRouterPeerRead(d *schema.ResourceData, meta interface{}) err
195243
d.Set("peer_ip_address", peer.PeerIpAddress)
196244
d.Set("peer_asn", peer.PeerAsn)
197245
d.Set("advertised_route_priority", peer.AdvertisedRoutePriority)
246+
d.Set("advertise_mode", peer.AdvertiseMode)
247+
d.Set("advertised_groups", peer.AdvertisedGroups)
248+
d.Set("advertised_ip_ranges", flattenAdvertisedIpRanges(peer.AdvertisedIpRanges))
198249
d.Set("ip_address", peer.IpAddress)
199250
d.Set("region", region)
200251
d.Set("project", project)
@@ -292,3 +343,50 @@ func resourceComputeRouterPeerImportState(d *schema.ResourceData, meta interface
292343

293344
return []*schema.ResourceData{d}, nil
294345
}
346+
347+
func expandAdvertisedGroups(v []interface{}) []string {
348+
var groups []string
349+
350+
if len(v) == 0 {
351+
return nil
352+
}
353+
354+
for _, group := range v {
355+
groups = append(groups, group.(string))
356+
}
357+
358+
return groups
359+
}
360+
361+
func expandAdvertisedIpRanges(v []interface{}) []*compute.RouterAdvertisedIpRange {
362+
var ranges []*compute.RouterAdvertisedIpRange
363+
364+
if len(v) == 0 {
365+
return nil
366+
}
367+
368+
for _, r := range v {
369+
ipRange := r.(map[string]interface{})
370+
371+
ranges = append(ranges, &compute.RouterAdvertisedIpRange{
372+
Range: ipRange["range"].(string),
373+
Description: ipRange["description"].(string),
374+
})
375+
}
376+
377+
return ranges
378+
}
379+
380+
func flattenAdvertisedIpRanges(ranges []*compute.RouterAdvertisedIpRange) []map[string]interface{} {
381+
ls := make([]map[string]interface{}, 0, len(ranges))
382+
for _, r := range ranges {
383+
if r == nil {
384+
continue
385+
}
386+
ls = append(ls, map[string]interface{}{
387+
"range": r.Range,
388+
"description": r.Description,
389+
})
390+
}
391+
return ls
392+
}

google/resource_compute_router_peer_test.go

+106
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ func TestAccComputeRouterPeer_basic(t *testing.T) {
3737
})
3838
}
3939

40+
func TestAccComputeRouterPeer_advertiseMode(t *testing.T) {
41+
t.Parallel()
42+
43+
testId := acctest.RandString(10)
44+
resource.Test(t, resource.TestCase{
45+
PreCheck: func() { testAccPreCheck(t) },
46+
Providers: testAccProviders,
47+
CheckDestroy: testAccCheckComputeRouterPeerDestroy,
48+
Steps: []resource.TestStep{
49+
{
50+
Config: testAccComputeRouterPeerAdvertiseMode(testId),
51+
Check: testAccCheckComputeRouterPeerExists(
52+
"google_compute_router_peer.foobar"),
53+
},
54+
{
55+
ResourceName: "google_compute_router_peer.foobar",
56+
ImportState: true,
57+
ImportStateVerify: true,
58+
},
59+
},
60+
})
61+
}
62+
4063
func testAccCheckComputeRouterPeerDestroy(s *terraform.State) error {
4164
config := testAccProvider.Meta().(*Config)
4265

@@ -303,3 +326,86 @@ func testAccComputeRouterPeerKeepRouter(testId string) string {
303326
}
304327
`, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId)
305328
}
329+
330+
func testAccComputeRouterPeerAdvertiseMode(testId string) string {
331+
return fmt.Sprintf(`
332+
resource "google_compute_network" "foobar" {
333+
name = "router-peer-test-%s"
334+
}
335+
resource "google_compute_subnetwork" "foobar" {
336+
name = "router-peer-test-subnetwork-%s"
337+
network = "${google_compute_network.foobar.self_link}"
338+
ip_cidr_range = "10.0.0.0/16"
339+
region = "us-central1"
340+
}
341+
resource "google_compute_address" "foobar" {
342+
name = "router-peer-test-%s"
343+
region = "${google_compute_subnetwork.foobar.region}"
344+
}
345+
resource "google_compute_vpn_gateway" "foobar" {
346+
name = "router-peer-test-%s"
347+
network = "${google_compute_network.foobar.self_link}"
348+
region = "${google_compute_subnetwork.foobar.region}"
349+
}
350+
resource "google_compute_forwarding_rule" "foobar_esp" {
351+
name = "router-peer-test-%s-1"
352+
region = "${google_compute_vpn_gateway.foobar.region}"
353+
ip_protocol = "ESP"
354+
ip_address = "${google_compute_address.foobar.address}"
355+
target = "${google_compute_vpn_gateway.foobar.self_link}"
356+
}
357+
resource "google_compute_forwarding_rule" "foobar_udp500" {
358+
name = "router-peer-test-%s-2"
359+
region = "${google_compute_forwarding_rule.foobar_esp.region}"
360+
ip_protocol = "UDP"
361+
port_range = "500-500"
362+
ip_address = "${google_compute_address.foobar.address}"
363+
target = "${google_compute_vpn_gateway.foobar.self_link}"
364+
}
365+
resource "google_compute_forwarding_rule" "foobar_udp4500" {
366+
name = "router-peer-test-%s-3"
367+
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
368+
ip_protocol = "UDP"
369+
port_range = "4500-4500"
370+
ip_address = "${google_compute_address.foobar.address}"
371+
target = "${google_compute_vpn_gateway.foobar.self_link}"
372+
}
373+
resource "google_compute_router" "foobar"{
374+
name = "router-peer-test-%s"
375+
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
376+
network = "${google_compute_network.foobar.self_link}"
377+
bgp {
378+
asn = 64514
379+
}
380+
}
381+
resource "google_compute_vpn_tunnel" "foobar" {
382+
name = "router-peer-test-%s"
383+
region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
384+
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
385+
shared_secret = "unguessable"
386+
peer_ip = "8.8.8.8"
387+
router = "${google_compute_router.foobar.name}"
388+
}
389+
resource "google_compute_router_interface" "foobar" {
390+
name = "router-peer-test-%s"
391+
router = "${google_compute_router.foobar.name}"
392+
region = "${google_compute_router.foobar.region}"
393+
ip_range = "169.254.3.1/30"
394+
vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}"
395+
}
396+
resource "google_compute_router_peer" "foobar" {
397+
name = "router-peer-test-%s"
398+
router = "${google_compute_router.foobar.name}"
399+
region = "${google_compute_router.foobar.region}"
400+
peer_ip_address = "169.254.3.2"
401+
peer_asn = 65515
402+
advertised_route_priority = 100
403+
advertise_mode = "CUSTOM"
404+
advertised_groups = ["ALL_SUBNETS"]
405+
advertised_ip_ranges {
406+
range = "10.1.0.0/32"
407+
}
408+
interface = "${google_compute_router_interface.foobar.name}"
409+
}
410+
`, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId)
411+
}

0 commit comments

Comments
 (0)