Skip to content

Commit 31b90ed

Browse files
modular-magicianslevenick
authored andcommitted
Backend service support for internet NEG backend (#3782) (#2304)
* Add ability to set global network endpoint group as backend for backend service. Make health_checks optional * PR fixes * Add encoder to remove max_utilization when neg backend * Check for global NEG in group to remove max_utilization * Add another nil check * Spacing * Docs fix Signed-off-by: Modular Magician <[email protected]>
1 parent c906750 commit 31b90ed

File tree

4 files changed

+134
-22
lines changed

4 files changed

+134
-22
lines changed

.changelog/3782.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: Added support to `google_compute_backend_service` for setting a network endpoint group as `backend.group`
3+
```

google-beta/resource_compute_backend_service.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"log"
2121
"reflect"
2222
"strconv"
23+
"strings"
2324
"time"
2425

2526
"github.com/hashicorp/errwrap"
@@ -163,21 +164,6 @@ func resourceComputeBackendService() *schema.Resource {
163164
SchemaVersion: 1,
164165

165166
Schema: map[string]*schema.Schema{
166-
"health_checks": {
167-
Type: schema.TypeSet,
168-
Required: true,
169-
Description: `The set of URLs to the HttpHealthCheck or HttpsHealthCheck resource
170-
for health checking this BackendService. Currently at most one health
171-
check can be specified, and a health check is required.
172-
173-
For internal load balancing, a URL to a HealthCheck resource must be specified instead.`,
174-
MinItems: 1,
175-
MaxItems: 1,
176-
Elem: &schema.Schema{
177-
Type: schema.TypeString,
178-
},
179-
Set: selfLinkRelativePathHash,
180-
},
181167
"name": {
182168
Type: schema.TypeString,
183169
Required: true,
@@ -491,6 +477,23 @@ requests.`,
491477
Optional: true,
492478
Description: `If true, enable Cloud CDN for this BackendService.`,
493479
},
480+
"health_checks": {
481+
Type: schema.TypeSet,
482+
Optional: true,
483+
Description: `The set of URLs to the HttpHealthCheck or HttpsHealthCheck resource
484+
for health checking this BackendService. Currently at most one health
485+
check can be specified.
486+
487+
A health check must be specified unless the backend service uses an internet NEG as a backend.
488+
489+
For internal load balancing, a URL to a HealthCheck resource must be specified instead.`,
490+
MinItems: 1,
491+
MaxItems: 1,
492+
Elem: &schema.Schema{
493+
Type: schema.TypeString,
494+
},
495+
Set: selfLinkRelativePathHash,
496+
},
494497
"iap": {
495498
Type: schema.TypeList,
496499
Optional: true,
@@ -3245,6 +3248,24 @@ func resourceComputeBackendServiceEncoder(d *schema.ResourceData, meta interface
32453248
obj["iap"] = iap
32463249
}
32473250

3251+
backendsRaw, ok := obj["backends"]
3252+
if !ok {
3253+
return obj, nil
3254+
}
3255+
backends := backendsRaw.([]interface{})
3256+
for _, backendRaw := range backends {
3257+
backend := backendRaw.(map[string]interface{})
3258+
backendGroup, ok := backend["group"]
3259+
if !ok {
3260+
continue
3261+
}
3262+
if strings.Contains(backendGroup.(string), "global/networkEndpointGroups") {
3263+
// Remove `max_utilization` from any backend that belongs to a global NEG. This field
3264+
// has a default value and causes API validation errors
3265+
backend["maxUtilization"] = nil
3266+
}
3267+
}
3268+
32483269
return obj, nil
32493270
}
32503271

google-beta/resource_compute_backend_service_generated_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,59 @@ resource "google_compute_health_check" "health_check" {
161161
`, context)
162162
}
163163

164+
func TestAccComputeBackendService_backendServiceNetworkEndpointExample(t *testing.T) {
165+
t.Parallel()
166+
167+
context := map[string]interface{}{
168+
"random_suffix": randString(t, 10),
169+
}
170+
171+
vcrTest(t, resource.TestCase{
172+
PreCheck: func() { testAccPreCheck(t) },
173+
Providers: testAccProviders,
174+
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
175+
Steps: []resource.TestStep{
176+
{
177+
Config: testAccComputeBackendService_backendServiceNetworkEndpointExample(context),
178+
},
179+
{
180+
ResourceName: "google_compute_backend_service.default",
181+
ImportState: true,
182+
ImportStateVerify: true,
183+
},
184+
},
185+
})
186+
}
187+
188+
func testAccComputeBackendService_backendServiceNetworkEndpointExample(context map[string]interface{}) string {
189+
return Nprintf(`
190+
resource "google_compute_global_network_endpoint_group" "external_proxy" {
191+
name = "tf-test-network-endpoint%{random_suffix}"
192+
network_endpoint_type = "INTERNET_FQDN_PORT"
193+
default_port = "443"
194+
}
195+
196+
resource "google_compute_global_network_endpoint" "proxy" {
197+
global_network_endpoint_group = google_compute_global_network_endpoint_group.external_proxy.id
198+
fqdn = "test.example.com"
199+
port = google_compute_global_network_endpoint_group.external_proxy.default_port
200+
}
201+
202+
resource "google_compute_backend_service" "default" {
203+
name = "tf-test-backend-service%{random_suffix}"
204+
enable_cdn = true
205+
timeout_sec = 10
206+
connection_draining_timeout_sec = 10
207+
208+
custom_request_headers = ["host: ${google_compute_global_network_endpoint.proxy.fqdn}"]
209+
210+
backend {
211+
group = google_compute_global_network_endpoint_group.external_proxy.id
212+
}
213+
}
214+
`, context)
215+
}
216+
164217
func testAccCheckComputeBackendServiceDestroyProducer(t *testing.T) func(s *terraform.State) error {
165218
return func(s *terraform.State) error {
166219
for name, rs := range s.RootModule().Resources {

website/docs/r/compute_backend_service.html.markdown

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,46 @@ resource "google_compute_health_check" "health_check" {
131131
}
132132
}
133133
```
134+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
135+
<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=backend_service_network_endpoint&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
136+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
137+
</a>
138+
</div>
139+
## Example Usage - Backend Service Network Endpoint
140+
141+
142+
```hcl
143+
resource "google_compute_global_network_endpoint_group" "external_proxy" {
144+
name = "network-endpoint"
145+
network_endpoint_type = "INTERNET_FQDN_PORT"
146+
default_port = "443"
147+
}
148+
149+
resource "google_compute_global_network_endpoint" "proxy" {
150+
global_network_endpoint_group = google_compute_global_network_endpoint_group.external_proxy.id
151+
fqdn = "test.example.com"
152+
port = google_compute_global_network_endpoint_group.external_proxy.default_port
153+
}
154+
155+
resource "google_compute_backend_service" "default" {
156+
name = "backend-service"
157+
enable_cdn = true
158+
timeout_sec = 10
159+
connection_draining_timeout_sec = 10
160+
161+
custom_request_headers = ["host: ${google_compute_global_network_endpoint.proxy.fqdn}"]
162+
163+
backend {
164+
group = google_compute_global_network_endpoint_group.external_proxy.id
165+
}
166+
}
167+
```
134168

135169
## Argument Reference
136170

137171
The following arguments are supported:
138172

139173

140-
* `health_checks` -
141-
(Required)
142-
The set of URLs to the HttpHealthCheck or HttpsHealthCheck resource
143-
for health checking this BackendService. Currently at most one health
144-
check can be specified, and a health check is required.
145-
For internal load balancing, a URL to a HealthCheck resource must be specified instead.
146-
147174
* `name` -
148175
(Required)
149176
Name of the resource. Provided by the client when the resource is
@@ -208,6 +235,14 @@ The following arguments are supported:
208235
(Optional)
209236
If true, enable Cloud CDN for this BackendService.
210237

238+
* `health_checks` -
239+
(Optional)
240+
The set of URLs to the HttpHealthCheck or HttpsHealthCheck resource
241+
for health checking this BackendService. Currently at most one health
242+
check can be specified.
243+
A health check must be specified unless the backend service uses an internet NEG as a backend.
244+
For internal load balancing, a URL to a HealthCheck resource must be specified instead.
245+
211246
* `iap` -
212247
(Optional)
213248
Settings for enabling Cloud Identity Aware Proxy Structure is documented below.

0 commit comments

Comments
 (0)