Skip to content

Commit 3edc574

Browse files
Properly handle AutomatedBackupPolicy midnight start time (hours = 0) (#8337) (#15219)
* Properly handle ABP midnight (hours = 0) * Add ExpectNonEmptyPlan to test * Simplify custom flatten Signed-off-by: Modular Magician <[email protected]>
1 parent d4c0756 commit 3edc574

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

.changelog/8337.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
Fix automated backup policy handling of 0 start time (midnight)
3+
```

google/resource_alloydb_cluster_test.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestAccAlloydbCluster_addAutomatedBackupPolicyAndInitialUser(t *testing.T)
7979

8080
context := map[string]interface{}{
8181
"random_suffix": acctest.RandString(t, 10),
82+
"hour": 23,
8283
}
8384

8485
acctest.VcrTest(t, resource.TestCase{
@@ -119,6 +120,7 @@ func TestAccAlloydbCluster_deleteAutomatedBackupPolicyAndInitialUser(t *testing.
119120

120121
context := map[string]interface{}{
121122
"random_suffix": acctest.RandString(t, 10),
123+
"hour": 23,
122124
}
123125

124126
acctest.VcrTest(t, resource.TestCase{
@@ -151,6 +153,37 @@ func TestAccAlloydbCluster_deleteAutomatedBackupPolicyAndInitialUser(t *testing.
151153
})
152154
}
153155

156+
// Test if automatedBackupPolicy properly handles a startTime of 0 (aka midnight). Calling terraform plan
157+
// after creating the cluster should not bring anything up.
158+
func TestAccAlloydbCluster_AutomatedBackupPolicyHandlesMidnight(t *testing.T) {
159+
t.Parallel()
160+
161+
context := map[string]interface{}{
162+
"random_suffix": acctest.RandString(t, 10),
163+
"hour": 0,
164+
}
165+
166+
acctest.VcrTest(t, resource.TestCase{
167+
PreCheck: func() { acctest.AccTestPreCheck(t) },
168+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
169+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
170+
Steps: []resource.TestStep{
171+
{
172+
Config: testAccAlloydbCluster_withInitialUserAndAutomatedBackupPolicy(context),
173+
},
174+
{
175+
ResourceName: "google_alloydb_cluster.default",
176+
ImportState: true,
177+
ImportStateVerify: true,
178+
ImportStateVerifyIgnore: []string{"initial_user", "cluster_id", "location"},
179+
},
180+
{
181+
Config: testAccAlloydbCluster_alloydbClusterBasicExample(context),
182+
},
183+
},
184+
})
185+
}
186+
154187
func testAccAlloydbCluster_withInitialUserAndAutomatedBackupPolicy(context map[string]interface{}) string {
155188
return acctest.Nprintf(`
156189
resource "google_alloydb_cluster" "default" {
@@ -172,7 +205,7 @@ resource "google_alloydb_cluster" "default" {
172205
days_of_week = ["MONDAY"]
173206
174207
start_times {
175-
hours = 23
208+
hours = %{hour}
176209
minutes = 0
177210
seconds = 0
178211
nanos = 0

google/services/alloydb/resource_alloydb_cluster.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,8 @@ func flattenAlloydbClusterAutomatedBackupPolicyWeeklyScheduleStartTimes(v interf
860860
for _, raw := range l {
861861
original := raw.(map[string]interface{})
862862
if len(original) < 1 {
863-
// Do not include empty json objects coming back from the api
864-
continue
863+
// If no start times exist, that means we take backups at midnight. This is represented as 0's all around.
864+
return append(transformed, map[string]interface{}{})
865865
}
866866
transformed = append(transformed, map[string]interface{}{
867867
"hours": flattenAlloydbClusterAutomatedBackupPolicyWeeklyScheduleStartTimesHours(original["hours"], d, config),
@@ -872,6 +872,7 @@ func flattenAlloydbClusterAutomatedBackupPolicyWeeklyScheduleStartTimes(v interf
872872
}
873873
return transformed
874874
}
875+
875876
func flattenAlloydbClusterAutomatedBackupPolicyWeeklyScheduleStartTimesHours(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
876877
// Handles the string fixed64 format
877878
if strVal, ok := v.(string); ok {

0 commit comments

Comments
 (0)