Skip to content

Commit 3488f65

Browse files
google_compute_service_attachment: allow use of global target forwarding rules (#14686) (#23892)
[upstream:870a3adaafd655859b73627e12c3f439bf4fa394] Signed-off-by: Modular Magician <[email protected]>
1 parent 61526d2 commit 3488f65

File tree

4 files changed

+195
-8
lines changed

4 files changed

+195
-8
lines changed

.changelog/14686.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unknown: google_compute_service_attachment: allow use of global target forwarding rules

google/services/compute/resource_compute_service_attachment.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -920,14 +920,6 @@ func expandComputeServiceAttachmentTargetService(v interface{}, d tpgresource.Te
920920
return nil, fmt.Errorf("invalid value for target_service")
921921
}
922922

923-
resourceKind := resource[len(resource)-2]
924-
resourceBound := resource[len(resource)-4]
925-
926-
_, err := tpgresource.ParseRegionalFieldValue(resourceKind, v.(string), "project", resourceBound, "zone", d, config, true)
927-
if err != nil {
928-
return nil, fmt.Errorf("invalid value for target_service: %w", err)
929-
}
930-
931923
return v, nil
932924
}
933925

google/services/compute/resource_compute_service_attachment_generated_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,113 @@ resource "google_compute_subnetwork" "psc_ilb_nat" {
482482
`, context)
483483
}
484484

485+
func TestAccComputeServiceAttachment_serviceAttachmentCrossRegionIlbExample(t *testing.T) {
486+
t.Parallel()
487+
488+
context := map[string]interface{}{
489+
"random_suffix": acctest.RandString(t, 10),
490+
}
491+
492+
acctest.VcrTest(t, resource.TestCase{
493+
PreCheck: func() { acctest.AccTestPreCheck(t) },
494+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
495+
CheckDestroy: testAccCheckComputeServiceAttachmentDestroyProducer(t),
496+
Steps: []resource.TestStep{
497+
{
498+
Config: testAccComputeServiceAttachment_serviceAttachmentCrossRegionIlbExample(context),
499+
},
500+
{
501+
ResourceName: "google_compute_service_attachment.psc_ilb_service_attachment",
502+
ImportState: true,
503+
ImportStateVerify: true,
504+
ImportStateVerifyIgnore: []string{"region"},
505+
},
506+
},
507+
})
508+
}
509+
510+
func testAccComputeServiceAttachment_serviceAttachmentCrossRegionIlbExample(context map[string]interface{}) string {
511+
return acctest.Nprintf(`
512+
resource "google_compute_service_attachment" "psc_ilb_service_attachment" {
513+
name = "sa%{random_suffix}"
514+
region = "us-central1"
515+
description = "A service attachment configured with Terraform"
516+
connection_preference = "ACCEPT_AUTOMATIC"
517+
enable_proxy_protocol = false
518+
nat_subnets = [google_compute_subnetwork.subnetwork_psc.id]
519+
target_service = google_compute_global_forwarding_rule.forwarding_rule.id
520+
}
521+
522+
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
523+
name = "sa%{random_suffix}"
524+
target = google_compute_target_http_proxy.http_proxy.id
525+
network = google_compute_network.network.id
526+
subnetwork = google_compute_subnetwork.subnetwork.id
527+
port_range = "80"
528+
load_balancing_scheme = "INTERNAL_MANAGED"
529+
530+
depends_on = [google_compute_subnetwork.subnetwork_proxy]
531+
}
532+
533+
resource "google_compute_target_http_proxy" "http_proxy" {
534+
name = "sa%{random_suffix}"
535+
description = "a description"
536+
url_map = google_compute_url_map.url_map.id
537+
}
538+
539+
resource "google_compute_url_map" "url_map" {
540+
name = "sa%{random_suffix}"
541+
description = "Url map."
542+
default_service = google_compute_backend_service.backend_service.id
543+
}
544+
545+
resource "google_compute_backend_service" "backend_service" {
546+
name = "sa%{random_suffix}"
547+
load_balancing_scheme = "INTERNAL_MANAGED"
548+
health_checks = [google_compute_health_check.health_check.id]
549+
}
550+
551+
resource "google_compute_health_check" "health_check" {
552+
name = "sa%{random_suffix}"
553+
check_interval_sec = 1
554+
timeout_sec = 1
555+
556+
tcp_health_check {
557+
port = "80"
558+
}
559+
}
560+
561+
resource "google_compute_subnetwork" "subnetwork_psc" {
562+
name = "sa%{random_suffix}-psc"
563+
region = "us-central1"
564+
network = google_compute_network.network.id
565+
purpose = "PRIVATE_SERVICE_CONNECT"
566+
ip_cidr_range = "10.1.0.0/16"
567+
}
568+
569+
resource "google_compute_subnetwork" "subnetwork_proxy" {
570+
name = "sa%{random_suffix}-proxy"
571+
region = "us-central1"
572+
network = google_compute_network.network.id
573+
purpose = "GLOBAL_MANAGED_PROXY"
574+
role = "ACTIVE"
575+
ip_cidr_range = "10.2.0.0/16"
576+
}
577+
578+
resource "google_compute_subnetwork" "subnetwork" {
579+
name = "sa%{random_suffix}"
580+
region = "us-central1"
581+
network = google_compute_network.network.id
582+
ip_cidr_range = "10.0.0.0/16"
583+
}
584+
585+
resource "google_compute_network" "network" {
586+
name = "sa%{random_suffix}"
587+
auto_create_subnetworks = false
588+
}
589+
`, context)
590+
}
591+
485592
func testAccCheckComputeServiceAttachmentDestroyProducer(t *testing.T) func(s *terraform.State) error {
486593
return func(s *terraform.State) error {
487594
for name, rs := range s.RootModule().Resources {

website/docs/r/compute_service_attachment.html.markdown

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,93 @@ resource "google_compute_subnetwork" "psc_ilb_nat" {
402402
ip_cidr_range = "10.1.0.0/16"
403403
}
404404
```
405+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
406+
<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=service_attachment_cross_region_ilb&open_in_editor=main.tf" target="_blank">
407+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
408+
</a>
409+
</div>
410+
## Example Usage - Service Attachment Cross Region Ilb
411+
412+
413+
```hcl
414+
resource "google_compute_service_attachment" "psc_ilb_service_attachment" {
415+
name = "sa"
416+
region = "us-central1"
417+
description = "A service attachment configured with Terraform"
418+
connection_preference = "ACCEPT_AUTOMATIC"
419+
enable_proxy_protocol = false
420+
nat_subnets = [google_compute_subnetwork.subnetwork_psc.id]
421+
target_service = google_compute_global_forwarding_rule.forwarding_rule.id
422+
}
423+
424+
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
425+
name = "sa"
426+
target = google_compute_target_http_proxy.http_proxy.id
427+
network = google_compute_network.network.id
428+
subnetwork = google_compute_subnetwork.subnetwork.id
429+
port_range = "80"
430+
load_balancing_scheme = "INTERNAL_MANAGED"
431+
432+
depends_on = [google_compute_subnetwork.subnetwork_proxy]
433+
}
434+
435+
resource "google_compute_target_http_proxy" "http_proxy" {
436+
name = "sa"
437+
description = "a description"
438+
url_map = google_compute_url_map.url_map.id
439+
}
440+
441+
resource "google_compute_url_map" "url_map" {
442+
name = "sa"
443+
description = "Url map."
444+
default_service = google_compute_backend_service.backend_service.id
445+
}
446+
447+
resource "google_compute_backend_service" "backend_service" {
448+
name = "sa"
449+
load_balancing_scheme = "INTERNAL_MANAGED"
450+
health_checks = [google_compute_health_check.health_check.id]
451+
}
452+
453+
resource "google_compute_health_check" "health_check" {
454+
name = "sa"
455+
check_interval_sec = 1
456+
timeout_sec = 1
457+
458+
tcp_health_check {
459+
port = "80"
460+
}
461+
}
462+
463+
resource "google_compute_subnetwork" "subnetwork_psc" {
464+
name = "sa-psc"
465+
region = "us-central1"
466+
network = google_compute_network.network.id
467+
purpose = "PRIVATE_SERVICE_CONNECT"
468+
ip_cidr_range = "10.1.0.0/16"
469+
}
470+
471+
resource "google_compute_subnetwork" "subnetwork_proxy" {
472+
name = "sa-proxy"
473+
region = "us-central1"
474+
network = google_compute_network.network.id
475+
purpose = "GLOBAL_MANAGED_PROXY"
476+
role = "ACTIVE"
477+
ip_cidr_range = "10.2.0.0/16"
478+
}
479+
480+
resource "google_compute_subnetwork" "subnetwork" {
481+
name = "sa"
482+
region = "us-central1"
483+
network = google_compute_network.network.id
484+
ip_cidr_range = "10.0.0.0/16"
485+
}
486+
487+
resource "google_compute_network" "network" {
488+
name = "sa"
489+
auto_create_subnetworks = false
490+
}
491+
```
405492

406493
## Argument Reference
407494

0 commit comments

Comments
 (0)