@@ -106,6 +106,155 @@ func TestAccComputeServiceAttachment_serviceAttachmentConnectedEndpointsOutput(t
106106 })
107107}
108108
109+ func TestAccComputeServiceAttachment_targetServiceInPlaceUpdate (t * testing.T ) {
110+ t .Parallel ()
111+
112+ context := map [string ]interface {}{
113+ "random_suffix" : acctest .RandString (t , 10 ),
114+ }
115+
116+ acctest .VcrTest (t , resource.TestCase {
117+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
118+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
119+ CheckDestroy : testAccCheckComputeServiceAttachmentDestroyProducer (t ),
120+ Steps : []resource.TestStep {
121+ {
122+ // Initial Creation of service attachment with target as L4 ILB
123+ Config : testAccComputeServiceAttachment_targetServiceUpdate (context , "l4ilb" ),
124+ Check : resource .ComposeTestCheckFunc (
125+ // Verify the service attachment was created
126+ resource .TestCheckResourceAttrSet ("google_compute_service_attachment.psc_attachment" , "self_link" ),
127+ ),
128+ },
129+ {
130+ // Update to target to L7 ILB
131+ Config : testAccComputeServiceAttachment_targetServiceUpdate (context , "l7ilb" ),
132+ ConfigPlanChecks : resource.ConfigPlanChecks {
133+ PreApply : []plancheck.PlanCheck {
134+ plancheck .ExpectNonEmptyPlan (),
135+ },
136+ },
137+ Check : resource .ComposeTestCheckFunc (
138+ // Verify the service attachment still exists (in-place update, not recreated)
139+ resource .TestCheckResourceAttrSet ("google_compute_service_attachment.psc_attachment" , "self_link" ),
140+ ),
141+ },
142+ {
143+ ResourceName : "google_compute_service_attachment.psc_attachment" ,
144+ ImportState : true ,
145+ ImportStateVerify : true ,
146+ ImportStateVerifyIgnore : []string {"target_service" , "region" },
147+ },
148+ },
149+ })
150+ }
151+
152+ func testAccComputeServiceAttachment_targetServiceUpdate (context map [string ]interface {}, targetKey string ) string {
153+ var targetService string
154+ if targetKey == "l4ilb" {
155+ targetService = "google_compute_forwarding_rule.producer_l4_ilb_fr.id"
156+ } else {
157+ targetService = "google_compute_forwarding_rule.producer_l7_ilb_forwarding_rule.id"
158+ }
159+ context ["target_service" ] = targetService
160+ return acctest .Nprintf (`
161+ resource "google_compute_network" "psc_ilb_network" {
162+ name = "tf-test-producer-net-%{random_suffix}"
163+ auto_create_subnetworks = false
164+ }
165+
166+ resource "google_compute_subnetwork" "psc_ilb_subnet" {
167+ name = "tf-test-subnet-%{random_suffix}"
168+ ip_cidr_range = "10.0.0.0/16"
169+ network = google_compute_network.psc_ilb_network.id
170+ region = "us-west2"
171+ }
172+
173+ resource "google_compute_subnetwork" "psc_ilb_nat_subnet" {
174+ name = "tf-test-nat-%{random_suffix}"
175+ region = "us-west2"
176+ network = google_compute_network.psc_ilb_network.id
177+ purpose = "PRIVATE_SERVICE_CONNECT"
178+ ip_cidr_range = "10.1.0.0/24"
179+ }
180+
181+ resource "google_compute_subnetwork" "proxy_only_subnet" {
182+ name = "tf-test-proxy-only-subnet-%{random_suffix}"
183+ ip_cidr_range = "10.5.0.0/24"
184+ purpose = "REGIONAL_MANAGED_PROXY"
185+ role = "ACTIVE"
186+ network = google_compute_network.psc_ilb_network.id
187+ region = "us-west2"
188+ }
189+
190+ resource "google_compute_health_check" "hc" {
191+ name = "tf-test-hc-%{random_suffix}"
192+ tcp_health_check {
193+ port = "80"
194+ }
195+ }
196+
197+ # Forwarding Rule A
198+ resource "google_compute_region_backend_service" "producer_l4_ilb_backend_service" {
199+ name = "tf-test-l4-ilb-backend-service-%{random_suffix}"
200+ region = "us-west2"
201+ health_checks = [google_compute_health_check.hc.id]
202+ }
203+
204+ resource "google_compute_forwarding_rule" "producer_l4_ilb_fr" {
205+ name = "tf-test-l4ilb-fr-%{random_suffix}"
206+ region = "us-west2"
207+ load_balancing_scheme = "INTERNAL"
208+ backend_service = google_compute_region_backend_service.producer_l4_ilb_backend_service.id
209+ all_ports = true
210+ network = google_compute_network.psc_ilb_network.name
211+ subnetwork = google_compute_subnetwork.psc_ilb_subnet.name
212+ }
213+
214+ # Producer L7 ILB
215+ resource "google_compute_region_backend_service" "producer_l7_ilb_backend_service" {
216+ name = "tf-test-l7-ilb-backend-service%{random_suffix}"
217+ load_balancing_scheme = "INTERNAL_MANAGED"
218+ region = "us-west2"
219+ health_checks = [google_compute_health_check.hc.id]
220+ }
221+
222+ resource "google_compute_region_url_map" "producer_url_map" {
223+ name = "producer-l7-ilb-url-map-%{random_suffix}"
224+ default_service = google_compute_region_backend_service.producer_l7_ilb_backend_service.id
225+ region = "us-west2"
226+ }
227+
228+ resource "google_compute_region_target_http_proxy" "producer_proxy" {
229+ name = "producer-l7-ilb-proxy-%{random_suffix}"
230+ url_map = google_compute_region_url_map.producer_url_map.id
231+ region = "us-west2"
232+ }
233+
234+ resource "google_compute_forwarding_rule" "producer_l7_ilb_forwarding_rule" {
235+ name = "tf-test-fr-l7-ilb-fr-%{random_suffix}"
236+ region = "us-west2"
237+ load_balancing_scheme = "INTERNAL_MANAGED"
238+ port_range = "8080"
239+ target = google_compute_region_target_http_proxy.producer_proxy.id
240+ network = google_compute_network.psc_ilb_network.name
241+ subnetwork = google_compute_subnetwork.psc_ilb_subnet.name
242+ depends_on = [google_compute_subnetwork.proxy_only_subnet]
243+ }
244+
245+ resource "google_compute_service_attachment" "psc_attachment" {
246+ name = "tf-test-sa-%{random_suffix}"
247+ region = "us-west2"
248+ enable_proxy_protocol = false
249+ connection_preference = "ACCEPT_AUTOMATIC"
250+ nat_subnets = [google_compute_subnetwork.psc_ilb_nat_subnet.id]
251+
252+ # Dynamically switch the target based on the test step
253+ target_service = %{target_service}
254+ }
255+ ` , context )
256+ }
257+
109258func testAccComputeServiceAttachment_serviceAttachmentBasicExampleFork (context map [string ]interface {}) string {
110259 return acctest .Nprintf (`
111260resource "google_compute_service_attachment" "psc_ilb_service_attachment" {
0 commit comments