From d338896f2c7b1b42057092d271132d550f9114d8 Mon Sep 17 00:00:00 2001 From: Chris Stephens Date: Thu, 9 Jan 2020 13:27:10 -0800 Subject: [PATCH] Add validation for start_time to resource_policy (#2940) Merged PR #2940. --- build/terraform | 2 +- build/terraform-beta | 2 +- build/terraform-mapper | 2 +- products/compute/api.yaml | 5 +++-- products/compute/terraform.yaml | 6 ++++++ third_party/terraform/utils/validation.go | 20 ++++++++++++++++++++ 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/build/terraform b/build/terraform index 430c83e2cec5..644697d5b8e5 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 430c83e2cec5e6cdc2ebb28c3bde2df7b59b2f37 +Subproject commit 644697d5b8e5e0b60b94c1583c49e8d85a8a59c8 diff --git a/build/terraform-beta b/build/terraform-beta index 7dd278efea15..f23b83d134b7 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 7dd278efea150954e25c1073add3b1a71ffc8703 +Subproject commit f23b83d134b7b68b7bed72d00f682d47ed7102c8 diff --git a/build/terraform-mapper b/build/terraform-mapper index fd5194a5227b..a16f7ed62114 160000 --- a/build/terraform-mapper +++ b/build/terraform-mapper @@ -1 +1 @@ -Subproject commit fd5194a5227b8ddaca919852b62ce713e1700aa7 +Subproject commit a16f7ed62114e9ed8c02f74b46c884e1c2d2b92f diff --git a/products/compute/api.yaml b/products/compute/api.yaml index a56d5eed512e..e344ecc2190c 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -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' diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index f082dc4ad25c..99b4b7e6451e 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -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 diff --git a/third_party/terraform/utils/validation.go b/third_party/terraform/utils/validation.go index 3a59f006b4e7..7ce1ccd2093e 100644 --- a/third_party/terraform/utils/validation.go +++ b/third_party/terraform/utils/validation.go @@ -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 +}