Skip to content

Commit

Permalink
Adding next_hop_ilb attribute to compute route resource
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
Madankapoor authored and modular-magician committed Aug 21, 2019
1 parent 9df153a commit 3c556c9
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
31 changes: 31 additions & 0 deletions google-beta/resource_compute_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func resourceComputeRoute() *schema.Resource {
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"next_hop_ilb": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"next_hop_instance": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -77,6 +83,7 @@ func resourceComputeRoute() *schema.Resource {
},
"next_hop_ip": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
Expand Down Expand Up @@ -188,6 +195,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 @@ -298,6 +311,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 @@ -426,6 +442,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 @@ -493,6 +516,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
76 changes: 76 additions & 0 deletions google-beta/resource_compute_route_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,82 @@ resource "google_compute_network" "default" {
`, context)
}

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

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckComputeRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeRoute_routeIlbBetaExample(context),
},
},
})
}

func testAccComputeRoute_routeIlbBetaExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network" "default" {
provider = "google-beta"
name = "compute-network%{random_suffix}"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "default" {
provider = "google-beta"
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" {
provider = "google-beta"
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" {
provider = "google-beta"
name = "compute-backend%{random_suffix}"
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%{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" "default" {
provider = "google-beta"
name = "route-ilb-beta%{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
70 changes: 70 additions & 0 deletions website/docs/r/compute_route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,68 @@ resource "google_compute_network" "default" {
name = "compute-network"
}
```
<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.jparrowsec.cn%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">
<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


```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
tcp_health_check {
port = "80"
}
}
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"
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" "default" {
provider = "google-beta"
name = "route-ilb-beta"
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
}
```

## Argument Reference

Expand Down Expand Up @@ -144,6 +206,14 @@ The following arguments are supported:
(Optional)
URL to a VpnTunnel that should handle matching packets.

* `next_hop_ilb` -
(Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html))
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 3c556c9

Please sign in to comment.