Skip to content

Commit

Permalink
Add support for L4 ILB subsetting policy (#5944) (#4246)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Apr 21, 2022
1 parent fc91610 commit 2760377
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5944.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `subsetting` field to `google_compute_region_backend_service` (beta)
```
95 changes: 95 additions & 0 deletions google-beta/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,22 @@ If it is not provided, the provider region is used.`,
Description: `Type of session affinity to use. The default is NONE. Session affinity is
not applicable if the protocol is UDP. Possible values: ["NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE", "CLIENT_IP_NO_DESTINATION"]`,
},
"subsetting": {
Type: schema.TypeList,
Optional: true,
Description: `Subsetting configuration for this BackendService. Currently this is applicable only for Internal TCP/UDP load balancing and Internal HTTP(S) load balancing.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"policy": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateEnum([]string{"CONSISTENT_HASH_SUBSETTING"}),
Description: `The algorithm used for subsetting. Possible values: ["CONSISTENT_HASH_SUBSETTING"]`,
},
},
},
},
"timeout_sec": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -1242,6 +1258,12 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
subsettingProp, err := expandComputeRegionBackendServiceSubsetting(d.Get("subsetting"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("subsetting"); !isEmptyValue(reflect.ValueOf(subsettingProp)) && (ok || !reflect.DeepEqual(v, subsettingProp)) {
obj["subsetting"] = subsettingProp
}
regionProp, err := expandComputeRegionBackendServiceRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1430,6 +1452,9 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
if err := d.Set("network", flattenComputeRegionBackendServiceNetwork(res["network"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if err := d.Set("subsetting", flattenComputeRegionBackendServiceSubsetting(res["subsetting"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if err := d.Set("region", flattenComputeRegionBackendServiceRegion(res["region"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
Expand Down Expand Up @@ -1594,6 +1619,12 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
subsettingProp, err := expandComputeRegionBackendServiceSubsetting(d.Get("subsetting"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("subsetting"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, subsettingProp)) {
obj["subsetting"] = subsettingProp
}
regionProp, err := expandComputeRegionBackendServiceRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -2851,6 +2882,23 @@ func flattenComputeRegionBackendServiceNetwork(v interface{}, d *schema.Resource
return ConvertSelfLinkToV1(v.(string))
}

func flattenComputeRegionBackendServiceSubsetting(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["policy"] =
flattenComputeRegionBackendServiceSubsettingPolicy(original["policy"], d, config)
return []interface{}{transformed}
}
func flattenComputeRegionBackendServiceSubsettingPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeRegionBackendServiceRegion(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -3874,6 +3922,29 @@ func expandComputeRegionBackendServiceNetwork(v interface{}, d TerraformResource
return f.RelativeLink(), nil
}

func expandComputeRegionBackendServiceSubsetting(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedPolicy, err := expandComputeRegionBackendServiceSubsettingPolicy(original["policy"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPolicy); val.IsValid() && !isEmptyValue(val) {
transformed["policy"] = transformedPolicy
}

return transformed, nil
}

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

func expandComputeRegionBackendServiceRegion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
if err != nil {
Expand Down Expand Up @@ -3907,6 +3978,19 @@ func resourceComputeRegionBackendServiceEncoder(d *schema.ResourceData, meta int
return obj, nil
}

// To remove subsetting on an ILB, "NONE" must be specified. If subsetting
// isn't specified, we set the value to NONE to make this use case work.
_, ok := obj["subsetting"]
if !ok {
loadBalancingScheme, ok := obj["loadBalancingScheme"]
// External load balancing scheme does not support subsetting
if !ok || loadBalancingScheme.(string) != "EXTERNAL" {
data := map[string]interface{}{}
data["policy"] = "NONE"
obj["subsetting"] = data
}
}

backendServiceOnlyManagedApiFieldNames := []string{
"capacityScaler",
"maxConnections",
Expand Down Expand Up @@ -3953,6 +4037,17 @@ func resourceComputeRegionBackendServiceDecoder(d *schema.ResourceData, meta int
delete(res, "iap")
}

// Since we add in a NONE subsetting policy, we need to remove it in some
// cases for backwards compatibility with the config
v, ok = res["subsetting"]
if ok && v != nil {
subsetting := v.(map[string]interface{})
policy, ok := subsetting["policy"]
if ok && policy == "NONE" {
delete(res, "subsetting")
}
}

// Requests with consistentHash will error for specific values of
// localityLbPolicy. However, the API will not remove it if the backend
// service is updated to from supporting to non-supporting localityLbPolicy
Expand Down
72 changes: 72 additions & 0 deletions google-beta/resource_compute_region_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,39 @@ func TestAccComputeRegionBackendService_withBackendAndIAP(t *testing.T) {
})
}

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

randString := randString(t, 10)

backendName := fmt.Sprintf("foo-%s", randString)
checkName := fmt.Sprintf("bar-%s", randString)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRegionBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionBackendService_ilbWithSubsetting(backendName, checkName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRegionBackendService_ilbNoSubsetting(backendName, checkName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeRegionBackendService_ilbBasic(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
Expand Down Expand Up @@ -874,3 +907,42 @@ resource "google_compute_health_check" "health_check" {
}
`, serviceName, checkName)
}

func testAccComputeRegionBackendService_ilbWithSubsetting(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
name = "%s"
health_checks = [google_compute_health_check.health_check.self_link]
protocol = "TCP"
load_balancing_scheme = "INTERNAL"
subsetting {
policy = "CONSISTENT_HASH_SUBSETTING"
}
}
resource "google_compute_health_check" "health_check" {
name = "%s"
http_health_check {
port = 80
}
}
`, serviceName, checkName)
}

func testAccComputeRegionBackendService_ilbNoSubsetting(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
name = "%s"
health_checks = [google_compute_health_check.health_check.self_link]
protocol = "TCP"
load_balancing_scheme = "INTERNAL"
}
resource "google_compute_health_check" "health_check" {
name = "%s"
http_health_check {
port = 80
}
}
`, serviceName, checkName)
}
12 changes: 12 additions & 0 deletions website/docs/r/compute_region_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ The following arguments are supported:
The URL of the network to which this backend service belongs.
This field can only be specified when the load balancing scheme is set to INTERNAL.

* `subsetting` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
Subsetting configuration for this BackendService. Currently this is applicable only for Internal TCP/UDP load balancing and Internal HTTP(S) load balancing.
Structure is [documented below](#nested_subsetting).

* `region` -
(Optional)
The Region in which the created backend service should reside.
Expand Down Expand Up @@ -1019,6 +1024,13 @@ The following arguments are supported:
where 1.0 means all logged requests are reported and 0.0 means no logged requests are reported.
The default value is 1.0.

<a name="nested_subsetting"></a>The `subsetting` block supports:

* `policy` -
(Required)
The algorithm used for subsetting.
Possible values are `CONSISTENT_HASH_SUBSETTING`.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down

0 comments on commit 2760377

Please sign in to comment.