Skip to content

Commit 3ffeccd

Browse files
gvijbhamelinath
authored andcommitted
Enhance subnetwork.yaml to allow purpose field to be patch-able (GoogleCloudPlatform#12922)
Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
1 parent 10cc10c commit 3ffeccd

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed

mmv1/products/compute/Subnetwork.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,11 @@ properties:
235235
A subnetwork with purpose set to `PRIVATE_NAT` is used as source range for Private NAT gateways.
236236
Note that `REGIONAL_MANAGED_PROXY` is the preferred setting for all regional Envoy load balancers.
237237
If unspecified, the purpose defaults to `PRIVATE`.
238-
immutable: true
239238
default_from_api: true
239+
update_url: 'projects/{{project}}/regions/{{region}}/subnetworks/{{name}}'
240+
update_verb: 'PATCH'
241+
update_id: 'purpose'
242+
fingerprint_name: 'fingerprint'
240243
- name: 'role'
241244
type: Enum
242245
description: |

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ locals {
9090
project_id = "%s"
9191
region = "%s"
9292
subnet_one = "%s"
93-
subnet_two = "%s"
93+
subnet_two = "%s"
9494
}
9595
9696
resource "google_compute_network" "this" {

mmv1/third_party/terraform/services/compute/resource_compute_subnetwork_test.go.tmpl

+88-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
99
"github.com/hashicorp/terraform-plugin-testing/terraform"
1010
"github.com/hashicorp/terraform-provider-google/google/acctest"
11-
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
11+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
12+
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
1213

1314
{{ if eq $.TargetVersionName `ga` }}
1415
"google.golang.org/api/compute/v1"
@@ -79,7 +80,7 @@ func TestAccComputeSubnetwork_basic(t *testing.T) {
7980
testAccCheckComputeSubnetworkExists(
8081
t, "google_compute_subnetwork.network-ref-by-name", &subnetwork2),
8182
resource.TestCheckResourceAttrSet(
82-
"google_compute_subnetwork.network-ref-by-name", "subnetwork_id"), ),
83+
"google_compute_subnetwork.network-ref-by-name", "subnetwork_id"),),
8384
},
8485
{
8586
ResourceName: "google_compute_subnetwork.network-ref-by-url",
@@ -99,7 +100,6 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
99100
t.Parallel()
100101

101102
var subnetwork compute.Subnetwork
102-
103103
cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
104104
subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
105105

@@ -111,32 +111,28 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
111111
{
112112
Config: testAccComputeSubnetwork_update1(cnName, "10.2.0.0/24", subnetworkName),
113113
Check: resource.ComposeTestCheckFunc(
114-
testAccCheckComputeSubnetworkExists(
115-
t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
114+
testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
116115
),
117116
},
118117
{
119118
// Expand IP CIDR range and update private_ip_google_access
120119
Config: testAccComputeSubnetwork_update2(cnName, "10.2.0.0/16", subnetworkName),
121120
Check: resource.ComposeTestCheckFunc(
122-
testAccCheckComputeSubnetworkExists(
123-
t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
121+
testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
124122
),
125123
},
126124
{
127125
// Shrink IP CIDR range and update private_ip_google_access
128126
Config: testAccComputeSubnetwork_update2(cnName, "10.2.0.0/24", subnetworkName),
129127
Check: resource.ComposeTestCheckFunc(
130-
testAccCheckComputeSubnetworkExists(
131-
t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
128+
testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
132129
),
133130
},
134131
{
135132
// Add a secondary range and enable flow logs at once
136133
Config: testAccComputeSubnetwork_update3(cnName, "10.2.0.0/24", subnetworkName),
137134
Check: resource.ComposeTestCheckFunc(
138-
testAccCheckComputeSubnetworkExists(
139-
t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
135+
testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.network-with-private-google-access", &subnetwork),
140136
),
141137
},
142138
{
@@ -152,6 +148,50 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
152148
}
153149
}
154150

151+
func TestAccComputeSubnetwork_purposeUpdate(t *testing.T) {
152+
t.Parallel()
153+
154+
var subnetwork compute.Subnetwork
155+
cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
156+
subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
157+
158+
acctest.VcrTest(t, resource.TestCase{
159+
PreCheck: func() { acctest.AccTestPreCheck(t) },
160+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
161+
CheckDestroy: testAccCheckComputeSubnetworkDestroyProducer(t),
162+
Steps: []resource.TestStep{
163+
{
164+
// Create a subnetwork with the purpose set to PEER_MIGRATION
165+
Config: testAccComputeSubnetwork_purposeUpdate(cnName, subnetworkName),
166+
Check: resource.ComposeTestCheckFunc(
167+
testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.network-with-migration-purpose", &subnetwork),
168+
),
169+
},
170+
{
171+
ResourceName: "google_compute_subnetwork.network-with-migration-purpose",
172+
ImportState: true,
173+
ImportStateVerify: true,
174+
},
175+
{
176+
// update the purpose from PEER_MIGRATION to PRIVATE
177+
Config: testAccComputeSubnetwork_purposeUpdate1(cnName, subnetworkName),
178+
ConfigPlanChecks: resource.ConfigPlanChecks{
179+
PreApply: []plancheck.PlanCheck{
180+
plancheck.ExpectResourceAction("google_compute_subnetwork.network-with-migration-purpose", plancheck.ResourceActionUpdate),
181+
},
182+
},
183+
},
184+
{
185+
ResourceName: "google_compute_subnetwork.network-with-migration-purpose",
186+
ImportState: true,
187+
ImportStateVerify: true,
188+
},
189+
},
190+
})
191+
192+
193+
}
194+
155195
func TestAccComputeSubnetwork_secondaryIpRanges(t *testing.T) {
156196
t.Parallel()
157197

@@ -544,6 +584,7 @@ resource "google_compute_subnetwork" "network-with-private-google-access" {
544584
network = google_compute_network.custom-test.self_link
545585
private_ip_google_access = true
546586
}
587+
547588
`, cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name)
548589
}
549590

@@ -601,6 +642,42 @@ resource "google_compute_subnetwork" "network-with-private-google-access" {
601642
`, cnName, subnetworkName, cidrRange)
602643
}
603644

645+
// Create a subnetwork with its purpose set to PEER_MIGRATION
646+
func testAccComputeSubnetwork_purposeUpdate(cnName, subnetworkName string) string {
647+
return fmt.Sprintf(`
648+
resource "google_compute_network" "custom-test" {
649+
name = "%s"
650+
auto_create_subnetworks = false
651+
}
652+
653+
resource "google_compute_subnetwork" "network-with-migration-purpose" {
654+
name = "%s"
655+
ip_cidr_range = "10.4.0.0/16"
656+
region = "us-central1"
657+
network = google_compute_network.custom-test.self_link
658+
purpose = "PEER_MIGRATION"
659+
}
660+
`, cnName, subnetworkName)
661+
}
662+
663+
// Returns a subnetwork with its purpose set to PRIVATE
664+
func testAccComputeSubnetwork_purposeUpdate1(cnName, subnetworkName string) string {
665+
return fmt.Sprintf(`
666+
resource "google_compute_network" "custom-test" {
667+
name = "%s"
668+
auto_create_subnetworks = false
669+
}
670+
671+
resource "google_compute_subnetwork" "network-with-migration-purpose" {
672+
name = "%s"
673+
ip_cidr_range = "10.4.0.0/16"
674+
region = "us-central1"
675+
network = google_compute_network.custom-test.self_link
676+
purpose = "PRIVATE"
677+
}
678+
`, cnName, subnetworkName)
679+
}
680+
604681
func testAccComputeSubnetwork_secondaryIpRanges_update1(cnName, subnetworkName string) string {
605682
return fmt.Sprintf(`
606683
resource "google_compute_network" "custom-test" {

0 commit comments

Comments
 (0)