forked from grafana/amixr-api-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
177 lines (145 loc) · 5.24 KB
/
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
package aapi
import (
"fmt"
"net/http"
)
// ScheduleService handles requests to schedule endpoint
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/
type ScheduleService struct {
client *Client
url string
}
// NewScheduleService creates ScheduleService with defined url
func NewScheduleService(client *Client) *ScheduleService {
scheduleService := ScheduleService{}
scheduleService.client = client
scheduleService.url = "schedules"
return &scheduleService
}
type PaginatedSchedulesResponse struct {
PaginatedResponse
Schedules []*Schedule `json:"results"`
}
type Schedule struct {
ID string `json:"id"`
TeamId string `json:"team_id"`
Type string `json:"type"`
OnCallNow []string `json:"on_call_now"`
Name string `json:"name"`
ICalUrlPrimary *string `json:"ical_url_primary"`
ICalUrlOverrides *string `json:"ical_url_overrides"`
EnableWebOverrides bool `json:"enable_web_overrides"`
TimeZone string `json:"time_zone"`
Slack *SlackSchedule `json:"slack"`
Shifts *[]string `json:"shifts"`
}
type SlackSchedule struct {
ChannelId *string `json:"channel_id"`
UserGroupId *string `json:"user_group_id"`
}
type ListScheduleOptions struct {
ListOptions
Name string `url:"name,omitempty" json:"name,omitempty"`
}
// ListSchedules fetches all schedules for authorized organization
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/#list-schedules
func (service *ScheduleService) ListSchedules(opt *ListScheduleOptions) (*PaginatedSchedulesResponse, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)
req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}
var schedules *PaginatedSchedulesResponse
resp, err := service.client.Do(req, &schedules)
if err != nil {
return nil, resp, err
}
return schedules, resp, err
}
type GetScheduleOptions struct {
}
// GetSchedule fetches a schedule by given id
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/#get-a-schedule
func (service *ScheduleService) GetSchedule(id string, opt *GetScheduleOptions) (*Schedule, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}
schedule := new(Schedule)
resp, err := service.client.Do(req, schedule)
if err != nil {
return nil, resp, err
}
return schedule, resp, err
}
type CreateScheduleOptions struct {
TeamId string `json:"team_id"`
Name string `json:"name"`
Type string `json:"type"`
ICalUrlPrimary *string `json:"ical_url_primary"`
ICalUrlOverrides *string `json:"ical_url_overrides"`
EnableWebOverrides bool `json:"enable_web_overrides"`
TimeZone string `json:"time_zone,omitempty"`
Slack *SlackSchedule `json:"slack,omitempty"`
Shifts *[]string `json:"shifts"`
}
// CreateSchedule creates a schedule with given name, type and other parameters depending on type/
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/#create-a-schedule
func (service *ScheduleService) CreateSchedule(opt *CreateScheduleOptions) (*Schedule, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)
req, err := service.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
schedule := new(Schedule)
resp, err := service.client.Do(req, schedule)
if err != nil {
return nil, resp, err
}
return schedule, resp, err
}
type UpdateScheduleOptions struct {
Name string `json:"name,omitempty"`
TeamId string `json:"team_id"`
ICalUrlPrimary *string `json:"ical_url_primary"`
ICalUrlOverrides *string `json:"ical_url_overrides"`
TimeZone string `json:"time_zone,omitempty"`
EnableWebOverrides bool `json:"enable_web_overrides"`
Slack *SlackSchedule `json:"slack,omitempty"`
Shifts *[]string `json:"shifts"`
}
// UpdateSchedule updates a schedule.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/#update-a-schedule
func (service *ScheduleService) UpdateSchedule(id string, opt *UpdateScheduleOptions) (*Schedule, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}
schedule := new(Schedule)
resp, err := service.client.Do(req, schedule)
if err != nil {
return nil, resp, err
}
return schedule, resp, err
}
type DeleteScheduleOptions struct {
}
// DeleteSchedule deletes a schedule.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/#delete-a-schedule
func (service *ScheduleService) DeleteSchedule(id string, opt *DeleteScheduleOptions) (*http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("DELETE", u, opt)
if err != nil {
return nil, err
}
resp, err := service.client.Do(req, nil)
return resp, err
}