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 committed Oct 3, 2024
1 parent a150487 commit 9793d43
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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"
start = "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 9793d43

Please sign in to comment.