Skip to content

Commit ba35bd1

Browse files
Add new resource "Service LB Policy" to the provider (#10730) (#18326)
[upstream:50ad40d6cf618247cab742c7d81e8b8dc8b8b118] Signed-off-by: Modular Magician <[email protected]>
1 parent 6c4ae8c commit ba35bd1

4 files changed

+238
-0
lines changed

google/services/compute/resource_compute_backend_service.go

+29
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,12 @@ alt name matches one of the specified values.`,
10611061
},
10621062
},
10631063
},
1064+
"service_lb_policy": {
1065+
Type: schema.TypeString,
1066+
Optional: true,
1067+
Description: `URL to networkservices.ServiceLbPolicy resource.
1068+
Can only be set if load balancing scheme is EXTERNAL, EXTERNAL_MANAGED, INTERNAL_MANAGED or INTERNAL_SELF_MANAGED and the scope is global.`,
1069+
},
10641070
"session_affinity": {
10651071
Type: schema.TypeString,
10661072
Computed: true,
@@ -1409,6 +1415,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{
14091415
} else if v, ok := d.GetOkExists("log_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
14101416
obj["logConfig"] = logConfigProp
14111417
}
1418+
serviceLbPolicyProp, err := expandComputeBackendServiceServiceLbPolicy(d.Get("service_lb_policy"), d, config)
1419+
if err != nil {
1420+
return err
1421+
} else if v, ok := d.GetOkExists("service_lb_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceLbPolicyProp)) && (ok || !reflect.DeepEqual(v, serviceLbPolicyProp)) {
1422+
obj["serviceLbPolicy"] = serviceLbPolicyProp
1423+
}
14121424

14131425
obj, err = resourceComputeBackendServiceEncoder(d, meta, obj)
14141426
if err != nil {
@@ -1663,6 +1675,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
16631675
if err := d.Set("log_config", flattenComputeBackendServiceLogConfig(res["logConfig"], d, config)); err != nil {
16641676
return fmt.Errorf("Error reading BackendService: %s", err)
16651677
}
1678+
if err := d.Set("service_lb_policy", flattenComputeBackendServiceServiceLbPolicy(res["serviceLbPolicy"], d, config)); err != nil {
1679+
return fmt.Errorf("Error reading BackendService: %s", err)
1680+
}
16661681
if err := d.Set("self_link", tpgresource.ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
16671682
return fmt.Errorf("Error reading BackendService: %s", err)
16681683
}
@@ -1848,6 +1863,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
18481863
} else if v, ok := d.GetOkExists("log_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
18491864
obj["logConfig"] = logConfigProp
18501865
}
1866+
serviceLbPolicyProp, err := expandComputeBackendServiceServiceLbPolicy(d.Get("service_lb_policy"), d, config)
1867+
if err != nil {
1868+
return err
1869+
} else if v, ok := d.GetOkExists("service_lb_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, serviceLbPolicyProp)) {
1870+
obj["serviceLbPolicy"] = serviceLbPolicyProp
1871+
}
18511872

18521873
obj, err = resourceComputeBackendServiceEncoder(d, meta, obj)
18531874
if err != nil {
@@ -3187,6 +3208,10 @@ func flattenComputeBackendServiceLogConfigSampleRate(v interface{}, d *schema.Re
31873208
return v
31883209
}
31893210

3211+
func flattenComputeBackendServiceServiceLbPolicy(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
3212+
return v
3213+
}
3214+
31903215
func expandComputeBackendServiceAffinityCookieTtlSec(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
31913216
return v, nil
31923217
}
@@ -4254,6 +4279,10 @@ func expandComputeBackendServiceLogConfigSampleRate(v interface{}, d tpgresource
42544279
return v, nil
42554280
}
42564281

4282+
func expandComputeBackendServiceServiceLbPolicy(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
4283+
return v, nil
4284+
}
4285+
42574286
func resourceComputeBackendServiceEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
42584287
// The BackendService API's Update / PUT API is badly formed and behaves like
42594288
// a PATCH field for at least IAP. When sent a `null` `iap` field, the API
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package networkservices_test

website/docs/r/compute_backend_service.html.markdown

+5
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ The following arguments are supported:
572572
If logging is enabled, logs will be exported to Stackdriver.
573573
Structure is [documented below](#nested_log_config).
574574

575+
* `service_lb_policy` -
576+
(Optional)
577+
URL to networkservices.ServiceLbPolicy resource.
578+
Can only be set if load balancing scheme is EXTERNAL, EXTERNAL_MANAGED, INTERNAL_MANAGED or INTERNAL_SELF_MANAGED and the scope is global.
579+
575580
* `project` - (Optional) The ID of the project in which the resource belongs.
576581
If it is not provided, the provider project is used.
577582

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: MMv1 ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This file is automatically generated by Magic Modules and manual
9+
# changes will be clobbered when the file is regenerated.
10+
#
11+
# Please read more about how to change this file in
12+
# .github/CONTRIBUTING.md.
13+
#
14+
# ----------------------------------------------------------------------------
15+
subcategory: "Network services"
16+
description: |-
17+
ServiceLbPolicy holds global load balancing and traffic distribution configuration that can be applied to a BackendService.
18+
---
19+
20+
# google_network_services_service_lb_policies
21+
22+
ServiceLbPolicy holds global load balancing and traffic distribution configuration that can be applied to a BackendService.
23+
24+
~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider.
25+
See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources.
26+
27+
28+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
29+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=network_services_service_lb_policies_basic&open_in_editor=main.tf" target="_blank">
30+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
31+
</a>
32+
</div>
33+
## Example Usage - Network Services Service Lb Policies Basic
34+
35+
36+
```hcl
37+
resource "google_network_services_service_lb_policies" "default" {
38+
provider = google-beta
39+
40+
name = "my-lb-policy"
41+
location = "global"
42+
}
43+
```
44+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
45+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=network_services_service_lb_policies_advanced&open_in_editor=main.tf" target="_blank">
46+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
47+
</a>
48+
</div>
49+
## Example Usage - Network Services Service Lb Policies Advanced
50+
51+
52+
```hcl
53+
resource "google_network_services_service_lb_policies" "default" {
54+
provider = google-beta
55+
56+
name = "my-lb-policy"
57+
location = "global"
58+
description = "my description"
59+
load_balancing_algorithm = "SPRAY_TO_REGION"
60+
61+
auto_capacity_drain {
62+
enable = true
63+
}
64+
65+
failover_config {
66+
failover_health_threshold = 70
67+
}
68+
69+
labels = {
70+
foo = "bar"
71+
}
72+
}
73+
74+
resource "google_compute_backend_service" "default" {
75+
provider = google-beta
76+
77+
name = "my-lb-backend"
78+
description = "my description"
79+
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
80+
protocol = "HTTP"
81+
service_lb_policy = "//networkservices.googleapis.com/${google_network_services_service_lb_policies.default.id}"
82+
}
83+
```
84+
85+
## Argument Reference
86+
87+
The following arguments are supported:
88+
89+
90+
* `name` -
91+
(Required)
92+
Name of the ServiceLbPolicy resource. It matches pattern `projects/{project}/locations/{location}/serviceLbPolicies/{service_lb_policy_name}`.
93+
94+
* `location` -
95+
(Required)
96+
The location of the service lb policy.
97+
98+
99+
- - -
100+
101+
102+
* `labels` -
103+
(Optional)
104+
Set of label tags associated with the ServiceLbPolicy resource.
105+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
106+
Please refer to the field `effective_labels` for all of the labels present on the resource.
107+
108+
* `description` -
109+
(Optional)
110+
A free-text description of the resource. Max length 1024 characters.
111+
112+
* `load_balancing_algorithm` -
113+
(Optional)
114+
The type of load balancing algorithm to be used. The default behavior is WATERFALL_BY_REGION.
115+
Possible values are: `SPRAY_TO_REGION`, `SPRAY_TO_WORLD`, `WATERFALL_BY_REGION`, `WATERFALL_BY_ZONE`.
116+
117+
* `auto_capacity_drain` -
118+
(Optional)
119+
Option to specify if an unhealthy MIG/NEG should be considered for global load balancing and traffic routing.
120+
Structure is [documented below](#nested_auto_capacity_drain).
121+
122+
* `failover_config` -
123+
(Optional)
124+
Option to specify health based failover behavior. This is not related to Network load balancer FailoverPolicy.
125+
Structure is [documented below](#nested_failover_config).
126+
127+
* `project` - (Optional) The ID of the project in which the resource belongs.
128+
If it is not provided, the provider project is used.
129+
130+
131+
<a name="nested_auto_capacity_drain"></a>The `auto_capacity_drain` block supports:
132+
133+
* `enable` -
134+
(Optional)
135+
Optional. If set to 'True', an unhealthy MIG/NEG will be set as drained. - An MIG/NEG is considered unhealthy if less than 25% of the instances/endpoints in the MIG/NEG are healthy. - This option will never result in draining more than 50% of the configured IGs/NEGs for the Backend Service.
136+
137+
<a name="nested_failover_config"></a>The `failover_config` block supports:
138+
139+
* `failover_health_threshold` -
140+
(Required)
141+
Optional. The percentage threshold that a load balancer will begin to send traffic to failover backends. If the percentage of endpoints in a MIG/NEG is smaller than this value, traffic would be sent to failover backends if possible. This field should be set to a value between 1 and 99. The default value is 50 for Global external HTTP(S) load balancer (classic) and Proxyless service mesh, and 70 for others.
142+
143+
## Attributes Reference
144+
145+
In addition to the arguments listed above, the following computed attributes are exported:
146+
147+
* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{location}}/serviceLbPolicies/{{name}}`
148+
149+
* `create_time` -
150+
Time the ServiceLbPolicy was created in UTC.
151+
152+
* `update_time` -
153+
Time the ServiceLbPolicy was updated in UTC.
154+
155+
* `terraform_labels` -
156+
The combination of labels configured directly on the resource
157+
and default labels configured on the provider.
158+
159+
* `effective_labels` -
160+
All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.
161+
162+
163+
## Timeouts
164+
165+
This resource provides the following
166+
[Timeouts](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts) configuration options:
167+
168+
- `create` - Default is 30 minutes.
169+
- `update` - Default is 30 minutes.
170+
- `delete` - Default is 30 minutes.
171+
172+
## Import
173+
174+
175+
ServiceLbPolicies can be imported using any of these accepted formats:
176+
177+
* `projects/{{project}}/locations/{{location}}/serviceLbPolicies/{{name}}`
178+
* `{{project}}/{{location}}/{{name}}`
179+
* `{{location}}/{{name}}`
180+
181+
182+
In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import ServiceLbPolicies using one of the formats above. For example:
183+
184+
```tf
185+
import {
186+
id = "projects/{{project}}/locations/{{location}}/serviceLbPolicies/{{name}}"
187+
to = google_network_services_service_lb_policies.default
188+
}
189+
```
190+
191+
When using the [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import), ServiceLbPolicies can be imported using one of the formats above. For example:
192+
193+
```
194+
$ terraform import google_network_services_service_lb_policies.default projects/{{project}}/locations/{{location}}/serviceLbPolicies/{{name}}
195+
$ terraform import google_network_services_service_lb_policies.default {{project}}/{{location}}/{{name}}
196+
$ terraform import google_network_services_service_lb_policies.default {{location}}/{{name}}
197+
```
198+
199+
## User Project Overrides
200+
201+
This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override).

0 commit comments

Comments
 (0)