Skip to content

Commit cb379c9

Browse files
Display nat ip (#15675)
Co-authored-by: Nick Elliot <[email protected]>
1 parent 88dc07e commit cb379c9

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

mmv1/products/compute/ServiceAttachment.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ custom_code:
3939
constants: 'templates/terraform/constants/compute_service_attachment.go.tmpl'
4040
update_encoder: 'templates/terraform/update_encoder/compute_service_attachment.go.tmpl'
4141
encoder: 'templates/terraform/encoders/compute_service_attachment.go.tmpl'
42+
pre_read: 'templates/terraform/pre_read/service_attachment_show_nat_ips.go.tmpl'
4243
sweeper:
4344
url_substitutions:
4445
- region: "us-west2"
@@ -160,6 +161,11 @@ properties:
160161
description: |
161162
The low 64 bits of the PSC service attachment ID.
162163
output: true
164+
- name: 'showNatIps'
165+
type: Boolean
166+
description: |
167+
If true, show NAT IPs of all connected endpoints.
168+
url_param_only: true
163169
- name: 'connectionPreference'
164170
type: String
165171
description: |
@@ -201,6 +207,12 @@ properties:
201207
description: |
202208
The number of consumer Network Connectivity Center spokes that the connected Private Service Connect endpoint has propagated to.
203209
output: true
210+
- name: 'natIps'
211+
type: Array
212+
description: 'The nat IPs of the connected endpoint.'
213+
output: true
214+
item_type:
215+
type: String
204216
- name: 'targetService'
205217
type: String
206218
description: |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if d.Get("show_nat_ips").(bool) {
2+
url += "?showNatIps=true"
3+
}
4+

mmv1/third_party/terraform/services/compute/resource_compute_service_attachment_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,134 @@ resource "google_network_services_gateway" "default" {
513513
}
514514
`, context)
515515
}
516+
517+
func TestAccComputeServiceAttachment_withNatIps(t *testing.T) {
518+
t.Parallel()
519+
520+
context := map[string]interface{}{
521+
"random_suffix": acctest.RandString(t, 10),
522+
}
523+
524+
acctest.VcrTest(t, resource.TestCase{
525+
PreCheck: func() { acctest.AccTestPreCheck(t) },
526+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
527+
CheckDestroy: testAccCheckComputeServiceAttachmentDestroyProducer(t),
528+
Steps: []resource.TestStep{
529+
{
530+
531+
Config: testAccComputeServiceAttachment_withNatIps(context, false),
532+
},
533+
{
534+
Config: testAccComputeServiceAttachment_withNatIps(context, true),
535+
Check: resource.ComposeTestCheckFunc(
536+
resource.TestCheckResourceAttrSet(
537+
"google_compute_service_attachment.psc_ilb_service_attachment", "connected_endpoints.0.nat_ips.0"),
538+
),
539+
},
540+
{
541+
ResourceName: "google_compute_service_attachment.psc_ilb_service_attachment",
542+
ImportState: true,
543+
ImportStateVerify: true,
544+
ImportStateVerifyIgnore: []string{"target_service", "region", "show_nat_ips", "connected_endpoints.0.nat_ips"},
545+
},
546+
{
547+
Config: testAccComputeServiceAttachment_withNatIps(context, false),
548+
Check: resource.ComposeTestCheckFunc(
549+
resource.TestCheckResourceAttr("google_compute_service_attachment.psc_ilb_service_attachment", "show_nat_ips", "false"),
550+
resource.TestCheckResourceAttr("google_compute_service_attachment.psc_ilb_service_attachment", "connected_endpoints.0.nat_ips.#", "0"),
551+
),
552+
},
553+
},
554+
})
555+
}
556+
557+
func testAccComputeServiceAttachment_withNatIps(context map[string]interface{}, showNatIps bool) string {
558+
context["show_nat_ips"] = showNatIps
559+
return acctest.Nprintf(`
560+
resource "google_compute_service_attachment" "psc_ilb_service_attachment" {
561+
name = "tf-test-my-psc-ilb%{random_suffix}"
562+
region = "us-west2"
563+
description = "A service attachment configured with Terraform"
564+
enable_proxy_protocol = false
565+
connection_preference = "ACCEPT_AUTOMATIC"
566+
show_nat_ips = %{show_nat_ips}
567+
nat_subnets = [google_compute_subnetwork.psc_ilb_nat.id]
568+
target_service = google_compute_forwarding_rule.psc_ilb_target_service.id
569+
}
570+
571+
resource "google_compute_address" "psc_ilb_consumer_address" {
572+
name = "tf-test-psc-ilb-consumer-address%{random_suffix}"
573+
region = "us-west2"
574+
subnetwork = google_compute_subnetwork.psc_ilb_consumer_subnetwork.id
575+
address_type = "INTERNAL"
576+
}
577+
578+
resource "google_compute_forwarding_rule" "psc_ilb_consumer" {
579+
name = "tf-test-psc-ilb-consumer-forwarding-rule%{random_suffix}"
580+
region = "us-west2"
581+
target = google_compute_service_attachment.psc_ilb_service_attachment.id
582+
load_balancing_scheme = ""
583+
network = google_compute_network.consumer_network.name
584+
ip_address = google_compute_address.psc_ilb_consumer_address.id
585+
}
586+
587+
resource "google_compute_forwarding_rule" "psc_ilb_target_service" {
588+
name = "tf-test-producer-forwarding-rule%{random_suffix}"
589+
region = "us-west2"
590+
load_balancing_scheme = "INTERNAL"
591+
backend_service = google_compute_region_backend_service.producer_service_backend.id
592+
all_ports = true
593+
network = google_compute_network.producer_network.name
594+
subnetwork = google_compute_subnetwork.psc_ilb_producer_subnetwork.name
595+
}
596+
597+
resource "google_compute_region_backend_service" "producer_service_backend" {
598+
name = "tf-test-producer-service%{random_suffix}"
599+
region = "us-west2"
600+
health_checks = [google_compute_health_check.producer_service_health_check.id]
601+
}
602+
603+
resource "google_compute_health_check" "producer_service_health_check" {
604+
name = "tf-test-producer-service-health-check%{random_suffix}"
605+
check_interval_sec = 1
606+
timeout_sec = 1
607+
tcp_health_check {
608+
port = "80"
609+
}
610+
}
611+
612+
resource "google_compute_network" "producer_network" {
613+
name = "tf-test-psc-ilb-producer-network%{random_suffix}"
614+
auto_create_subnetworks = false
615+
delete_default_routes_on_create = true
616+
}
617+
618+
resource "google_compute_network" "consumer_network" {
619+
name = "tf-test-psc-ilb-consumer-network%{random_suffix}"
620+
auto_create_subnetworks = false
621+
delete_default_routes_on_create = true
622+
}
623+
624+
resource "google_compute_subnetwork" "psc_ilb_producer_subnetwork" {
625+
name = "tf-test-psc-ilb-producer-subnetwork%{random_suffix}"
626+
region = "us-west2"
627+
network = google_compute_network.producer_network.id
628+
ip_cidr_range = "10.0.0.0/16"
629+
}
630+
631+
resource "google_compute_subnetwork" "psc_ilb_consumer_subnetwork" {
632+
name = "tf-test-psc-ilb-consumer-subnetwork%{random_suffix}"
633+
region = "us-west2"
634+
network = google_compute_network.consumer_network.id
635+
ip_cidr_range = "10.2.0.0/16"
636+
}
637+
638+
resource "google_compute_subnetwork" "psc_ilb_nat" {
639+
name = "tf-test-psc-ilb-nat%{random_suffix}"
640+
region = "us-west2"
641+
network = google_compute_network.producer_network.id
642+
purpose = "PRIVATE_SERVICE_CONNECT"
643+
ip_cidr_range = "10.1.0.0/16"
644+
}
645+
`, context)
646+
}

0 commit comments

Comments
 (0)