From 3b0372f6f2559708b7b9509cecaa4275d41d59f2 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 24 Dec 2021 18:54:54 +0800 Subject: [PATCH] schedule: Validate daily/weekly restrictions Do not allow a daily restriction that spans a full 24*time.Hour and similar for weekly restrictions. The PD API rejects 24*time.Hour and 24*7*time.Hour for the API. --- pagerduty/resource_pagerduty_schedule.go | 7 ++ pagerduty/resource_pagerduty_schedule_test.go | 70 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/pagerduty/resource_pagerduty_schedule.go b/pagerduty/resource_pagerduty_schedule.go index fecb0bafb..43a0e4422 100644 --- a/pagerduty/resource_pagerduty_schedule.go +++ b/pagerduty/resource_pagerduty_schedule.go @@ -25,9 +25,16 @@ func resourcePagerDutySchedule() *schema.Resource { rn := diff.Get(fmt.Sprintf("layer.%d.restriction.#", li)).(int) for ri := 0; ri <= rn; ri++ { t := diff.Get(fmt.Sprintf("layer.%d.restriction.%d.type", li, ri)).(string) + d := diff.Get(fmt.Sprintf("layer.%d.restriction.%d.duration_seconds", li, ri)).(int) if t == "daily_restriction" && diff.Get(fmt.Sprintf("layer.%d.restriction.%d.start_day_of_week", li, ri)).(int) != 0 { return fmt.Errorf("start_day_of_week must only be set for a weekly_restriction schedule restriction type") } + if t == "weekly_restriction" && d >= int((time.Hour*24*7).Seconds()) { + return fmt.Errorf("weekly restriction must be less than a week: %v", (time.Hour * 24 * 7).Seconds()) + } + if t == "daily_restriction" && d >= int((time.Hour*24).Seconds()) { + return fmt.Errorf("daily restriction must be less than a day: %v", (time.Hour * 24).Seconds()) + } } } return nil diff --git a/pagerduty/resource_pagerduty_schedule_test.go b/pagerduty/resource_pagerduty_schedule_test.go index 4ed90a954..32fecfe97 100644 --- a/pagerduty/resource_pagerduty_schedule_test.go +++ b/pagerduty/resource_pagerduty_schedule_test.go @@ -262,6 +262,11 @@ func TestAccPagerDutySchedule_BasicWeek(t *testing.T) { "pagerduty_schedule.foo", "layer.0.rotation_virtual_start", rotationVirtualStart), ), }, + { + Config: testAccCheckPagerDutyScheduleConfigWeekUpdatedInvalidWeeklyRestrictions(username, email, scheduleUpdated, location, start, rotationVirtualStart), + PlanOnly: true, + ExpectError: regexp.MustCompile("weekly restriction must be less than a week"), + }, }, }) } @@ -365,6 +370,11 @@ func TestAccPagerDutySchedule_Multi(t *testing.T) { "pagerduty_schedule.foo", "layer.#", "2"), ), }, + { + Config: testAccCheckPagerDutyScheduleConfigInvalidDailyRestriction(username, email, schedule, location, start, rotationVirtualStart), + PlanOnly: true, + ExpectError: regexp.MustCompile("daily restriction must be less than a day:"), + }, }, }) } @@ -800,3 +810,63 @@ resource "pagerduty_schedule" "foo" { } `, username, email, team, schedule, location, start, rotationVirtualStart) } + +func testAccCheckPagerDutyScheduleConfigInvalidDailyRestriction(username, email, schedule, location, start, rotationVirtualStart string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "foo" { + name = "%s" + email = "%s" +} + +resource "pagerduty_schedule" "foo" { + name = "%s" + + time_zone = "%s" + description = "foo" + + layer { + name = "foo" + start = "%s" + rotation_virtual_start = "%s" + rotation_turn_length_seconds = 86400 + users = [pagerduty_user.foo.id] + + restriction { + type = "daily_restriction" + start_time_of_day = "08:00:00" + duration_seconds = 86400 + } + } +} +`, username, email, schedule, location, start, rotationVirtualStart) +} + +func testAccCheckPagerDutyScheduleConfigWeekUpdatedInvalidWeeklyRestrictions(username, email, schedule, location, start, rotationVirtualStart string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "foo" { + name = "%s" + email = "%s" +} + +resource "pagerduty_schedule" "foo" { + name = "%s" + + time_zone = "%s" + + layer { + name = "foo" + start = "%s" + rotation_virtual_start = "%s" + rotation_turn_length_seconds = 86400 + users = [pagerduty_user.foo.id] + + restriction { + type = "weekly_restriction" + start_time_of_day = "08:00:00" + start_day_of_week = 5 + duration_seconds = 604800 + } + } +} +`, username, email, schedule, location, start, rotationVirtualStart) +}