provider/aws: Validate window time format (#11089)

* create window format validate function

* apply ValidateFunc for window time format
This commit is contained in:
Kazuma Watanabe 2017-01-09 22:12:07 +09:00 committed by Paul Stack
parent 4110ec4702
commit c560c72e58
6 changed files with 122 additions and 9 deletions

View File

@ -120,9 +120,10 @@ func resourceAwsDbInstance() *schema.Resource {
},
"backup_window": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateOnceADayWindowFormat,
},
"iops": {
@ -147,6 +148,7 @@ func resourceAwsDbInstance() *schema.Resource {
}
return ""
},
ValidateFunc: validateOnceAWeekWindowFormat,
},
"multi_az": {

View File

@ -77,9 +77,10 @@ func resourceAwsElastiCacheCommonSchema() map[string]*schema.Schema {
Set: schema.HashString,
},
"snapshot_window": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateOnceADayWindowFormat,
},
"snapshot_name": &schema.Schema{
Type: schema.TypeString,
@ -96,6 +97,7 @@ func resourceAwsElastiCacheCommonSchema() map[string]*schema.Schema {
// to lowercase
return strings.ToLower(val.(string))
},
ValidateFunc: validateOnceAWeekWindowFormat,
},
"port": &schema.Schema{
Type: schema.TypeInt,

View File

@ -160,9 +160,10 @@ func resourceAwsRDSCluster() *schema.Resource {
},
"preferred_backup_window": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateOnceADayWindowFormat,
},
"preferred_maintenance_window": {
@ -175,6 +176,7 @@ func resourceAwsRDSCluster() *schema.Resource {
}
return strings.ToLower(val.(string))
},
ValidateFunc: validateOnceAWeekWindowFormat,
},
"backup_retention_period": {

View File

@ -101,6 +101,7 @@ func resourceAwsRedshiftCluster() *schema.Resource {
}
return strings.ToLower(val.(string))
},
ValidateFunc: validateOnceAWeekWindowFormat,
},
"cluster_parameter_group_name": {

View File

@ -624,3 +624,27 @@ func validateSecurityRuleType(v interface{}, k string) (ws []string, errors []er
}
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
}

View File

@ -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)
}
}
}