From 9793d437d70186e9f48cf99fa4ba5c7cbf6eac8f Mon Sep 17 00:00:00 2001 From: Malte Lehmann Date: Wed, 2 Oct 2024 20:13:24 +0200 Subject: [PATCH] feat: add `until` parameter to grafana_oncall_on_call_shift resource fixes https://github.com/grafana/terraform-provider-grafana/issues/1821 --- internal/resources/oncall/resource_shift.go | 27 ++++++++++++++++ .../resources/oncall/resource_shift_test.go | 31 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/internal/resources/oncall/resource_shift.go b/internal/resources/oncall/resource_shift.go index c8862d0ce..dc6255007 100644 --- a/internal/resources/oncall/resource_shift.go +++ b/internal/resources/oncall/resource_shift.go @@ -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{ @@ -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) @@ -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) @@ -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) diff --git a/internal/resources/oncall/resource_shift_test.go b/internal/resources/oncall/resource_shift_test.go index e92cd9b10..b19b85e12 100644 --- a/internal/resources/oncall/resource_shift_test.go +++ b/internal/resources/oncall/resource_shift_test.go @@ -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"), ), }, { @@ -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"), + ), + }, }, }) } @@ -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]