diff --git a/build/terraform b/build/terraform index cd7dd3540fea..9f96a317411b 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit cd7dd3540feaa014900047ef0ff49645298f9e35 +Subproject commit 9f96a317411ba1043211bf4c54a9ffef925c3f8f diff --git a/build/terraform-beta b/build/terraform-beta index 2cc880235eec..2afcecd478de 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 2cc880235eec2190c2815313902c5d934e410935 +Subproject commit 2afcecd478de836a6d1557ecf7b8846fc4948ee9 diff --git a/third_party/terraform/resources/resource_compute_router_peer.go b/third_party/terraform/resources/resource_compute_router_peer.go index 3f3afd0c25e8..3b569f169af7 100644 --- a/third_party/terraform/resources/resource_compute_router_peer.go +++ b/third_party/terraform/resources/resource_compute_router_peer.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" ) @@ -55,6 +56,41 @@ func resourceComputeRouterPeer() *schema.Resource { ForceNew: true, }, + "advertise_mode": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"DEFAULT", "CUSTOM", ""}, false), + Default: "DEFAULT", + }, + + "advertised_groups": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "advertised_ip_ranges": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "description": { + Type: schema.TypeString, + Optional: true, + }, + "range": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + "ip_address": { Type: schema.TypeString, Computed: true, @@ -136,6 +172,18 @@ func resourceComputeRouterPeerCreate(d *schema.ResourceData, meta interface{}) e peer.AdvertisedRoutePriority = int64(v.(int)) } + if v, ok := d.GetOk("advertise_mode"); ok { + peer.AdvertiseMode = v.(string) + } + + if v, ok := d.GetOk("advertised_groups"); ok { + peer.AdvertisedGroups = expandAdvertisedGroups(v.([]interface{})) + } + + if v, ok := d.GetOk("advertised_ip_ranges"); ok { + peer.AdvertisedIpRanges = expandAdvertisedIpRanges(v.([]interface{})) + } + log.Printf("[INFO] Adding peer %s", peerName) peers = append(peers, peer) patchRouter := &compute.Router{ @@ -195,6 +243,9 @@ func resourceComputeRouterPeerRead(d *schema.ResourceData, meta interface{}) err d.Set("peer_ip_address", peer.PeerIpAddress) d.Set("peer_asn", peer.PeerAsn) d.Set("advertised_route_priority", peer.AdvertisedRoutePriority) + d.Set("advertise_mode", peer.AdvertiseMode) + d.Set("advertised_groups", peer.AdvertisedGroups) + d.Set("advertised_ip_ranges", flattenAdvertisedIpRanges(peer.AdvertisedIpRanges)) d.Set("ip_address", peer.IpAddress) d.Set("region", region) d.Set("project", project) @@ -292,3 +343,50 @@ func resourceComputeRouterPeerImportState(d *schema.ResourceData, meta interface return []*schema.ResourceData{d}, nil } + +func expandAdvertisedGroups(v []interface{}) []string { + var groups []string + + if len(v) == 0 { + return nil + } + + for _, group := range v { + groups = append(groups, group.(string)) + } + + return groups +} + +func expandAdvertisedIpRanges(v []interface{}) []*compute.RouterAdvertisedIpRange { + var ranges []*compute.RouterAdvertisedIpRange + + if len(v) == 0 { + return nil + } + + for _, r := range v { + ipRange := r.(map[string]interface{}) + + ranges = append(ranges, &compute.RouterAdvertisedIpRange{ + Range: ipRange["range"].(string), + Description: ipRange["description"].(string), + }) + } + + return ranges +} + +func flattenAdvertisedIpRanges(ranges []*compute.RouterAdvertisedIpRange) []map[string]interface{} { + ls := make([]map[string]interface{}, 0, len(ranges)) + for _, r := range ranges { + if r == nil { + continue + } + ls = append(ls, map[string]interface{}{ + "range": r.Range, + "description": r.Description, + }) + } + return ls +} diff --git a/third_party/terraform/tests/resource_compute_router_peer_test.go b/third_party/terraform/tests/resource_compute_router_peer_test.go index 89ea3a96f287..120af07cce1e 100644 --- a/third_party/terraform/tests/resource_compute_router_peer_test.go +++ b/third_party/terraform/tests/resource_compute_router_peer_test.go @@ -37,6 +37,29 @@ func TestAccComputeRouterPeer_basic(t *testing.T) { }) } +func TestAccComputeRouterPeer_advertiseMode(t *testing.T) { + t.Parallel() + + testId := acctest.RandString(10) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeRouterPeerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeRouterPeerAdvertiseMode(testId), + Check: testAccCheckComputeRouterPeerExists( + "google_compute_router_peer.foobar"), + }, + { + ResourceName: "google_compute_router_peer.foobar", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckComputeRouterPeerDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -303,3 +326,86 @@ func testAccComputeRouterPeerKeepRouter(testId string) string { } `, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId) } + +func testAccComputeRouterPeerAdvertiseMode(testId string) string { + return fmt.Sprintf(` + resource "google_compute_network" "foobar" { + name = "router-peer-test-%s" + } + resource "google_compute_subnetwork" "foobar" { + name = "router-peer-test-subnetwork-%s" + network = "${google_compute_network.foobar.self_link}" + ip_cidr_range = "10.0.0.0/16" + region = "us-central1" + } + resource "google_compute_address" "foobar" { + name = "router-peer-test-%s" + region = "${google_compute_subnetwork.foobar.region}" + } + resource "google_compute_vpn_gateway" "foobar" { + name = "router-peer-test-%s" + network = "${google_compute_network.foobar.self_link}" + region = "${google_compute_subnetwork.foobar.region}" + } + resource "google_compute_forwarding_rule" "foobar_esp" { + name = "router-peer-test-%s-1" + 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 = "router-peer-test-%s-2" + 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 = "router-peer-test-%s-3" + 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_router" "foobar"{ + name = "router-peer-test-%s" + region = "${google_compute_forwarding_rule.foobar_udp500.region}" + network = "${google_compute_network.foobar.self_link}" + bgp { + asn = 64514 + } + } + resource "google_compute_vpn_tunnel" "foobar" { + name = "router-peer-test-%s" + region = "${google_compute_forwarding_rule.foobar_udp4500.region}" + target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}" + shared_secret = "unguessable" + peer_ip = "8.8.8.8" + router = "${google_compute_router.foobar.name}" + } + resource "google_compute_router_interface" "foobar" { + name = "router-peer-test-%s" + router = "${google_compute_router.foobar.name}" + region = "${google_compute_router.foobar.region}" + ip_range = "169.254.3.1/30" + vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}" + } + resource "google_compute_router_peer" "foobar" { + name = "router-peer-test-%s" + router = "${google_compute_router.foobar.name}" + region = "${google_compute_router.foobar.region}" + peer_ip_address = "169.254.3.2" + peer_asn = 65515 + advertised_route_priority = 100 + advertise_mode = "CUSTOM" + advertised_groups = ["ALL_SUBNETS"] + advertised_ip_ranges { + range = "10.1.0.0/32" + } + interface = "${google_compute_router_interface.foobar.name}" + } + `, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId) +}