Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add advanced routing options to router peer #2271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
98 changes: 98 additions & 0 deletions third_party/terraform/resources/resource_compute_router_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
106 changes: 106 additions & 0 deletions third_party/terraform/tests/resource_compute_router_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a lot cleaner with the context map that some other tests use, but its all the same string and changing it would require changing the other tests in this file so 🤷‍♂

}