Skip to content

Commit

Permalink
Merge pull request #386 from drastawi/no-start-day-for-daily-restriction
Browse files Browse the repository at this point in the history
Ensure start_day_of_week is not specified for a daily schedule restriction
  • Loading branch information
Scott McAllister authored Sep 27, 2021
2 parents c97d2da + 1f5bdc3 commit 5c859cc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pagerduty/resource_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ func resourcePagerDutySchedule() *schema.Resource {
Read: resourcePagerDutyScheduleRead,
Update: resourcePagerDutyScheduleUpdate,
Delete: resourcePagerDutyScheduleDelete,
CustomizeDiff: func(diff *schema.ResourceDiff, i interface{}) error {
ln := diff.Get("layer.#").(int)
for li := 0; li <= ln; li++ {
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)
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")
}
}
}
return nil
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -103,6 +116,10 @@ func resourcePagerDutySchedule() *schema.Resource {
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateValueFunc([]string{
"daily_restriction",
"weekly_restriction",
}),
},

"start_time_of_day": {
Expand Down
36 changes: 36 additions & 0 deletions pagerduty/resource_pagerduty_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"fmt"
"log"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -102,6 +103,10 @@ func TestAccPagerDutySchedule_Basic(t *testing.T) {
"pagerduty_schedule.foo", "layer.0.rotation_virtual_start", rotationVirtualStart),
),
},
{
Config: testAccCheckPagerDutyScheduleConfigRestrictionType(username, email, schedule, location, start, rotationVirtualStart),
ExpectError: regexp.MustCompile("start_day_of_week must only be set for a weekly_restriction schedule restriction type"),
},
},
})
}
Expand Down Expand Up @@ -434,6 +439,37 @@ resource "pagerduty_schedule" "foo" {
`, username, email, schedule, location, start, rotationVirtualStart)
}

func testAccCheckPagerDutyScheduleConfigRestrictionType(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 = 32101
start_day_of_week = 5
}
}
}
`, username, email, schedule, location, start, rotationVirtualStart)
}

func testAccCheckPagerDutyScheduleConfigUpdated(username, email, schedule, location, start, rotationVirtualStart string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
Expand Down

0 comments on commit 5c859cc

Please sign in to comment.