Skip to content

Commit

Permalink
Check oncall rolling_users shift does not include empty group (#1685)
Browse files Browse the repository at this point in the history
* Check oncall rolling_users shift does not include empty group

* Add test for error case
  • Loading branch information
matiasb authored Jul 16, 2024
1 parent b2ecce9 commit 9d29e94
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/resources/oncall/resource_shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ func resourceOnCallShiftCreate(ctx context.Context, d *schema.ResourceData, clie
rollingUsersData, rollingUsersOk := d.GetOk(rollingUsers)
if rollingUsersOk {
if typeData == rollingUsers {
listSet := rollingUsersData.([]interface{})
for _, set := range listSet {
if set == nil {
return diag.Errorf("`rolling_users` can not include an empty group")
}
}
rollingUsersDataSlice := common.ListOfSetsToStringSlice(rollingUsersData.([]interface{}))
createOptions.RollingUsers = &rollingUsersDataSlice
} else {
Expand Down Expand Up @@ -419,6 +425,12 @@ func resourceOnCallShiftUpdate(ctx context.Context, d *schema.ResourceData, clie
rollingUsersData, rollingUsersOk := d.GetOk(rollingUsers)
if rollingUsersOk {
if typeData == rollingUsers {
listSet := rollingUsersData.([]interface{})
for _, set := range listSet {
if set == nil {
return diag.Errorf("`rolling_users` can not include an empty group")
}
}
rollingUsersDataSlice := common.ListOfSetsToStringSlice(rollingUsersData.([]interface{}))
updateOptions.RollingUsers = &rollingUsersDataSlice
} else {
Expand Down
58 changes: 58 additions & 0 deletions internal/resources/oncall/resource_shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oncall_test

import (
"fmt"
"regexp"
"testing"

onCallAPI "github.com/grafana/amixr-api-go-client"
Expand Down Expand Up @@ -46,6 +47,17 @@ func TestAccOnCallOnCallShift_basic(t *testing.T) {
resource.TestCheckResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "frequency", "hourly"),
),
},
{
Config: testAccOnCallOnCallShiftEmptyRollingUsers(scheduleName, shiftName),
Check: resource.ComposeTestCheckFunc(
testAccCheckOnCallOnCallShiftResourceExists("grafana_oncall_on_call_shift.test-acc-on_call_shift"),
resource.TestCheckResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "rolling_users.#", "0"),
),
},
{
Config: testAccOnCallOnCallShiftRollingUsersEmptyGroup(scheduleName, shiftName),
ExpectError: regexp.MustCompile("Error: `rolling_users` can not include an empty group"),
},
{
Config: testAccOnCallOnCallShiftConfigSingle(scheduleName, shiftName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -113,6 +125,52 @@ resource "grafana_oncall_on_call_shift" "test-acc-on_call_shift" {
`, scheduleName, shiftName)
}

func testAccOnCallOnCallShiftEmptyRollingUsers(scheduleName, shiftName string) string {
return fmt.Sprintf(`
resource "grafana_oncall_schedule" "test-acc-schedule" {
type = "calendar"
name = "%s"
time_zone = "UTC"
}
resource "grafana_oncall_on_call_shift" "test-acc-on_call_shift" {
name = "%s"
type = "rolling_users"
start = "2020-09-04T16:00:00"
duration = 3600
level = 1
frequency = "weekly"
week_start = "SU"
interval = 2
by_day = ["MO", "FR"]
rolling_users = []
}
`, scheduleName, shiftName)
}

func testAccOnCallOnCallShiftRollingUsersEmptyGroup(scheduleName, shiftName string) string {
return fmt.Sprintf(`
resource "grafana_oncall_schedule" "test-acc-schedule" {
type = "calendar"
name = "%s"
time_zone = "UTC"
}
resource "grafana_oncall_on_call_shift" "test-acc-on_call_shift" {
name = "%s"
type = "rolling_users"
start = "2020-09-04T16:00:00"
duration = 3600
level = 1
frequency = "weekly"
week_start = "SU"
interval = 2
by_day = ["MO", "FR"]
rolling_users = [[]]
}
`, scheduleName, shiftName)
}

func testAccOnCallOnCallShiftConfigSingle(scheduleName, shiftName string) string {
return fmt.Sprintf(`
resource "grafana_oncall_schedule" "test-acc-schedule" {
Expand Down

0 comments on commit 9d29e94

Please sign in to comment.