Skip to content

Commit

Permalink
Add validation for start_time to resource_policy
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
chrisst committed Jan 9, 2020
1 parent 430c83e commit 644697d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
19 changes: 11 additions & 8 deletions google/resource_compute_resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ which cannot be a dash.`,
Description: `The number of days between snapshots.`,
},
"start_time": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateHourlyOnly,
Description: `This must be in UTC format that resolves to one of
00:00, 04:00, 08:00, 12:00, 16:00, or 20:00. For example,
both 13:00-5 and 08:00 are valid.`,
Expand All @@ -118,12 +119,14 @@ both 13:00-5 and 08:00 are valid.`,
Description: `The number of hours between snapshots.`,
},
"start_time": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateHourlyOnly,
Description: `Time within the window to start the operations.
It must be in format "HH:MM",
where HH : [00-23] and MM : [00-00] GMT.`,
It must be in an hourly format "HH:MM",
where HH : [00-23] and MM : [00] GMT.
eg: 21:00`,
},
},
},
Expand Down
20 changes: 20 additions & 0 deletions google/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,23 @@ func StringNotInSlice(invalid []string, ignoreCase bool) schema.SchemaValidateFu
return
}
}

// Ensure that hourly timestamp strings "HH:MM" have the minutes zeroed out for hourly only inputs
func validateHourlyOnly(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
parts := strings.Split(v, ":")
if len(parts) != 2 {
errs = append(errs, fmt.Errorf("%q must be in the format HH:00, got: %s", key, v))
return
}
if parts[1] != "00" {
errs = append(errs, fmt.Errorf("%q does not allow minutes, it must be in the format HH:00, got: %s", key, v))
}
i, err := strconv.Atoi(parts[0])
if err != nil {
errs = append(errs, fmt.Errorf("%q cannot be parsed, it must be in the format HH:00, got: %s", key, v))
} else if i < 0 || i > 23 {
errs = append(errs, fmt.Errorf("%q does not specify a valid hour, it must be in the format HH:00 where HH : [00-23], got: %s", key, v))
}
return
}
5 changes: 3 additions & 2 deletions website/docs/r/compute_resource_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ The `hourly_schedule` block supports:
* `start_time` -
(Required)
Time within the window to start the operations.
It must be in format "HH:MM",
where HH : [00-23] and MM : [00-00] GMT.
It must be in an hourly format "HH:MM",
where HH : [00-23] and MM : [00] GMT.
eg: 21:00

The `daily_schedule` block supports:

Expand Down

0 comments on commit 644697d

Please sign in to comment.