Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
zli82016 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
"github.com/hashicorp/terraform-provider-google/google/services/spanner"
)

func TestAccSpannerDatabase_basic(t *testing.T) {
Expand Down Expand Up @@ -466,77 +465,6 @@ resource "google_spanner_database" "basic" {
`, instanceName, instanceName, databaseName)
}

// Unit Tests for validation of retention period argument
zli82016 marked this conversation as resolved.
Show resolved Hide resolved
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