Skip to content

Commit

Permalink
feat: add until parameter to grafana_oncall_on_call_shift resource (
Browse files Browse the repository at this point in the history
  • Loading branch information
maltelehmann authored Oct 3, 2024
1 parent a4a2b46 commit c20d360
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/resources/oncall_on_call_shift.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ output "emea_weekday__rolling_users" {
- `start_rotation_from_user_index` (Number) The index of the list of users in rolling_users, from which on-call rotation starts.
- `team_id` (String) The ID of the OnCall team. To get one, create a team in Grafana, and navigate to the OnCall plugin (to sync the team with OnCall). You can then get the ID using the `grafana_oncall_team` datasource.
- `time_zone` (String) The shift's timezone. Overrides schedule's timezone.
- `until` (String) The end time of recurrent on-call shifts (endless if null). This parameter takes a date format as yyyy-MM-dd'T'HH:mm:ss (for example "2020-09-05T08:00:00")
- `users` (Set of String) The list of on-call users (for single_event and recurrent_event event type).
- `week_start` (String) Start day of the week in iCal format. Can be MO, TU, WE, TH, FR, SA, SU

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/fatih/color v1.17.0
github.com/go-openapi/runtime v0.28.0
github.com/go-openapi/strfmt v0.23.0
github.com/grafana/amixr-api-go-client v0.0.13 // main branch
github.com/grafana/amixr-api-go-client v0.0.15 // main branch
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240807172819-ac10800522a3
github.com/grafana/grafana-openapi-client-go v0.0.0-20240723170622-ae2c94b7c9a3
github.com/grafana/machine-learning-go-client v0.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/grafana/amixr-api-go-client v0.0.13 h1:MwZ2DHnFOWY9EX7sMCd1GYvQSX6ZeEOiiONkomHQUNA=
github.com/grafana/amixr-api-go-client v0.0.13/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/amixr-api-go-client v0.0.15 h1:kq2C8QIsTm1lA7i8S5UA1PjeUJkSIRpNVXQXFLlxRAE=
github.com/grafana/amixr-api-go-client v0.0.15/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240807172819-ac10800522a3 h1:CVLTffnWgBGvVaXfUUcSgFrZbiMzvj0/Hpi909zdeG0=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240807172819-ac10800522a3/go.mod h1:u9d0BESoKlztYm93CpoRleQjMbYBcZ+JOLHHP2nN6Wg=
github.com/grafana/grafana-openapi-client-go v0.0.0-20240723170622-ae2c94b7c9a3 h1:W35ScJIkeyLuDlOo3F+u1JSRRvmoIYYf/ghA/17Y18Q=
Expand Down
27 changes: 27 additions & 0 deletions internal/resources/oncall/resource_shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func resourceOnCallShift() *common.Resource {
"interval",
},
},
"until": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The end time of recurrent on-call shifts (endless if null). This parameter takes a date format as yyyy-MM-dd'T'HH:mm:ss (for example \"2020-09-05T08:00:00\")",
},
"users": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Expand Down Expand Up @@ -307,6 +313,16 @@ func resourceOnCallShiftCreate(ctx context.Context, d *schema.ResourceData, clie
}
}

untilData, untilOk := d.GetOk("until")
if untilOk {
if typeData == singleEvent {
return diag.Errorf("`until` can not be set with type: %s", typeData)
} else {
u := untilData.(string)
createOptions.Until = &u
}
}

timeZoneData, timeZoneOk := d.GetOk("time_zone")
if timeZoneOk {
tz := timeZoneData.(string)
Expand Down Expand Up @@ -418,6 +434,16 @@ func resourceOnCallShiftUpdate(ctx context.Context, d *schema.ResourceData, clie
}
}

untilData, untilOk := d.GetOk("until")
if untilOk {
if typeData == singleEvent {
return diag.Errorf("`until` can not be set with type: %s", typeData)
} else {
u := untilData.(string)
updateOptions.Until = &u
}
}

timeZoneData, timeZoneOk := d.GetOk("time_zone")
if timeZoneOk {
tz := timeZoneData.(string)
Expand Down Expand Up @@ -471,6 +497,7 @@ func resourceOnCallShiftRead(ctx context.Context, d *schema.ResourceData, client
d.Set("type", onCallShift.Type)
d.Set("level", onCallShift.Level)
d.Set("start", onCallShift.Start)
d.Set("until", onCallShift.Until)
d.Set("duration", onCallShift.Duration)
d.Set("frequency", onCallShift.Frequency)
d.Set("week_start", onCallShift.WeekStart)
Expand Down
31 changes: 31 additions & 0 deletions internal/resources/oncall/resource_shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestAccOnCallOnCallShift_basic(t *testing.T) {
resource.TestCheckResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "by_day.#", "2"),
resource.TestCheckResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "by_day.0", "FR"),
resource.TestCheckResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "by_day.1", "MO"),
resource.TestCheckNoResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "until"),
),
},
{
Expand Down Expand Up @@ -65,6 +66,13 @@ func TestAccOnCallOnCallShift_basic(t *testing.T) {
resource.TestCheckResourceAttr("grafana_oncall_on_call_shift.test-acc-on_call_shift", "name", shiftName),
),
},
{
Config: testAccOnCallOnCallShiftConfigUntil(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", "until", "2020-10-04T16:00:00"),
),
},
},
})
}
Expand Down Expand Up @@ -188,6 +196,29 @@ resource "grafana_oncall_on_call_shift" "test-acc-on_call_shift" {
`, scheduleName, shiftName)
}

func testAccOnCallOnCallShiftConfigUntil(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 = "recurrent_event"
start = "2020-09-04T16:00:00"
until = "2020-10-04T16:00:00"
duration = 3600
level = 1
frequency = "weekly"
week_start = "SU"
interval = 2
by_day = ["MO", "FR"]
}
`, scheduleName, shiftName)
}

func testAccCheckOnCallOnCallShiftResourceExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down

0 comments on commit c20d360

Please sign in to comment.