diff --git a/pagerduty/resource_pagerduty_schedule.go b/pagerduty/resource_pagerduty_schedule.go index e754f1578..0d5891fee 100644 --- a/pagerduty/resource_pagerduty_schedule.go +++ b/pagerduty/resource_pagerduty_schedule.go @@ -25,12 +25,15 @@ 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") } - ds := diff.Get(fmt.Sprintf("layer.%d.restriction.%d.duration_seconds", li, ri)).(int) - if t == "daily_restriction" && ds >= 3600*24 { - return fmt.Errorf("duration_seconds for a daily_restriction schedule restriction type must be shorter than a day") + 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()) } } } 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) +}