provider/aws: Validate window time format (#11089)
* create window format validate function * apply ValidateFunc for window time format
This commit is contained in:
parent
4110ec4702
commit
c560c72e58
|
@ -120,9 +120,10 @@ func resourceAwsDbInstance() *schema.Resource {
|
||||||
},
|
},
|
||||||
|
|
||||||
"backup_window": {
|
"backup_window": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
ValidateFunc: validateOnceADayWindowFormat,
|
||||||
},
|
},
|
||||||
|
|
||||||
"iops": {
|
"iops": {
|
||||||
|
@ -147,6 +148,7 @@ func resourceAwsDbInstance() *schema.Resource {
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
},
|
},
|
||||||
|
ValidateFunc: validateOnceAWeekWindowFormat,
|
||||||
},
|
},
|
||||||
|
|
||||||
"multi_az": {
|
"multi_az": {
|
||||||
|
|
|
@ -77,9 +77,10 @@ func resourceAwsElastiCacheCommonSchema() map[string]*schema.Schema {
|
||||||
Set: schema.HashString,
|
Set: schema.HashString,
|
||||||
},
|
},
|
||||||
"snapshot_window": &schema.Schema{
|
"snapshot_window": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
ValidateFunc: validateOnceADayWindowFormat,
|
||||||
},
|
},
|
||||||
"snapshot_name": &schema.Schema{
|
"snapshot_name": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
|
@ -96,6 +97,7 @@ func resourceAwsElastiCacheCommonSchema() map[string]*schema.Schema {
|
||||||
// to lowercase
|
// to lowercase
|
||||||
return strings.ToLower(val.(string))
|
return strings.ToLower(val.(string))
|
||||||
},
|
},
|
||||||
|
ValidateFunc: validateOnceAWeekWindowFormat,
|
||||||
},
|
},
|
||||||
"port": &schema.Schema{
|
"port": &schema.Schema{
|
||||||
Type: schema.TypeInt,
|
Type: schema.TypeInt,
|
||||||
|
|
|
@ -160,9 +160,10 @@ func resourceAwsRDSCluster() *schema.Resource {
|
||||||
},
|
},
|
||||||
|
|
||||||
"preferred_backup_window": {
|
"preferred_backup_window": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
ValidateFunc: validateOnceADayWindowFormat,
|
||||||
},
|
},
|
||||||
|
|
||||||
"preferred_maintenance_window": {
|
"preferred_maintenance_window": {
|
||||||
|
@ -175,6 +176,7 @@ func resourceAwsRDSCluster() *schema.Resource {
|
||||||
}
|
}
|
||||||
return strings.ToLower(val.(string))
|
return strings.ToLower(val.(string))
|
||||||
},
|
},
|
||||||
|
ValidateFunc: validateOnceAWeekWindowFormat,
|
||||||
},
|
},
|
||||||
|
|
||||||
"backup_retention_period": {
|
"backup_retention_period": {
|
||||||
|
|
|
@ -101,6 +101,7 @@ func resourceAwsRedshiftCluster() *schema.Resource {
|
||||||
}
|
}
|
||||||
return strings.ToLower(val.(string))
|
return strings.ToLower(val.(string))
|
||||||
},
|
},
|
||||||
|
ValidateFunc: validateOnceAWeekWindowFormat,
|
||||||
},
|
},
|
||||||
|
|
||||||
"cluster_parameter_group_name": {
|
"cluster_parameter_group_name": {
|
||||||
|
|
|
@ -624,3 +624,27 @@ func validateSecurityRuleType(v interface{}, k string) (ws []string, errors []er
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateOnceAWeekWindowFormat(v interface{}, k string) (ws []string, errors []error) {
|
||||||
|
// valid time format is "ddd:hh24:mi"
|
||||||
|
validTimeFormat := "(sun|mon|tue|wed|thu|fri|sat):([0-1][0-9]|2[0-3]):([0-5][0-9])"
|
||||||
|
|
||||||
|
value := strings.ToLower(v.(string))
|
||||||
|
if !regexp.MustCompile(validTimeFormat + "-" + validTimeFormat).MatchString(value) {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"%q must satisfy the format of \"ddd:hh24:mi-ddd:hh24:mi\".", k))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateOnceADayWindowFormat(v interface{}, k string) (ws []string, errors []error) {
|
||||||
|
// valid time format is "hh24:mi"
|
||||||
|
validTimeFormat := "([0-1][0-9]|2[0-3]):([0-5][0-9])"
|
||||||
|
|
||||||
|
value := v.(string)
|
||||||
|
if !regexp.MustCompile(validTimeFormat + "-" + validTimeFormat).MatchString(value) {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"%q must satisfy the format of \"hh24:mi-hh24:mi\".", k))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -923,3 +923,85 @@ func TestValidateSecurityRuleType(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateOnceAWeekWindowFormat(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Value string
|
||||||
|
ErrCount int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// once a day window format
|
||||||
|
Value: "04:00-05:00",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid day of week
|
||||||
|
Value: "san:04:00-san:05:00",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid hour
|
||||||
|
Value: "sun:24:00-san:25:00",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid min
|
||||||
|
Value: "sun:04:00-sun:04:60",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// valid format
|
||||||
|
Value: "sun:04:00-sun:05:00",
|
||||||
|
ErrCount: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// "Sun" can also be used
|
||||||
|
Value: "Sun:04:00-Sun:05:00",
|
||||||
|
ErrCount: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
_, errors := validateOnceAWeekWindowFormat(tc.Value, "maintenance_window")
|
||||||
|
|
||||||
|
if len(errors) != tc.ErrCount {
|
||||||
|
t.Fatalf("Expected %d validation errors, But got %d errors for \"%s\"", tc.ErrCount, len(errors), tc.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateOnceADayWindowFormat(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Value string
|
||||||
|
ErrCount int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// once a week window format
|
||||||
|
Value: "sun:04:00-sun:05:00",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid hour
|
||||||
|
Value: "24:00-25:00",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// invalid min
|
||||||
|
Value: "04:00-04:60",
|
||||||
|
ErrCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// valid format
|
||||||
|
Value: "04:00-05:00",
|
||||||
|
ErrCount: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
_, errors := validateOnceADayWindowFormat(tc.Value, "backup_window")
|
||||||
|
|
||||||
|
if len(errors) != tc.ErrCount {
|
||||||
|
t.Fatalf("Expected %d validation errors, But got %d errors for \"%s\"", tc.ErrCount, len(errors), tc.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue