Skip to content

Commit 8940912

Browse files
Fix Spanner update when adding/removing autoscaling config (#9814) (#17033)
* Add reproducer test for error. Creating a Spanner instance without auto-scaling and then updating it to add auto-scaling should work, but currently doesn't. * Fix `fieldMask` for spanner updates when enabling/disable autoscaling. When adding/removing auto-scaling config wholesale, the API expects only the top-level `autoscalingConfig` key to be listed in the `fieldMask`. Listing sub-fields of the autoscaling config causes an error. * Add tests for dropping auto-scaling. Not sure if this was broken before, but makes sense to me that we should test in both directions. * Apply review feedback * Fix indentation in template * Rename test instances to match expected pattern * Consolidate tests * Fix name-too-long errors [upstream:8f488115df8493669aae88a65b80a2fcb31d7557] Signed-off-by: Modular Magician <[email protected]>
1 parent dc56dec commit 8940912

File tree

3 files changed

+79
-19
lines changed

3 files changed

+79
-19
lines changed

.changelog/9814.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
spanner: fixed error when adding `autoscaling_config` to an existing `google_spanner_instance`
3+
```

google/services/spanner/resource_spanner_instance.go

+26-17
Original file line numberDiff line numberDiff line change
@@ -1153,23 +1153,32 @@ func resourceSpannerInstanceUpdateEncoder(d *schema.ResourceData, meta interface
11531153
if d.HasChange("processing_units") {
11541154
updateMask = append(updateMask, "processingUnits")
11551155
}
1156-
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.max_processing_units") {
1157-
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.maxProcessingUnits")
1158-
}
1159-
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.min_processing_units") {
1160-
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.minProcessingUnits")
1161-
}
1162-
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.max_nodes") {
1163-
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.maxNodes")
1164-
}
1165-
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.min_nodes") {
1166-
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.minNodes")
1167-
}
1168-
if d.HasChange("autoscaling_config.0.autoscaling_targets.0.high_priority_cpu_utilization_percent") {
1169-
updateMask = append(updateMask, "autoscalingConfig.autoscalingTargets.highPriorityCpuUtilizationPercent")
1170-
}
1171-
if d.HasChange("autoscaling_config.0.autoscaling_targets.0.storage_utilization_percent") {
1172-
updateMask = append(updateMask, "autoscalingConfig.autoscalingTargets.storageUtilizationPercent")
1156+
if d.HasChange("autoscaling_config") {
1157+
old, new := d.GetChange("autoscaling_config")
1158+
oldSlice := old.([]interface{})
1159+
newSlice := new.([]interface{})
1160+
if len(oldSlice) == 0 || len(newSlice) == 0 {
1161+
updateMask = append(updateMask, "autoscalingConfig")
1162+
} else {
1163+
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.max_processing_units") {
1164+
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.maxProcessingUnits")
1165+
}
1166+
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.min_processing_units") {
1167+
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.minProcessingUnits")
1168+
}
1169+
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.max_nodes") {
1170+
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.maxNodes")
1171+
}
1172+
if d.HasChange("autoscaling_config.0.autoscaling_limits.0.min_nodes") {
1173+
updateMask = append(updateMask, "autoscalingConfig.autoscalingLimits.minNodes")
1174+
}
1175+
if d.HasChange("autoscaling_config.0.autoscaling_targets.0.high_priority_cpu_utilization_percent") {
1176+
updateMask = append(updateMask, "autoscalingConfig.autoscalingTargets.highPriorityCpuUtilizationPercent")
1177+
}
1178+
if d.HasChange("autoscaling_config.0.autoscaling_targets.0.storage_utilization_percent") {
1179+
updateMask = append(updateMask, "autoscalingConfig.autoscalingTargets.storageUtilizationPercent")
1180+
}
1181+
}
11731182
}
11741183
newObj["fieldMask"] = strings.Join(updateMask, ",")
11751184
return newObj, nil

google/services/spanner/resource_spanner_instance_test.go

+50-2
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,24 @@ func TestAccSpannerInstance_basicWithAutoscalingUsingProcessingUnitConfig(t *tes
171171
func TestAccSpannerInstance_basicWithAutoscalingUsingProcessingUnitConfigUpdate(t *testing.T) {
172172
t.Parallel()
173173

174-
displayName := fmt.Sprintf("spanner-test-%s-dname", acctest.RandString(t, 10))
174+
displayName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
175175
acctest.VcrTest(t, resource.TestCase{
176176
PreCheck: func() { acctest.AccTestPreCheck(t) },
177177
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
178178
CheckDestroy: testAccCheckSpannerInstanceDestroyProducer(t),
179179
Steps: []resource.TestStep{
180+
{
181+
Config: testAccSpannerInstance_basic(displayName),
182+
Check: resource.ComposeTestCheckFunc(
183+
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "state"),
184+
),
185+
},
186+
{
187+
ResourceName: "google_spanner_instance.basic",
188+
ImportState: true,
189+
ImportStateVerify: true,
190+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
191+
},
180192
{
181193
Config: testAccSpannerInstance_basicWithAutoscalerConfigUsingProcessingUnitsAsConfigsUpdate(displayName, 1000, 2000, 65, 95),
182194
Check: resource.ComposeTestCheckFunc(
@@ -201,6 +213,18 @@ func TestAccSpannerInstance_basicWithAutoscalingUsingProcessingUnitConfigUpdate(
201213
ImportStateVerify: true,
202214
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
203215
},
216+
{
217+
Config: testAccSpannerInstance_basic(displayName),
218+
Check: resource.ComposeTestCheckFunc(
219+
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "state"),
220+
),
221+
},
222+
{
223+
ResourceName: "google_spanner_instance.basic",
224+
ImportState: true,
225+
ImportStateVerify: true,
226+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
227+
},
204228
},
205229
})
206230
}
@@ -232,12 +256,24 @@ func TestAccSpannerInstance_basicWithAutoscalingUsingNodeConfig(t *testing.T) {
232256
func TestAccSpannerInstance_basicWithAutoscalingUsingNodeConfigUpdate(t *testing.T) {
233257
t.Parallel()
234258

235-
displayName := fmt.Sprintf("spanner-test-%s-dname", acctest.RandString(t, 10))
259+
displayName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
236260
acctest.VcrTest(t, resource.TestCase{
237261
PreCheck: func() { acctest.AccTestPreCheck(t) },
238262
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
239263
CheckDestroy: testAccCheckSpannerInstanceDestroyProducer(t),
240264
Steps: []resource.TestStep{
265+
{
266+
Config: testAccSpannerInstance_basic(displayName),
267+
Check: resource.ComposeTestCheckFunc(
268+
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "state"),
269+
),
270+
},
271+
{
272+
ResourceName: "google_spanner_instance.basic",
273+
ImportState: true,
274+
ImportStateVerify: true,
275+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
276+
},
241277
{
242278
Config: testAccSpannerInstance_basicWithAutoscalerConfigUsingNodesAsConfigsUpdate(displayName, 1, 2, 65, 95),
243279
Check: resource.ComposeTestCheckFunc(
@@ -262,6 +298,18 @@ func TestAccSpannerInstance_basicWithAutoscalingUsingNodeConfigUpdate(t *testing
262298
ImportStateVerify: true,
263299
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
264300
},
301+
{
302+
Config: testAccSpannerInstance_basic(displayName),
303+
Check: resource.ComposeTestCheckFunc(
304+
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "state"),
305+
),
306+
},
307+
{
308+
ResourceName: "google_spanner_instance.basic",
309+
ImportState: true,
310+
ImportStateVerify: true,
311+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
312+
},
265313
},
266314
})
267315
}

0 commit comments

Comments
 (0)