diff --git a/examples/resources/grafana_oncall_schedule/resource.tf b/examples/resources/grafana_oncall_schedule/resource.tf index ca75c2c16..3910fc535 100644 --- a/examples/resources/grafana_oncall_schedule/resource.tf +++ b/examples/resources/grafana_oncall_schedule/resource.tf @@ -42,3 +42,19 @@ resource "grafana_oncall_schedule" "example_schedule" { ] ical_url_overrides = "https://example.com/example_overrides_ical.ics" } + +// Web based schedule +resource "grafana_oncall_schedule" "example_schedule" { + name = "Example Calendar Schadule" + type = "web" + time_zone = "America/New_York" + + // Optional: specify the team to which the schedule belongs + team_id = data.grafana_oncall_team.my_team.id + + // Is highly recommended to set ignore changes on all properties to avoid + // recreating the schedule all the time due to changes on the Web UI. + lifecycle { + ignore_changes = all + } +} diff --git a/internal/resources/oncall/resource_schedule.go b/internal/resources/oncall/resource_schedule.go index b9e23d3ed..550a0b61d 100644 --- a/internal/resources/oncall/resource_schedule.go +++ b/internal/resources/oncall/resource_schedule.go @@ -32,6 +32,8 @@ func resourceSchedule() *common.Resource { StateContext: schema.ImportStatePassthroughContext, }, + CustomizeDiff: resourceScheduleCustomizeDiff, + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -173,10 +175,10 @@ func resourceScheduleCreate(ctx context.Context, d *schema.ResourceData, client timeZoneData, timeZoneOk := d.GetOk("time_zone") if timeZoneOk { - if isScheduleTypeCalendar(typeData) { - createOptions.TimeZone = timeZoneData.(string) - } else { + if isScheduleTypeiCal(typeData) { return diag.Errorf("time_zone can not be set with type: %s", typeData) + } else { + createOptions.TimeZone = timeZoneData.(string) } } @@ -226,10 +228,10 @@ func resourceScheduleUpdate(ctx context.Context, d *schema.ResourceData, client timeZoneData, timeZoneOk := d.GetOk("time_zone") if timeZoneOk { - if isScheduleTypeCalendar(typeData) { - updateOptions.TimeZone = timeZoneData.(string) - } else { + if isScheduleTypeiCal(typeData) { return diag.Errorf("time_zone can not be set with type: %s", typeData) + } else { + updateOptions.TimeZone = timeZoneData.(string) } } @@ -325,3 +327,31 @@ func expandScheduleSlack(in []any) *onCallAPI.SlackSchedule { func isScheduleTypeCalendar(t string) bool { return t == "calendar" } + +func isScheduleTypeiCal(t string) bool { + return t == "ical" +} + +func resourceScheduleCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, m interface{}) error { + oldType, _ := d.GetChange("type") + + if oldType == nil { + return nil + } + + if oldType.(string) == "web" { + for _, key := range d.GetChangedKeysPrefix("") { + if key == "id" { + continue + } + + if err := d.ForceNew(key); err != nil { + return err + } + + return nil + } + } + + return nil +}