-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathresource_schedule.go
322 lines (281 loc) · 9.34 KB
/
resource_schedule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package oncall
import (
"context"
"log"
"net/http"
"strings"
onCallAPI "github.com/grafana/amixr-api-go-client"
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
var scheduleTypeOptions = []string{
"ical",
"calendar",
}
func resourceSchedule() *common.Resource {
schema := &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/schedules/)
`,
CreateContext: withClient[schema.CreateContextFunc](resourceScheduleCreate),
ReadContext: withClient[schema.ReadContextFunc](resourceScheduleRead),
UpdateContext: withClient[schema.UpdateContextFunc](resourceScheduleUpdate),
DeleteContext: withClient[schema.DeleteContextFunc](resourceScheduleDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The schedule's name.",
},
"team_id": {
Type: schema.TypeString,
Optional: true,
Description: "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.",
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(scheduleTypeOptions, false),
Description: "The schedule's type. Valid values are `" + strings.Join(scheduleTypeOptions, "`, `") + "`.",
},
"time_zone": {
Type: schema.TypeString,
Optional: true,
Description: "The schedule's time zone.",
},
"ical_url_primary": {
Type: schema.TypeString,
Optional: true,
Description: "The URL of the external calendar iCal file.",
},
"ical_url_overrides": {
Type: schema.TypeString,
Optional: true,
Description: "The URL of external iCal calendar which override primary events.",
},
"enable_web_overrides": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable overrides via web UI (it will ignore ical_url_overrides).",
},
"slack": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"channel_id": {
Type: schema.TypeString,
Optional: true,
Description: "Slack channel id. Reminder about schedule shifts will be directed to this channel in Slack.",
},
"user_group_id": {
Type: schema.TypeString,
Optional: true,
Description: " Slack user group id. Members of user group will be updated when on-call users change.",
},
},
},
MaxItems: 1,
Description: "The Slack-specific settings for a schedule.",
},
"shifts": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The list of ID's of on-call shifts.",
},
},
}
return common.NewLegacySDKResource(
common.CategoryOnCall,
"grafana_oncall_schedule",
resourceID,
schema,
).WithLister(oncallListerFunction(listSchedules))
}
func listSchedules(client *onCallAPI.Client, listOptions onCallAPI.ListOptions) (ids []string, nextPage *string, err error) {
resp, _, err := client.Schedules.ListSchedules(&onCallAPI.ListScheduleOptions{ListOptions: listOptions})
if err != nil {
return nil, nil, err
}
for _, i := range resp.Schedules {
ids = append(ids, i.ID)
}
return ids, resp.Next, nil
}
func resourceScheduleCreate(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
nameData := d.Get("name").(string)
teamIDData := d.Get("team_id").(string)
typeData := d.Get("type").(string)
slackData := d.Get("slack").([]interface{})
createOptions := &onCallAPI.CreateScheduleOptions{
TeamId: teamIDData,
Name: nameData,
Type: typeData,
Slack: expandScheduleSlack(slackData),
}
iCalURLPrimaryData, iCalURLPrimaryOk := d.GetOk("ical_url_primary")
if iCalURLPrimaryOk {
if typeData == "ical" {
url := iCalURLPrimaryData.(string)
createOptions.ICalUrlPrimary = &url
} else {
return diag.Errorf("ical_url_primary can not be set with type: %s", typeData)
}
}
iCalURLOverridesData, iCalURLOverridesOk := d.GetOk("ical_url_overrides")
if iCalURLOverridesOk {
url := iCalURLOverridesData.(string)
createOptions.ICalUrlOverrides = &url
}
enableWebOverridesData, enableWebOverridesOk := d.GetOk("enable_web_overrides")
if enableWebOverridesOk {
enable := enableWebOverridesData.(bool)
createOptions.EnableWebOverrides = enable
}
shiftsData, shiftsOk := d.GetOk("shifts")
if shiftsOk {
if typeData == "calendar" {
shiftsDataSlice := common.SetToStringSlice(shiftsData.(*schema.Set))
createOptions.Shifts = &shiftsDataSlice
} else {
return diag.Errorf("shifts can not be set with type: %s", typeData)
}
}
timeZoneData, timeZoneOk := d.GetOk("time_zone")
if timeZoneOk {
if typeData == "calendar" {
createOptions.TimeZone = timeZoneData.(string)
} else {
return diag.Errorf("time_zone can not be set with type: %s", typeData)
}
}
schedule, _, err := client.Schedules.CreateSchedule(createOptions)
if err != nil {
return diag.FromErr(err)
}
d.SetId(schedule.ID)
return resourceScheduleRead(ctx, d, client)
}
func resourceScheduleUpdate(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
nameData := d.Get("name").(string)
teamIDData := d.Get("team_id").(string)
slackData := d.Get("slack").([]interface{})
typeData := d.Get("type").(string)
updateOptions := &onCallAPI.UpdateScheduleOptions{
Name: nameData,
TeamId: teamIDData,
Slack: expandScheduleSlack(slackData),
}
iCalURLPrimaryData, iCalURLPrimaryOk := d.GetOk("ical_url_primary")
if iCalURLPrimaryOk {
if typeData == "ical" {
url := iCalURLPrimaryData.(string)
updateOptions.ICalUrlPrimary = &url
} else {
return diag.Errorf("ical_url_primary can not be set with type: %s", typeData)
}
}
iCalURLOverridesData, iCalURLOverridesOk := d.GetOk("ical_url_overrides")
if iCalURLOverridesOk {
url := iCalURLOverridesData.(string)
updateOptions.ICalUrlOverrides = &url
}
enableWebOverridesData, enableWebOverridesOk := d.GetOk("enable_web_overrides")
if enableWebOverridesOk {
enable := enableWebOverridesData.(bool)
updateOptions.EnableWebOverrides = enable
}
timeZoneData, timeZoneOk := d.GetOk("time_zone")
if timeZoneOk {
if typeData == "calendar" {
updateOptions.TimeZone = timeZoneData.(string)
} else {
return diag.Errorf("time_zone can not be set with type: %s", typeData)
}
}
shiftsData, shiftsOk := d.GetOk("shifts")
if shiftsOk {
if typeData == "calendar" {
shiftsDataSlice := common.SetToStringSlice(shiftsData.(*schema.Set))
updateOptions.Shifts = &shiftsDataSlice
} else {
return diag.Errorf("shifts can not be set with type: %s", typeData)
}
}
schedule, _, err := client.Schedules.UpdateSchedule(d.Id(), updateOptions)
if err != nil {
return diag.FromErr(err)
}
d.SetId(schedule.ID)
return resourceScheduleRead(ctx, d, client)
}
func resourceScheduleRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.GetScheduleOptions{}
schedule, r, err := client.Schedules.GetSchedule(d.Id(), options)
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing schedule %s from state because it no longer exists", d.Get("name").(string))
d.SetId("")
return nil
}
return diag.FromErr(err)
}
d.Set("name", schedule.Name)
d.Set("team_id", schedule.TeamId)
d.Set("type", schedule.Type)
d.Set("ical_url_primary", schedule.ICalUrlPrimary)
d.Set("ical_url_overrides", schedule.ICalUrlOverrides)
d.Set("enable_web_overrides", schedule.EnableWebOverrides)
d.Set("time_zone", schedule.TimeZone)
d.Set("slack", flattenScheduleSlack(schedule.Slack))
d.Set("shifts", schedule.Shifts)
return nil
}
func resourceScheduleDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.DeleteScheduleOptions{}
_, err := client.Schedules.DeleteSchedule(d.Id(), options)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func flattenScheduleSlack(in *onCallAPI.SlackSchedule) []map[string]interface{} {
slack := make([]map[string]interface{}, 0, 1)
out := make(map[string]interface{})
if in.ChannelId != nil {
out["channel_id"] = in.ChannelId
}
if in.UserGroupId != nil {
out["user_group_id"] = in.UserGroupId
}
if in.ChannelId != nil || in.UserGroupId != nil {
slack = append(slack, out)
}
return slack
}
func expandScheduleSlack(in []interface{}) *onCallAPI.SlackSchedule {
slackSchedule := onCallAPI.SlackSchedule{}
for _, r := range in {
inputMap := r.(map[string]interface{})
if inputMap["channel_id"] != "" {
channelID := inputMap["channel_id"].(string)
slackSchedule.ChannelId = &channelID
}
if inputMap["user_group_id"] != "" {
userGroupID := inputMap["user_group_id"].(string)
slackSchedule.UserGroupId = &userGroupID
}
}
return &slackSchedule
}