Skip to content

Commit

Permalink
fix(spanner): remove validation on version retention period on spanne…
Browse files Browse the repository at this point in the history
…r database
  • Loading branch information
rahul2393 committed Mar 13, 2024
1 parent e3a06e3 commit 133a79e
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 111 deletions.
2 changes: 0 additions & 2 deletions mmv1/products/spanner/Database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ properties:
the values 1d, 24h, 1440m, and 86400s are equivalent. Default value is 1h.
If this property is used, you must avoid adding new DDL statements to `ddl` that
update the database's version_retention_period.
validation: !ruby/object:Provider::Terraform::Validation
function: ValidateDatabaseRetentionPeriod
default_from_api: true
- !ruby/object:Api::Type::Array
name: 'ddl'
Expand Down
38 changes: 0 additions & 38 deletions mmv1/templates/terraform/constants/spanner_database.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,6 @@ func resourceSpannerDBDdlCustomDiff(_ context.Context, diff *schema.ResourceDiff
return resourceSpannerDBDdlCustomDiffFunc(diff)
}

func ValidateDatabaseRetentionPeriod(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
valueError := fmt.Errorf("version_retention_period should be in range [1h, 7d], in a format resembling 1d, 24h, 1440m, or 86400s")

r := regexp.MustCompile("^(\\d{1}d|\\d{1,3}h|\\d{2,5}m|\\d{4,6}s)$")
if !r.MatchString(value) {
errors = append(errors, valueError)
return
}

unit := value[len(value)-1:]
multiple := value[:len(value)-1]
num, err := strconv.Atoi(multiple)
if err != nil {
errors = append(errors, valueError)
return
}

if unit == "d" && (num < 1 || num > 7) {
errors = append(errors, valueError)
return
}
if unit == "h" && (num < 1 || num > 7*24) {
errors = append(errors, valueError)
return
}
if unit == "m" && (num < 1*60 || num > 7*24*60) {
errors = append(errors, valueError)
return
}
if unit == "s" && (num < 1*60*60 || num > 7*24*60*60) {
errors = append(errors, valueError)
return
}

return
}

func resourceSpannerDBVirtualUpdate(d *schema.ResourceData, resourceSchema map[string]*schema.Schema) bool {
// deletion_protection is the only virtual field
if d.HasChange("deletion_protection") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,77 +466,6 @@ resource "google_spanner_database" "basic" {
`, instanceName, instanceName, databaseName)
}

// Unit Tests for validation of retention period argument
func TestValidateDatabaseRetentionPeriod(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
input string
expectError bool
}{
// Not valid input
"empty_string": {
input: "",
expectError: true,
},
"number_with_no_unit": {
input: "1",
expectError: true,
},
"less_than_1h": {
input: "59m",
expectError: true,
},
"more_than_7days": {
input: "8d",
expectError: true,
},
// Valid input
"1_hour_in_secs": {
input: "3600s",
expectError: false,
},
"1_hour_in_mins": {
input: "60m",
expectError: false,
},
"1_hour_in_hours": {
input: "1h",
expectError: false,
},
"7_days_in_secs": {
input: fmt.Sprintf("%ds", 7*24*60*60),
expectError: false,
},
"7_days_in_mins": {
input: fmt.Sprintf("%dm", 7*24*60),
expectError: false,
},
"7_days_in_hours": {
input: fmt.Sprintf("%dh", 7*24),
expectError: false,
},
"7_days_in_days": {
input: "7d",
expectError: false,
},
}

for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
_, errs := spanner.ValidateDatabaseRetentionPeriod(tc.input, "foobar")
var wantErrCount string
if tc.expectError {
wantErrCount = "1+"
} else {
wantErrCount = "0"
}
if (len(errs) > 0 && tc.expectError == false) || (len(errs) == 0 && tc.expectError == true) {
t.Errorf("failed, expected `%s` test case validation to have %s errors", tn, wantErrCount)
}
})
}
}

func TestAccSpannerDatabase_deletionProtection(t *testing.T) {
acctest.SkipIfVcr(t)
t.Parallel()
Expand Down

0 comments on commit 133a79e

Please sign in to comment.