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 (#10184) (#7117)

* fix(spanner): remove validation on version retention period on spanner database

* fix build

[upstream:5e5b8d75bfb2d2e98d048a3616a10302b6515493]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Mar 19, 2024
1 parent 88a62aa commit 4b10bcf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 116 deletions.
3 changes: 3 additions & 0 deletions .changelog/10184.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
spanner: removed validation function for the field `version_retention_period` in the resource `google_spanner_database` and directly returned error from backend
```
47 changes: 3 additions & 44 deletions google-beta/services/spanner/resource_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -67,44 +65,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 Expand Up @@ -210,10 +170,9 @@ in the same location as the Spanner Database.`,
},
},
"version_retention_period": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: ValidateDatabaseRetentionPeriod,
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `The retention period for the database. The retention period must be between 1 hour
and 7 days, and can be specified in days, hours, minutes, or seconds. For example,
the values 1d, 24h, 1440m, and 86400s are equivalent. Default value is 1h.
Expand Down
72 changes: 0 additions & 72 deletions google-beta/services/spanner/resource_spanner_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/spanner"
)

func TestAccSpannerDatabase_basic(t *testing.T) {
Expand Down Expand Up @@ -467,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 4b10bcf

Please sign in to comment.