Skip to content

Commit

Permalink
Ip address selection policy (#11957) (#19863)
Browse files Browse the repository at this point in the history
[upstream:4c84dbe59bdde7f2e6a485ac0339ebd0772c9ed0]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Oct 15, 2024
1 parent 158cc59 commit 824d665
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11957.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `ipAddressSelectionPolicy` field to `google_compute_backend_service` and `google_compute_region_backend_service`.
```
29 changes: 29 additions & 0 deletions google/services/compute/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,12 @@ For internal load balancing, a URL to a HealthCheck resource must be specified i
},
},
},
"ip_address_selection_policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"IPV4_ONLY", "PREFER_IPV6", "IPV6_ONLY", ""}),
Description: `Specifies preference of traffic to the backend (from the proxy and from the client for proxyless gRPC). Possible values: ["IPV4_ONLY", "PREFER_IPV6", "IPV6_ONLY"]`,
},
"load_balancing_scheme": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1416,6 +1422,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("iap"); ok || !reflect.DeepEqual(v, iapProp) {
obj["iap"] = iapProp
}
ipAddressSelectionPolicyProp, err := expandComputeBackendServiceIpAddressSelectionPolicy(d.Get("ip_address_selection_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_address_selection_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(ipAddressSelectionPolicyProp)) && (ok || !reflect.DeepEqual(v, ipAddressSelectionPolicyProp)) {
obj["ipAddressSelectionPolicy"] = ipAddressSelectionPolicyProp
}
loadBalancingSchemeProp, err := expandComputeBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1721,6 +1733,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
if err := d.Set("iap", flattenComputeBackendServiceIap(res["iap"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("ip_address_selection_policy", flattenComputeBackendServiceIpAddressSelectionPolicy(res["ipAddressSelectionPolicy"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("load_balancing_scheme", flattenComputeBackendServiceLoadBalancingScheme(res["loadBalancingScheme"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
Expand Down Expand Up @@ -1873,6 +1888,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("iap"); ok || !reflect.DeepEqual(v, iapProp) {
obj["iap"] = iapProp
}
ipAddressSelectionPolicyProp, err := expandComputeBackendServiceIpAddressSelectionPolicy(d.Get("ip_address_selection_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_address_selection_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, ipAddressSelectionPolicyProp)) {
obj["ipAddressSelectionPolicy"] = ipAddressSelectionPolicyProp
}
loadBalancingSchemeProp, err := expandComputeBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -2867,6 +2888,10 @@ func flattenComputeBackendServiceIapOauth2ClientSecretSha256(v interface{}, d *s
return v
}

func flattenComputeBackendServiceIpAddressSelectionPolicy(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeBackendServiceLoadBalancingScheme(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -4114,6 +4139,10 @@ func expandComputeBackendServiceIapOauth2ClientSecretSha256(v interface{}, d tpg
return v, nil
}

func expandComputeBackendServiceIpAddressSelectionPolicy(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceLoadBalancingScheme(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,41 @@ resource "google_compute_health_check" "default" {
`, context)
}

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

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

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_backendServiceIpAddressSelectionPolicyExample(context),
},
{
ResourceName: "google_compute_backend_service.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"iap.0.oauth2_client_secret", "security_settings.0.aws_v4_authentication.0.access_key"},
},
},
})
}

func testAccComputeBackendService_backendServiceIpAddressSelectionPolicyExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_backend_service" "default" {
name = "tf-test-backend-service%{random_suffix}"
load_balancing_scheme = "EXTERNAL_MANAGED"
ip_address_selection_policy = "IPV6_ONLY"
}
`, context)
}

func testAccCheckComputeBackendServiceDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
29 changes: 29 additions & 0 deletions google/services/compute/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ or serverless NEG as a backend.`,
},
},
},
"ip_address_selection_policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"IPV4_ONLY", "PREFER_IPV6", "IPV6_ONLY", ""}),
Description: `Specifies preference of traffic to the backend (from the proxy and from the client for proxyless gRPC). Possible values: ["IPV4_ONLY", "PREFER_IPV6", "IPV6_ONLY"]`,
},
"load_balancing_scheme": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1182,6 +1188,12 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("iap"); ok || !reflect.DeepEqual(v, iapProp) {
obj["iap"] = iapProp
}
ipAddressSelectionPolicyProp, err := expandComputeRegionBackendServiceIpAddressSelectionPolicy(d.Get("ip_address_selection_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_address_selection_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(ipAddressSelectionPolicyProp)) && (ok || !reflect.DeepEqual(v, ipAddressSelectionPolicyProp)) {
obj["ipAddressSelectionPolicy"] = ipAddressSelectionPolicyProp
}
loadBalancingSchemeProp, err := expandComputeRegionBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1424,6 +1436,9 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
if err := d.Set("iap", flattenComputeRegionBackendServiceIap(res["iap"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if err := d.Set("ip_address_selection_policy", flattenComputeRegionBackendServiceIpAddressSelectionPolicy(res["ipAddressSelectionPolicy"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if err := d.Set("load_balancing_scheme", flattenComputeRegionBackendServiceLoadBalancingScheme(res["loadBalancingScheme"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
Expand Down Expand Up @@ -1555,6 +1570,12 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("iap"); ok || !reflect.DeepEqual(v, iapProp) {
obj["iap"] = iapProp
}
ipAddressSelectionPolicyProp, err := expandComputeRegionBackendServiceIpAddressSelectionPolicy(d.Get("ip_address_selection_policy"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_address_selection_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, ipAddressSelectionPolicyProp)) {
obj["ipAddressSelectionPolicy"] = ipAddressSelectionPolicyProp
}
loadBalancingSchemeProp, err := expandComputeRegionBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -2462,6 +2483,10 @@ func flattenComputeRegionBackendServiceIapOauth2ClientSecretSha256(v interface{}
return v
}

func flattenComputeRegionBackendServiceIpAddressSelectionPolicy(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeRegionBackendServiceLoadBalancingScheme(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -3576,6 +3601,10 @@ func expandComputeRegionBackendServiceIapOauth2ClientSecretSha256(v interface{},
return v, nil
}

func expandComputeRegionBackendServiceIpAddressSelectionPolicy(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionBackendServiceLoadBalancingScheme(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,54 @@ resource "google_compute_subnetwork" "default" {
`, context)
}

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

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

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeRegionBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionBackendService_regionBackendServiceIpAddressSelectionPolicyExample(context),
},
{
ResourceName: "google_compute_region_backend_service.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"iap.0.oauth2_client_secret", "network", "region"},
},
},
})
}

func testAccComputeRegionBackendService_regionBackendServiceIpAddressSelectionPolicyExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_region_backend_service" "default" {
name = "tf-test-region-service%{random_suffix}"
region = "us-central1"
health_checks = [google_compute_region_health_check.health_check.id]
load_balancing_scheme = "EXTERNAL_MANAGED"
protocol = "HTTP"
ip_address_selection_policy = "IPV6_ONLY"
}
resource "google_compute_region_health_check" "health_check" {
name = "tf-test-rbs-health-check%{random_suffix}"
region = "us-central1"
tcp_health_check {
port = 80
}
}
`, context)
}

func testAccCheckComputeRegionBackendServiceDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
20 changes: 20 additions & 0 deletions website/docs/r/compute_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ resource "google_compute_health_check" "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_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=backend_service_ip_address_selection_policy&open_in_editor=main.tf" 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 - Backend Service Ip Address Selection Policy


```hcl
resource "google_compute_backend_service" "default" {
name = "backend-service"
load_balancing_scheme = "EXTERNAL_MANAGED"
ip_address_selection_policy = "IPV6_ONLY"
}
```

## Argument Reference

Expand Down Expand Up @@ -493,6 +508,11 @@ The following arguments are supported:
Settings for enabling Cloud Identity Aware Proxy
Structure is [documented below](#nested_iap).

* `ip_address_selection_policy` -
(Optional)
Specifies preference of traffic to the backend (from the proxy and from the client for proxyless gRPC).
Possible values are: `IPV4_ONLY`, `PREFER_IPV6`, `IPV6_ONLY`.

* `load_balancing_scheme` -
(Optional)
Indicates whether the backend service will be used with internal or
Expand Down
33 changes: 33 additions & 0 deletions website/docs/r/compute_region_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,34 @@ resource "google_compute_region_health_check" "health_check" {
}
}
```
<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_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=region_backend_service_ip_address_selection_policy&open_in_editor=main.tf" 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 - Region Backend Service Ip Address Selection Policy


```hcl
resource "google_compute_region_backend_service" "default" {
name = "region-service"
region = "us-central1"
health_checks = [google_compute_region_health_check.health_check.id]
load_balancing_scheme = "EXTERNAL_MANAGED"
protocol = "HTTP"
ip_address_selection_policy = "IPV6_ONLY"
}
resource "google_compute_region_health_check" "health_check" {
name = "rbs-health-check"
region = "us-central1"
tcp_health_check {
port = 80
}
}
```

## Argument Reference

Expand Down Expand Up @@ -488,6 +516,11 @@ The following arguments are supported:
Settings for enabling Cloud Identity Aware Proxy
Structure is [documented below](#nested_iap).

* `ip_address_selection_policy` -
(Optional)
Specifies preference of traffic to the backend (from the proxy and from the client for proxyless gRPC).
Possible values are: `IPV4_ONLY`, `PREFER_IPV6`, `IPV6_ONLY`.

* `load_balancing_scheme` -
(Optional)
Indicates what kind of load balancing this regional backend service
Expand Down

0 comments on commit 824d665

Please sign in to comment.