forked from grafana/amixr-api-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
188 lines (156 loc) · 5.56 KB
/
route.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
package aapi
import (
"fmt"
"log"
"net/http"
)
// RouteService handles requests to route endpoint
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/routes/
type RouteService struct {
client *Client
url string
}
// NewRouteService creates RouteService with defined url
func NewRouteService(client *Client) *RouteService {
routeService := RouteService{}
routeService.client = client
routeService.url = "routes"
return &routeService
}
type PaginatedRoutesResponse struct {
PaginatedResponse
Routes []*Route `json:"results"`
}
type Route struct {
ID string `json:"id"`
IntegrationId string `json:"integration_id"`
EscalationChainId string `json:"escalation_chain_id"`
Position int `json:"position"`
RoutingRegex string `json:"routing_regex"`
RoutingType string `json:"routing_type"`
IsTheLastRoute bool `json:"is_the_last_route"`
SlackRoute *SlackRoute `json:"slack"`
TelegramRoute *TelegramRoute `json:"telegram"`
MSTeamsRoute *MSTeamsRoute `json:"msteams"`
}
type SlackRoute struct {
ChannelId *string `json:"channel_id"`
Enabled bool `json:"enabled"`
}
type TelegramRoute struct {
Id *string `json:"id"`
Enabled bool `json:"enabled"`
}
type MSTeamsRoute struct {
Id *string `json:"id"`
Enabled bool `json:"enabled"`
}
type ListRouteOptions struct {
ListOptions
IntegrationId string `url:"integration_id,omitempty" json:"integration_id,omitempty"`
RoutingRegex string `url:"routing_regex,omitempty" json:"routing_regex,omitempty"`
RoutingType string `url:"routing_type,omitempty" json:"routing_type,omitempty"`
}
// ListRoutes fetches all routes for authorized organization
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/routes/#list-routes
func (service *RouteService) ListRoutes(opt *ListRouteOptions) (*PaginatedRoutesResponse, *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 routes *PaginatedRoutesResponse
resp, err := service.client.Do(req, &routes)
if err != nil {
return nil, resp, err
}
return routes, resp, err
}
type GetRouteOptions struct {
}
// GetRoute fetches route by given id
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/routes/#get-a-route
func (service *RouteService) GetRoute(id string, opt *GetRouteOptions) (*Route, *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
}
route := new(Route)
resp, err := service.client.Do(req, route)
if err != nil {
return nil, resp, err
}
return route, resp, err
}
type CreateRouteOptions struct {
IntegrationId string `json:"integration_id,omitempty"`
EscalationChainId string `json:"escalation_chain_id,omitempty"`
Position *int `json:"position,omitempty"`
RoutingRegex string `json:"routing_regex,omitempty"`
RoutingType string `json:"routing_type,omitempty"`
Slack *SlackRoute `json:"slack,omitempty"`
Telegram *TelegramRoute `json:"telegram,omitempty"`
MSTeams *MSTeamsRoute `json:"msteams,omitempty"`
ManualOrder bool `url:"manual_order,omitempty" json:"manual_order,omitempty"`
}
// CreateRoute creates route with given name and type
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/routes/#create-a-route
func (service *RouteService) CreateRoute(opt *CreateRouteOptions) (*Route, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)
req, err := service.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
route := new(Route)
resp, err := service.client.Do(req, route)
log.Printf("[DEBUG] request success")
if err != nil {
return nil, resp, err
}
return route, resp, err
}
type UpdateRouteOptions struct {
EscalationChainId string `json:"escalation_chain_id,omitempty"`
Position *int `json:"position,omitempty"`
Slack *SlackRoute `json:"slack,omitempty"`
Telegram *TelegramRoute `json:"telegram,omitempty"`
MSTeams *MSTeamsRoute `json:"msteams,omitempty"`
RoutingRegex string `json:"routing_regex,omitempty"`
RoutingType string `json:"routing_type,omitempty"`
ManualOrder bool `url:"manual_order,omitempty" json:"manual_order,omitempty"`
}
// UpdateRoute updates route with new templates and/or name. At least one field in template is required
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/routes/#update-route
func (service *RouteService) UpdateRoute(id string, opt *UpdateRouteOptions) (*Route, *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
}
route := new(Route)
resp, err := service.client.Do(req, route)
if err != nil {
return nil, resp, err
}
return route, resp, err
}
type DeleteRouteOptions struct {
}
// DeleteRoute deletes route
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/routes/#delete-a-route
func (service *RouteService) DeleteRoute(id string, opt *DeleteRouteOptions) (*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
}