88 "testing"
99
1010 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+ "github.com/hashicorp/terraform-plugin-testing/plancheck"
1112 "github.com/hashicorp/terraform-plugin-testing/terraform"
1213 "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
1314 tpgcompute "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/compute"
@@ -97,7 +98,6 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
9798 t .Parallel ()
9899
99100 var subnetwork compute.Subnetwork
100-
101101 cnName := fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
102102 subnetworkName := fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
103103
@@ -109,32 +109,28 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
109109 {
110110 Config : testAccComputeSubnetwork_update1 (cnName , "10.2.0.0/24" , subnetworkName ),
111111 Check : resource .ComposeTestCheckFunc (
112- testAccCheckComputeSubnetworkExists (
113- t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
112+ testAccCheckComputeSubnetworkExists (t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
114113 ),
115114 },
116115 {
117116 // Expand IP CIDR range and update private_ip_google_access
118117 Config : testAccComputeSubnetwork_update2 (cnName , "10.2.0.0/16" , subnetworkName ),
119118 Check : resource .ComposeTestCheckFunc (
120- testAccCheckComputeSubnetworkExists (
121- t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
119+ testAccCheckComputeSubnetworkExists (t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
122120 ),
123121 },
124122 {
125123 // Shrink IP CIDR range and update private_ip_google_access
126124 Config : testAccComputeSubnetwork_update2 (cnName , "10.2.0.0/24" , subnetworkName ),
127125 Check : resource .ComposeTestCheckFunc (
128- testAccCheckComputeSubnetworkExists (
129- t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
126+ testAccCheckComputeSubnetworkExists (t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
130127 ),
131128 },
132129 {
133130 // Add a secondary range and enable flow logs at once
134131 Config : testAccComputeSubnetwork_update3 (cnName , "10.2.0.0/24" , subnetworkName ),
135132 Check : resource .ComposeTestCheckFunc (
136- testAccCheckComputeSubnetworkExists (
137- t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
133+ testAccCheckComputeSubnetworkExists (t , "google_compute_subnetwork.network-with-private-google-access" , & subnetwork ),
138134 ),
139135 },
140136 {
@@ -150,6 +146,49 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
150146 }
151147}
152148
149+ func TestAccComputeSubnetwork_purposeUpdate (t * testing.T ) {
150+ t .Parallel ()
151+
152+ var subnetwork compute.Subnetwork
153+ cnName := fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
154+ subnetworkName := fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
155+
156+ acctest .VcrTest (t , resource.TestCase {
157+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
158+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
159+ CheckDestroy : testAccCheckComputeSubnetworkDestroyProducer (t ),
160+ Steps : []resource.TestStep {
161+ {
162+ // Create a subnetwork with the purpose set to PEER_MIGRATION
163+ Config : testAccComputeSubnetwork_purposeUpdate (cnName , subnetworkName ),
164+ Check : resource .ComposeTestCheckFunc (
165+ testAccCheckComputeSubnetworkExists (t , "google_compute_subnetwork.network-with-migration-purpose" , & subnetwork ),
166+ ),
167+ },
168+ {
169+ ResourceName : "google_compute_subnetwork.network-with-migration-purpose" ,
170+ ImportState : true ,
171+ ImportStateVerify : true ,
172+ },
173+ {
174+ // update the purpose from PEER_MIGRATION to PRIVATE
175+ Config : testAccComputeSubnetwork_purposeUpdate1 (cnName , subnetworkName ),
176+ ConfigPlanChecks : resource.ConfigPlanChecks {
177+ PreApply : []plancheck.PlanCheck {
178+ plancheck .ExpectResourceAction ("google_compute_subnetwork.network-with-migration-purpose" , plancheck .ResourceActionUpdate ),
179+ },
180+ },
181+ },
182+ {
183+ ResourceName : "google_compute_subnetwork.network-with-migration-purpose" ,
184+ ImportState : true ,
185+ ImportStateVerify : true ,
186+ },
187+ },
188+ })
189+
190+ }
191+
153192func TestAccComputeSubnetwork_secondaryIpRanges (t * testing.T ) {
154193 t .Parallel ()
155194
@@ -542,6 +581,7 @@ resource "google_compute_subnetwork" "network-with-private-google-access" {
542581 network = google_compute_network.custom-test.self_link
543582 private_ip_google_access = true
544583}
584+
545585` , cnName , subnetwork1Name , subnetwork2Name , subnetwork3Name )
546586}
547587
@@ -599,6 +639,42 @@ resource "google_compute_subnetwork" "network-with-private-google-access" {
599639` , cnName , subnetworkName , cidrRange )
600640}
601641
642+ // Create a subnetwork with its purpose set to PEER_MIGRATION
643+ func testAccComputeSubnetwork_purposeUpdate (cnName , subnetworkName string ) string {
644+ return fmt .Sprintf (`
645+ resource "google_compute_network" "custom-test" {
646+ name = "%s"
647+ auto_create_subnetworks = false
648+ }
649+
650+ resource "google_compute_subnetwork" "network-with-migration-purpose" {
651+ name = "%s"
652+ ip_cidr_range = "10.4.0.0/16"
653+ region = "us-central1"
654+ network = google_compute_network.custom-test.self_link
655+ purpose = "PEER_MIGRATION"
656+ }
657+ ` , cnName , subnetworkName )
658+ }
659+
660+ // Returns a subnetwork with its purpose set to PRIVATE
661+ func testAccComputeSubnetwork_purposeUpdate1 (cnName , subnetworkName string ) string {
662+ return fmt .Sprintf (`
663+ resource "google_compute_network" "custom-test" {
664+ name = "%s"
665+ auto_create_subnetworks = false
666+ }
667+
668+ resource "google_compute_subnetwork" "network-with-migration-purpose" {
669+ name = "%s"
670+ ip_cidr_range = "10.4.0.0/16"
671+ region = "us-central1"
672+ network = google_compute_network.custom-test.self_link
673+ purpose = "PRIVATE"
674+ }
675+ ` , cnName , subnetworkName )
676+ }
677+
602678func testAccComputeSubnetwork_secondaryIpRanges_update1 (cnName , subnetworkName string ) string {
603679 return fmt .Sprintf (`
604680resource "google_compute_network" "custom-test" {
0 commit comments