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 <magic-modules@google.com>
  • Loading branch information
chrisst authored and modular-magician committed Jan 8, 2020
1 parent b0ee45e commit 2855ecf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 8 additions & 6 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,9 +119,10 @@ 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.`,
Expand Down
13 changes: 13 additions & 0 deletions google/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,16 @@ 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))
}
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))
}
return
}

0 comments on commit 2855ecf

Please sign in to comment.