-
Notifications
You must be signed in to change notification settings - Fork 7
/
integration.go
206 lines (169 loc) · 6.21 KB
/
integration.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
package aapi
import (
"fmt"
"net/http"
)
// IntegrationService handles requests to integration endpoint
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/integrations/
type IntegrationService struct {
client *Client
url string
}
// NewIntegrationService creates IntegrationService with corresponding url part
func NewIntegrationService(client *Client) *IntegrationService {
integrationService := IntegrationService{}
integrationService.client = client
integrationService.url = "integrations"
return &integrationService
}
type PaginatedIntegrationsResponse struct {
PaginatedResponse
Integrations []*Integration `json:"results"`
}
type Integration struct {
ID string `json:"id"`
TeamId string `json:"team_id"`
Name string `json:"name"`
Link string `json:"link"`
IncidentsCount int `json:"incidents_count"`
Type string `json:"type"`
DefaultRoute *DefaultRoute `json:"default_route"`
Templates *Templates `json:"templates"`
}
type DefaultRoute struct {
ID string `json:"id"`
EscalationChainId *string `json:"escalation_chain_id"`
SlackRoute *SlackRoute `json:"slack,omitempty"`
TelegramRoute *TelegramRoute `json:"telegram,omitempty"`
MSTeamsRoute *MSTeamsRoute `json:"msteams,omitempty"`
}
type Templates struct {
GroupingKey *string `json:"grouping_key"`
ResolveSignal *string `json:"resolve_signal"`
AcknowledgeSignal *string `json:"acknowledge_signal"`
SourceLink *string `json:"source_link"`
Slack *TitleMessageImageTemplate `json:"slack"`
Web *TitleMessageImageTemplate `json:"web"`
MSTeams *TitleMessageImageTemplate `json:"msteams"`
Telegram *TitleMessageImageTemplate `json:"telegram"`
PhoneCall *TitleTemplate `json:"phone_call"`
SMS *TitleTemplate `json:"sms"`
Email *TitleMessageTemplate `json:"email"`
MobileApp *TitleMessageTemplate `json:"mobile_app"`
}
type TitleMessageImageTemplate struct {
Title *string `json:"title"`
Message *string `json:"message"`
ImageURL *string `json:"image_url"`
}
type TitleMessageTemplate struct {
Title *string `json:"title"`
Message *string `json:"message"`
}
type TitleTemplate struct {
Title *string `json:"title"`
}
type MessageTemplate struct {
Message *string `json:"message"`
}
type ImageURLTemplate struct {
ImageURL *string `json:"image_url"`
}
type ListIntegrationOptions struct {
ListOptions
}
// ListIntegrations fetches all integrations for current organization.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/integrations/#get-integration
func (service *IntegrationService) ListIntegrations(opt *ListIntegrationOptions) (*PaginatedIntegrationsResponse, *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 integrations *PaginatedIntegrationsResponse
resp, err := service.client.Do(req, &integrations)
if err != nil {
return nil, resp, err
}
return integrations, resp, err
}
type GetIntegrationOptions struct {
}
// GetIntegration fetches integration by given id.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/integrations/#get-integration
func (service *IntegrationService) GetIntegration(id string, opt *GetIntegrationOptions) (*Integration, *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
}
integration := new(Integration)
resp, err := service.client.Do(req, integration)
if err != nil {
return nil, resp, err
}
return integration, resp, err
}
type CreateIntegrationOptions struct {
TeamId string `json:"team_id"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Templates *Templates `json:"templates,omitempty"`
DefaultRoute *DefaultRoute `json:"default_route,omitempty"`
}
// CreateIntegration creates integration with type, team_id and optional given name.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/integrations/#get-integration
func (service *IntegrationService) CreateIntegration(opt *CreateIntegrationOptions) (*Integration, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)
req, err := service.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
integration := new(Integration)
resp, err := service.client.Do(req, integration)
if err != nil {
return nil, resp, err
}
return integration, resp, err
}
type UpdateIntegrationOptions struct {
Name string `json:"name,omitempty"`
TeamId string `json:"team_id"`
Templates *Templates `json:"templates,omitempty"`
DefaultRoute *DefaultRoute `json:"default_route,omitempty"`
}
// UpdateIntegration updates integration with new templates, name and default route.
// To update template it is enough to provide at least one field.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/integrations/#update-integration
func (service *IntegrationService) UpdateIntegration(id string, opt *UpdateIntegrationOptions) (*Integration, *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
}
integration := new(Integration)
resp, err := service.client.Do(req, integration)
if err != nil {
return nil, resp, err
}
return integration, resp, err
}
type DeleteIntegrationOptions struct {
}
// DeleteIntegration deletes integration.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/integrations/#delete-integration
func (service *IntegrationService) DeleteIntegration(id string, opt *DeleteIntegrationOptions) (*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
}