Skip to content

Commit 4b10bcf

Browse files
fix(spanner): remove validation on version retention period on spanner database (#10184) (#7117)
* fix(spanner): remove validation on version retention period on spanner database * fix build [upstream:5e5b8d75bfb2d2e98d048a3616a10302b6515493] Signed-off-by: Modular Magician <[email protected]>
1 parent 88a62aa commit 4b10bcf

File tree

3 files changed

+6
-116
lines changed

3 files changed

+6
-116
lines changed

.changelog/10184.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
spanner: removed validation function for the field `version_retention_period` in the resource `google_spanner_database` and directly returned error from backend
3+
```

google-beta/services/spanner/resource_spanner_database.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"fmt"
2323
"log"
2424
"reflect"
25-
"regexp"
26-
"strconv"
2725
"strings"
2826
"time"
2927

@@ -67,44 +65,6 @@ func resourceSpannerDBDdlCustomDiff(_ context.Context, diff *schema.ResourceDiff
6765
return resourceSpannerDBDdlCustomDiffFunc(diff)
6866
}
6967

70-
func ValidateDatabaseRetentionPeriod(v interface{}, k string) (ws []string, errors []error) {
71-
value := v.(string)
72-
valueError := fmt.Errorf("version_retention_period should be in range [1h, 7d], in a format resembling 1d, 24h, 1440m, or 86400s")
73-
74-
r := regexp.MustCompile("^(\\d{1}d|\\d{1,3}h|\\d{2,5}m|\\d{4,6}s)$")
75-
if !r.MatchString(value) {
76-
errors = append(errors, valueError)
77-
return
78-
}
79-
80-
unit := value[len(value)-1:]
81-
multiple := value[:len(value)-1]
82-
num, err := strconv.Atoi(multiple)
83-
if err != nil {
84-
errors = append(errors, valueError)
85-
return
86-
}
87-
88-
if unit == "d" && (num < 1 || num > 7) {
89-
errors = append(errors, valueError)
90-
return
91-
}
92-
if unit == "h" && (num < 1 || num > 7*24) {
93-
errors = append(errors, valueError)
94-
return
95-
}
96-
if unit == "m" && (num < 1*60 || num > 7*24*60) {
97-
errors = append(errors, valueError)
98-
return
99-
}
100-
if unit == "s" && (num < 1*60*60 || num > 7*24*60*60) {
101-
errors = append(errors, valueError)
102-
return
103-
}
104-
105-
return
106-
}
107-
10868
func resourceSpannerDBVirtualUpdate(d *schema.ResourceData, resourceSchema map[string]*schema.Schema) bool {
10969
// deletion_protection is the only virtual field
11070
if d.HasChange("deletion_protection") {
@@ -210,10 +170,9 @@ in the same location as the Spanner Database.`,
210170
},
211171
},
212172
"version_retention_period": {
213-
Type: schema.TypeString,
214-
Computed: true,
215-
Optional: true,
216-
ValidateFunc: ValidateDatabaseRetentionPeriod,
173+
Type: schema.TypeString,
174+
Computed: true,
175+
Optional: true,
217176
Description: `The retention period for the database. The retention period must be between 1 hour
218177
and 7 days, and can be specified in days, hours, minutes, or seconds. For example,
219178
the values 1d, 24h, 1440m, and 86400s are equivalent. Default value is 1h.

google-beta/services/spanner/resource_spanner_database_test.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
1212
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
13-
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/spanner"
1413
)
1514

1615
func TestAccSpannerDatabase_basic(t *testing.T) {
@@ -467,77 +466,6 @@ resource "google_spanner_database" "basic" {
467466
`, instanceName, instanceName, databaseName)
468467
}
469468

470-
// Unit Tests for validation of retention period argument
471-
func TestValidateDatabaseRetentionPeriod(t *testing.T) {
472-
t.Parallel()
473-
testCases := map[string]struct {
474-
input string
475-
expectError bool
476-
}{
477-
// Not valid input
478-
"empty_string": {
479-
input: "",
480-
expectError: true,
481-
},
482-
"number_with_no_unit": {
483-
input: "1",
484-
expectError: true,
485-
},
486-
"less_than_1h": {
487-
input: "59m",
488-
expectError: true,
489-
},
490-
"more_than_7days": {
491-
input: "8d",
492-
expectError: true,
493-
},
494-
// Valid input
495-
"1_hour_in_secs": {
496-
input: "3600s",
497-
expectError: false,
498-
},
499-
"1_hour_in_mins": {
500-
input: "60m",
501-
expectError: false,
502-
},
503-
"1_hour_in_hours": {
504-
input: "1h",
505-
expectError: false,
506-
},
507-
"7_days_in_secs": {
508-
input: fmt.Sprintf("%ds", 7*24*60*60),
509-
expectError: false,
510-
},
511-
"7_days_in_mins": {
512-
input: fmt.Sprintf("%dm", 7*24*60),
513-
expectError: false,
514-
},
515-
"7_days_in_hours": {
516-
input: fmt.Sprintf("%dh", 7*24),
517-
expectError: false,
518-
},
519-
"7_days_in_days": {
520-
input: "7d",
521-
expectError: false,
522-
},
523-
}
524-
525-
for tn, tc := range testCases {
526-
t.Run(tn, func(t *testing.T) {
527-
_, errs := spanner.ValidateDatabaseRetentionPeriod(tc.input, "foobar")
528-
var wantErrCount string
529-
if tc.expectError {
530-
wantErrCount = "1+"
531-
} else {
532-
wantErrCount = "0"
533-
}
534-
if (len(errs) > 0 && tc.expectError == false) || (len(errs) == 0 && tc.expectError == true) {
535-
t.Errorf("failed, expected `%s` test case validation to have %s errors", tn, wantErrCount)
536-
}
537-
})
538-
}
539-
}
540-
541469
func TestAccSpannerDatabase_deletionProtection(t *testing.T) {
542470
acctest.SkipIfVcr(t)
543471
t.Parallel()

0 commit comments

Comments
 (0)