Skip to content

Commit 2ff7a2b

Browse files
melinathNA2047
authored andcommitted
Made nested query resources replace the whole resource instead of merging new over old (GoogleCloudPlatform#13234)
1 parent 7ac081b commit 2ff7a2b

File tree

2 files changed

+52
-104
lines changed

2 files changed

+52
-104
lines changed

mmv1/templates/terraform/nested_query.go.tmpl

+22-4
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,29 @@ func resource{{ $.ResourceName }}PatchUpdateEncoder(d *schema.ResourceData, meta
140140
return nil, fmt.Errorf("Unable to update {{ $.Name }} %q - not found in list", d.Id())
141141
}
142142

143-
// Merge new object into old.
144-
for k, v := range obj {
145-
item[k] = v
143+
// Copy over values for immutable fields
144+
{{- range $prop := $.SettableProperties }}
145+
{{- if $prop.IsForceNew }}
146+
obj["{{$prop.ApiName}}"] = item["{{$prop.ApiName}}"]
147+
{{- end }}
148+
{{- end }}
149+
// Merge any fields in item that aren't managed by this resource into obj
150+
// This is necessary because item might be managed by multiple resources.
151+
settableFields := map[string]struct{}{
152+
{{- range $prop := $.SettableProperties }}
153+
{{- if not $prop.IsForceNew }}
154+
"{{$prop.ApiName}}": struct{}{},
155+
{{- end }}
156+
{{- end }}
146157
}
147-
items[idx] = item
158+
for k, v := range item {
159+
if _, ok := settableFields[k]; !ok {
160+
obj[k] = v
161+
}
162+
}
163+
164+
// Override old object with new
165+
items[idx] = obj
148166

149167
// Return list with new item added
150168
res := map[string]interface{}{

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

+30-100
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
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/google/acctest"
1314
"github.com/hashicorp/terraform-provider-google/google/envvar"
@@ -84,6 +85,11 @@ func TestAccComputeRouterNat_update(t *testing.T) {
8485
},
8586
{
8687
Config: testAccComputeRouterNatUpdated(routerName),
88+
ConfigPlanChecks: resource.ConfigPlanChecks{
89+
PreApply: []plancheck.PlanCheck{
90+
plancheck.ExpectResourceAction("google_compute_router_nat.foobar", plancheck.ResourceActionUpdate),
91+
},
92+
},
8793
},
8894
{
8995
ResourceName: "google_compute_router_nat.foobar",
@@ -92,6 +98,11 @@ func TestAccComputeRouterNat_update(t *testing.T) {
9298
},
9399
{
94100
Config: testAccComputeRouterNatUpdateToNatIPsId(routerName),
101+
ConfigPlanChecks: resource.ConfigPlanChecks{
102+
PreApply: []plancheck.PlanCheck{
103+
plancheck.ExpectResourceAction("google_compute_router_nat.foobar", plancheck.ResourceActionNoop),
104+
},
105+
},
95106
},
96107
{
97108
ResourceName: "google_compute_router_nat.foobar",
@@ -100,6 +111,11 @@ func TestAccComputeRouterNat_update(t *testing.T) {
100111
},
101112
{
102113
Config: testAccComputeRouterNatUpdateToNatIPsName(routerName),
114+
ConfigPlanChecks: resource.ConfigPlanChecks{
115+
PreApply: []plancheck.PlanCheck{
116+
plancheck.ExpectResourceAction("google_compute_router_nat.foobar", plancheck.ResourceActionNoop),
117+
},
118+
},
103119
},
104120
{
105121
ResourceName: "google_compute_router_nat.foobar",
@@ -108,37 +124,11 @@ func TestAccComputeRouterNat_update(t *testing.T) {
108124
},
109125
{
110126
Config: testAccComputeRouterNatBasicBeforeUpdate(routerName),
111-
},
112-
{
113-
ResourceName: "google_compute_router_nat.foobar",
114-
ImportState: true,
115-
ImportStateVerify: true,
116-
},
117-
},
118-
})
119-
}
120-
121-
func TestAccComputeRouterNat_removeLogConfig(t *testing.T) {
122-
t.Parallel()
123-
124-
testId := acctest.RandString(t, 10)
125-
routerName := fmt.Sprintf("tf-test-router-nat-%s", testId)
126-
127-
acctest.VcrTest(t, resource.TestCase{
128-
PreCheck: func() { acctest.AccTestPreCheck(t) },
129-
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
130-
CheckDestroy: testAccCheckComputeRouterNatDestroyProducer(t),
131-
Steps: []resource.TestStep{
132-
{
133-
Config: testAccComputeRouterNatLogConfig(routerName),
134-
},
135-
{
136-
ResourceName: "google_compute_router_nat.foobar",
137-
ImportState: true,
138-
ImportStateVerify: true,
139-
},
140-
{
141-
Config: testAccComputeRouterNatLogConfigRemoved(routerName),
127+
ConfigPlanChecks: resource.ConfigPlanChecks{
128+
PreApply: []plancheck.PlanCheck{
129+
plancheck.ExpectResourceAction("google_compute_router_nat.foobar", plancheck.ResourceActionUpdate),
130+
},
131+
},
142132
},
143133
{
144134
ResourceName: "google_compute_router_nat.foobar",
@@ -1016,6 +1006,7 @@ resource "google_compute_router" "foobar" {
10161006

10171007
resource "google_compute_network" "foobar" {
10181008
name = "%s-net"
1009+
auto_create_subnetworks = false
10191010
}
10201011

10211012
resource "google_compute_subnetwork" "foobar" {
@@ -1037,11 +1028,6 @@ resource "google_compute_router_nat" "foobar" {
10371028
nat_ip_allocate_option = "AUTO_ONLY"
10381029
nat_ips = []
10391030
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
1040-
1041-
log_config {
1042-
enable = true
1043-
filter = "ERRORS_ONLY"
1044-
}
10451031
}
10461032
`, routerName, routerName, routerName, routerName, routerName)
10471033
}
@@ -1056,6 +1042,7 @@ resource "google_compute_router" "foobar" {
10561042

10571043
resource "google_compute_network" "foobar" {
10581044
name = "%s-net"
1045+
auto_create_subnetworks = false
10591046
}
10601047

10611048
resource "google_compute_subnetwork" "foobar" {
@@ -1090,6 +1077,7 @@ resource "google_compute_router_nat" "foobar" {
10901077
tcp_established_idle_timeout_sec = 1600
10911078
tcp_transitory_idle_timeout_sec = 60
10921079
tcp_time_wait_timeout_sec = 60
1080+
max_ports_per_vm = 128
10931081

10941082
log_config {
10951083
enable = true
@@ -1142,7 +1130,8 @@ network = google_compute_network.foobar.self_link
11421130
}
11431131

11441132
resource "google_compute_network" "foobar" {
1145-
name = "%s-net"
1133+
name = "%s-net"
1134+
auto_create_subnetworks = false
11461135
}
11471136
resource "google_compute_subnetwork" "foobar" {
11481137
name = "%s-subnet"
@@ -1176,6 +1165,7 @@ resource "google_compute_router_nat" "foobar" {
11761165
tcp_established_idle_timeout_sec = 1600
11771166
tcp_transitory_idle_timeout_sec = 60
11781167
tcp_time_wait_timeout_sec = 60
1168+
max_ports_per_vm = 128
11791169

11801170
log_config {
11811171
enable = true
@@ -1194,7 +1184,8 @@ network = google_compute_network.foobar.self_link
11941184
}
11951185

11961186
resource "google_compute_network" "foobar" {
1197-
name = "%s-net"
1187+
name = "%s-net"
1188+
auto_create_subnetworks = false
11981189
}
11991190
resource "google_compute_subnetwork" "foobar" {
12001191
name = "%s-subnet"
@@ -1228,6 +1219,7 @@ resource "google_compute_router_nat" "foobar" {
12281219
tcp_established_idle_timeout_sec = 1600
12291220
tcp_transitory_idle_timeout_sec = 60
12301221
tcp_time_wait_timeout_sec = 60
1222+
max_ports_per_vm = 128
12311223

12321224
log_config {
12331225
enable = true
@@ -1833,68 +1825,6 @@ resource "google_compute_router" "foobar" {
18331825
`, routerName, routerName, routerName)
18341826
}
18351827

1836-
func testAccComputeRouterNatLogConfig(routerName string) string {
1837-
return fmt.Sprintf(`
1838-
resource "google_compute_network" "foobar" {
1839-
name = "%s-net"
1840-
}
1841-
1842-
resource "google_compute_subnetwork" "foobar" {
1843-
name = "%s-subnet"
1844-
network = google_compute_network.foobar.self_link
1845-
ip_cidr_range = "10.0.0.0/16"
1846-
region = "us-central1"
1847-
}
1848-
1849-
resource "google_compute_router" "foobar" {
1850-
name = "%s"
1851-
region = google_compute_subnetwork.foobar.region
1852-
network = google_compute_network.foobar.self_link
1853-
}
1854-
1855-
resource "google_compute_router_nat" "foobar" {
1856-
name = "%s"
1857-
router = google_compute_router.foobar.name
1858-
region = google_compute_router.foobar.region
1859-
nat_ip_allocate_option = "AUTO_ONLY"
1860-
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
1861-
log_config {
1862-
enable = false
1863-
filter = "ALL"
1864-
}
1865-
}
1866-
`, routerName, routerName, routerName, routerName)
1867-
}
1868-
1869-
func testAccComputeRouterNatLogConfigRemoved(routerName string) string {
1870-
return fmt.Sprintf(`
1871-
resource "google_compute_network" "foobar" {
1872-
name = "%s-net"
1873-
}
1874-
1875-
resource "google_compute_subnetwork" "foobar" {
1876-
name = "%s-subnet"
1877-
network = google_compute_network.foobar.self_link
1878-
ip_cidr_range = "10.0.0.0/16"
1879-
region = "us-central1"
1880-
}
1881-
1882-
resource "google_compute_router" "foobar" {
1883-
name = "%s"
1884-
region = google_compute_subnetwork.foobar.region
1885-
network = google_compute_network.foobar.self_link
1886-
}
1887-
1888-
resource "google_compute_router_nat" "foobar" {
1889-
name = "%s"
1890-
router = google_compute_router.foobar.name
1891-
region = google_compute_router.foobar.region
1892-
nat_ip_allocate_option = "AUTO_ONLY"
1893-
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
1894-
}
1895-
`, routerName, routerName, routerName, routerName)
1896-
}
1897-
18981828
func testAccComputeRouterNatBaseResourcesWithPrivateNatSubnetworks(routerName, hubName string) string {
18991829
return fmt.Sprintf(`
19001830
resource "google_compute_network" "foobar" {

0 commit comments

Comments
 (0)