Skip to content

Commit

Permalink
Add purpose and role to subnetwork for L7 load balancing (hashicorp#1051
Browse files Browse the repository at this point in the history
)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and Ty Larrabee committed Sep 3, 2019
1 parent a15637d commit ce80031
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
54 changes: 53 additions & 1 deletion google-beta/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,24 @@ func resourceComputeSubnetwork() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"purpose": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"INTERNAL_HTTPS_LOAD_BALANCER", "PRIVATE_RFC_1918", "PRIVATE", ""}, false),
},
"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"role": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "BACKUP", ""}, false),
},
"secondary_ip_range": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -275,6 +286,18 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("fingerprint"); !isEmptyValue(reflect.ValueOf(fingerprintProp)) && (ok || !reflect.DeepEqual(v, fingerprintProp)) {
obj["fingerprint"] = fingerprintProp
}
purposeProp, err := expandComputeSubnetworkPurpose(d.Get("purpose"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("purpose"); !isEmptyValue(reflect.ValueOf(purposeProp)) && (ok || !reflect.DeepEqual(v, purposeProp)) {
obj["purpose"] = purposeProp
}
roleProp, err := expandComputeSubnetworkRole(d.Get("role"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("role"); !isEmptyValue(reflect.ValueOf(roleProp)) && (ok || !reflect.DeepEqual(v, roleProp)) {
obj["role"] = roleProp
}
secondaryIpRangesProp, err := expandComputeSubnetworkSecondaryIpRange(d.Get("secondary_ip_range"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -388,6 +411,12 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("fingerprint", flattenComputeSubnetworkFingerprint(res["fingerprint"], d)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("purpose", flattenComputeSubnetworkPurpose(res["purpose"], d)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("role", flattenComputeSubnetworkRole(res["role"], d)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("secondary_ip_range", flattenComputeSubnetworkSecondaryIpRange(res["secondaryIpRanges"], d)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
Expand Down Expand Up @@ -451,7 +480,7 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e

d.SetPartial("ip_cidr_range")
}
if d.HasChange("enable_flow_logs") || d.HasChange("fingerprint") || d.HasChange("secondary_ip_range") {
if d.HasChange("enable_flow_logs") || d.HasChange("fingerprint") || d.HasChange("role") || d.HasChange("secondary_ip_range") {
obj := make(map[string]interface{})
enableFlowLogsProp, err := expandComputeSubnetworkEnableFlowLogs(d.Get("enable_flow_logs"), d, config)
if err != nil {
Expand All @@ -465,6 +494,12 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("fingerprint"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, fingerprintProp)) {
obj["fingerprint"] = fingerprintProp
}
roleProp, err := expandComputeSubnetworkRole(d.Get("role"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("role"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, roleProp)) {
obj["role"] = roleProp
}
secondaryIpRangesProp, err := expandComputeSubnetworkSecondaryIpRange(d.Get("secondary_ip_range"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -497,6 +532,7 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e

d.SetPartial("enable_flow_logs")
d.SetPartial("fingerprint")
d.SetPartial("role")
d.SetPartial("secondary_ip_range")
}
if d.HasChange("private_ip_google_access") {
Expand Down Expand Up @@ -634,6 +670,14 @@ func flattenComputeSubnetworkFingerprint(v interface{}, d *schema.ResourceData)
return v
}

func flattenComputeSubnetworkPurpose(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeSubnetworkRole(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeSubnetworkSecondaryIpRange(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -729,6 +773,14 @@ func expandComputeSubnetworkFingerprint(v interface{}, d TerraformResourceData,
return v, nil
}

func expandComputeSubnetworkPurpose(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkRole(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkSecondaryIpRange(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand Down
44 changes: 44 additions & 0 deletions google-beta/resource_compute_subnetwork_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,50 @@ provider "google-beta"{
`, context)
}

func TestAccComputeSubnetwork_subnetworkInternalL7lbExample(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: testAccCheckComputeSubnetworkDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeSubnetwork_subnetworkInternalL7lbExample(context),
},
},
})
}

func testAccComputeSubnetwork_subnetworkInternalL7lbExample(context map[string]interface{}) string {
return Nprintf(`
provider "google-beta" {
region = "us-central1"
zone = "us-central1-a"
}
resource "google_compute_subnetwork" "network-for-l7lb" {
provider = "google-beta"
name = "l7lb-test-subnetwork%{random_suffix}"
ip_cidr_range = "10.0.0.0/22"
region = "us-central1"
purpose = "INTERNAL_HTTPS_LOAD_BALANCER"
role = "ACTIVE"
network = "${google_compute_network.custom-test.self_link}"
}
resource "google_compute_network" "custom-test" {
provider = "google-beta"
name = "l7lb-test-network%{random_suffix}"
auto_create_subnetworks = false
}
`, context)
}

func testAccCheckComputeSubnetworkDestroy(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_subnetwork" {
Expand Down
46 changes: 46 additions & 0 deletions website/docs/r/compute_subnetwork.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ provider "google-beta"{
zone = "us-central1-a"
}
```
<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=subnetwork_internal_l7lb&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 - Subnetwork Internal L7lb


```hcl
provider "google-beta" {
region = "us-central1"
zone = "us-central1-a"
}
resource "google_compute_subnetwork" "network-for-l7lb" {
provider = "google-beta"
name = "l7lb-test-subnetwork"
ip_cidr_range = "10.0.0.0/22"
region = "us-central1"
purpose = "INTERNAL_HTTPS_LOAD_BALANCER"
role = "ACTIVE"
network = "${google_compute_network.custom-test.self_link}"
}
resource "google_compute_network" "custom-test" {
provider = "google-beta"
name = "l7lb-test-network"
auto_create_subnetworks = false
}
```

## Argument Reference

Expand Down Expand Up @@ -155,6 +185,22 @@ The following arguments are supported:
(Optional)
Whether to enable flow logging for this subnetwork.

* `purpose` -
(Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html))
The purpose of the resource. This field can be either PRIVATE_RFC_1918
or INTERNAL_HTTPS_LOAD_BALANCER. A subnetwork with purpose set to
INTERNAL_HTTPS_LOAD_BALANCER is a user-created subnetwork that is
reserved for Internal HTTP(S) Load Balancing.
If set to INTERNAL_HTTPS_LOAD_BALANCER you must also set the role.

* `role` -
(Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html))
The role of subnetwork. Currenly, this field is only used
when purpose = INTERNAL_HTTPS_LOAD_BALANCER. The value can be set
to ACTIVE or BACKUP. An ACTIVE subnetwork is one that is currently
being used for Internal HTTP(S) Load Balancing. A BACKUP subnetwork
is one that is ready to be promoted to ACTIVE or is currently draining.

* `secondary_ip_range` -
(Optional)
An array of configurations for secondary IP ranges for VM instances
Expand Down

0 comments on commit ce80031

Please sign in to comment.