Skip to content

Commit

Permalink
Add validation for start_time to resource_policy (#2940)
Browse files Browse the repository at this point in the history
Merged PR #2940.
  • Loading branch information
chrisst authored and modular-magician committed Jan 9, 2020
1 parent c256bb5 commit d338896
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
5 changes: 3 additions & 2 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10087,8 +10087,9 @@ objects:
name: 'startTime'
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
required: true
- !ruby/object:Api::Type::NestedObject
name: 'dailySchedule'
Expand Down
6 changes: 6 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,12 @@ overrides: !ruby/object:Overrides::ResourceOverrides
region: !ruby/object:Overrides::Terraform::PropertyOverride
required: false
default_from_api: true
snapshotSchedulePolicy.schedule.dailySchedule.startTime: !ruby/object:Overrides::Terraform::PropertyOverride
validation: !ruby/object:Provider::Terraform::Validation
function: 'validateHourlyOnly'
snapshotSchedulePolicy.schedule.hourlySchedule.startTime: !ruby/object:Overrides::Terraform::PropertyOverride
validation: !ruby/object:Provider::Terraform::Validation
function: 'validateHourlyOnly'
snapshotSchedulePolicy.schedule.weeklySchedule.dayOfWeeks: !ruby/object:Overrides::Terraform::PropertyOverride
is_set: true
snapshotSchedulePolicy.snapshotProperties.storageLocations: !ruby/object:Overrides::Terraform::PropertyOverride
Expand Down
20 changes: 20 additions & 0 deletions third_party/terraform/utils/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
}

0 comments on commit d338896

Please sign in to comment.