Skip to content

Commit

Permalink
Moving nexthop ILB to GA
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
chrisst committed Dec 12, 2019
1 parent 4a368b7 commit 60af650
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 14 deletions.
44 changes: 40 additions & 4 deletions google/resource_compute_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,19 @@ partial valid URL:
* 'projects/project/global/gateways/default-internet-gateway'
* 'global/gateways/default-internet-gateway'
* The string 'default-internet-gateway'.`,
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel"},
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel", "next_hop_ilb"},
},
"next_hop_ilb": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `The URL to a forwarding rule of type loadBalancingScheme=INTERNAL that should handle matching packets.
You can only specify the forwarding rule as a partial or full URL. For example, the following are all valid URLs:
https://www.googleapis.com/compute/v1/projects/project/regions/region/forwardingRules/forwardingRule
regions/region/forwardingRules/forwardingRule
Note that this can only be used when the destinationRange is a public (non-RFC 1918) IP CIDR range.`,
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel", "next_hop_ilb"},
},
"next_hop_instance": {
Type: schema.TypeString,
Expand All @@ -99,23 +111,23 @@ You can specify this as a full or partial URL. For example:
* 'projects/project/zones/zone/instances/instance'
* 'zones/zone/instances/instance'
* Just the instance name, with the zone in 'next_hop_instance_zone'.`,
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel"},
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel", "next_hop_ilb"},
},
"next_hop_ip": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Network IP address of an instance that should handle matching packets.`,
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel"},
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel", "next_hop_ilb"},
},
"next_hop_vpn_tunnel": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `URL to a VpnTunnel that should handle matching packets.`,
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel"},
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel", "next_hop_ilb"},
},
"priority": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -228,6 +240,12 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("next_hop_vpn_tunnel"); !isEmptyValue(reflect.ValueOf(nextHopVpnTunnelProp)) && (ok || !reflect.DeepEqual(v, nextHopVpnTunnelProp)) {
obj["nextHopVpnTunnel"] = nextHopVpnTunnelProp
}
nextHopIlbProp, err := expandComputeRouteNextHopIlb(d.Get("next_hop_ilb"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("next_hop_ilb"); !isEmptyValue(reflect.ValueOf(nextHopIlbProp)) && (ok || !reflect.DeepEqual(v, nextHopIlbProp)) {
obj["nextHopIlb"] = nextHopIlbProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/routes")
if err != nil {
Expand Down Expand Up @@ -332,6 +350,9 @@ func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("next_hop_network", flattenComputeRouteNextHopNetwork(res["nextHopNetwork"], d)); err != nil {
return fmt.Errorf("Error reading Route: %s", err)
}
if err := d.Set("next_hop_ilb", flattenComputeRouteNextHopIlb(res["nextHopIlb"], d)); err != nil {
return fmt.Errorf("Error reading Route: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading Route: %s", err)
}
Expand Down Expand Up @@ -454,6 +475,13 @@ func flattenComputeRouteNextHopNetwork(v interface{}, d *schema.ResourceData) in
return v
}

func flattenComputeRouteNextHopIlb(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}
return ConvertSelfLinkToV1(v.(string))
}

func expandComputeRouteDestRange(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -517,6 +545,14 @@ func expandComputeRouteNextHopVpnTunnel(v interface{}, d TerraformResourceData,
return f.RelativeLink(), nil
}

func expandComputeRouteNextHopIlb(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseRegionalFieldValue("forwardingRules", v.(string), "project", "region", "zone", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for next_hop_ilb: %s", err)
}
return f.RelativeLink(), nil
}

func resourceComputeRouteDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
if v, ok := res["nextHopInstance"]; ok {
val, err := parseZonalFieldValue("instances", v.(string), "project", "next_hop_instance_zone", d, meta.(*Config), true)
Expand Down
75 changes: 75 additions & 0 deletions google/resource_compute_route_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,81 @@ resource "google_compute_network" "default" {
`, context)
}

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

context := map[string]interface{}{
"random_suffix": acctest.RandString(10),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeRoute_routeIlbExample(context),
},
{
ResourceName: "google_compute_route.route-ilb",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeRoute_routeIlbExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network" "default" {
name = "compute-network%{random_suffix}"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "default" {
name = "compute-subnet%{random_suffix}"
ip_cidr_range = "10.0.1.0/24"
region = "us-central1"
network = google_compute_network.default.self_link
}
resource "google_compute_health_check" "hc" {
name = "proxy-health-check%{random_suffix}"
check_interval_sec = 1
timeout_sec = 1
tcp_health_check {
port = "80"
}
}
resource "google_compute_region_backend_service" "backend" {
name = "compute-backend%{random_suffix}"
region = "us-central1"
health_checks = [google_compute_health_check.hc.self_link]
}
resource "google_compute_forwarding_rule" "default" {
name = "compute-forwarding-rule%{random_suffix}"
region = "us-central1"
load_balancing_scheme = "INTERNAL"
backend_service = google_compute_region_backend_service.backend.self_link
all_ports = true
network = google_compute_network.default.name
subnetwork = google_compute_subnetwork.default.name
}
resource "google_compute_route" "route-ilb" {
name = "route-ilb%{random_suffix}"
dest_range = "0.0.0.0/0"
network = google_compute_network.default.name
next_hop_ilb = google_compute_forwarding_rule.default.self_link
priority = 2000
}
`, context)
}

func testAccCheckComputeRouteDestroy(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_route" {
Expand Down
22 changes: 12 additions & 10 deletions website/docs/r/compute_route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,27 @@ resource "google_compute_network" "default" {
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=route_ilb_beta&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=route_ilb&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Route Ilb Beta
## Example Usage - Route Ilb


```hcl
resource "google_compute_network" "default" {
provider = google-beta
name = "compute-network"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "default" {
provider = google-beta
name = "compute-subnet"
ip_cidr_range = "10.0.1.0/24"
region = "us-central1"
network = google_compute_network.default.self_link
}
resource "google_compute_health_check" "hc" {
provider = google-beta
name = "proxy-health-check"
check_interval_sec = 1
timeout_sec = 1
Expand All @@ -108,14 +105,12 @@ resource "google_compute_health_check" "hc" {
}
resource "google_compute_region_backend_service" "backend" {
provider = google-beta
name = "compute-backend"
region = "us-central1"
health_checks = [google_compute_health_check.hc.self_link]
}
resource "google_compute_forwarding_rule" "default" {
provider = google-beta
name = "compute-forwarding-rule"
region = "us-central1"
Expand All @@ -126,9 +121,8 @@ resource "google_compute_forwarding_rule" "default" {
subnetwork = google_compute_subnetwork.default.name
}
resource "google_compute_route" "route-ilb-beta" {
provider = google-beta
name = "route-ilb-beta"
resource "google_compute_route" "route-ilb" {
name = "route-ilb"
dest_range = "0.0.0.0/0"
network = google_compute_network.default.name
next_hop_ilb = google_compute_forwarding_rule.default.self_link
Expand Down Expand Up @@ -208,6 +202,14 @@ The following arguments are supported:
(Optional)
URL to a VpnTunnel that should handle matching packets.

* `next_hop_ilb` -
(Optional)
The URL to a forwarding rule of type loadBalancingScheme=INTERNAL that should handle matching packets.
You can only specify the forwarding rule as a partial or full URL. For example, the following are all valid URLs:
https://www.googleapis.com/compute/v1/projects/project/regions/region/forwardingRules/forwardingRule
regions/region/forwardingRules/forwardingRule
Note that this can only be used when the destinationRange is a public (non-RFC 1918) IP CIDR range.

* `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 60af650

Please sign in to comment.