Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schedule: Validate daily/weekly restrictions #439

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pagerduty/resource_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Expand Down
70 changes: 70 additions & 0 deletions pagerduty/resource_pagerduty_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
},
})
}
Expand Down Expand Up @@ -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:"),
},
},
})
}
Expand Down Expand Up @@ -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)
}