-
Notifications
You must be signed in to change notification settings - Fork 1
/
connector.go
445 lines (385 loc) · 12.7 KB
/
connector.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Connector definition
type Connector struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
CreatedAt string `json:"createdAt,omitempty"` // date time string in ISO 8601
UpdatedAt string `json:"updatedAt,omitempty"` // date time string in ISO 8601
Params interface{} `json:"params"`
}
// ConnectorOutput definition
type ConnectorOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
CreatedAt string `json:"createdAt"` // date time string in ISO 8601
UpdatedAt string `json:"updatedAt"` // date time string in ISO 8601
Params ConnectorOutputParams `json:"params"`
}
// ConnectorOutputParams definition
type ConnectorOutputParams struct {
APIKey string `json:"apiKey,omitempty"` // Datadog or Zendesk or Github or Serverless or Autotask api key
Authorization string `json:"authorization,omitempty"` // Serverless
URL string `json:"url,omitempty"` // Jira or Microsoft Teams or Zendesk or Discord or Autotask server url
Email string `json:"email,omitempty"` // Jira or ServiceNow or Zendesk username or email
Username string `json:"username,omitempty"` // TOPdesk or ServiceNow or Autotask username
Password string `json:"password,omitempty"` // Jira or ServiceNow or Autotask user password or api token
Secret string `json:"secret,omitempty"` // DingTalk
}
// ConnectorParamsDatadog definition
type ConnectorParamsDatadog struct {
APIKey string `json:"apiKey"`
}
// ConnectorParamsJira definition
type ConnectorParamsJira struct {
URL string `json:"url"`
Email string `json:"email"`
Password string `json:"password"`
}
// ConnectorParamsMicrosoftTeams definition
type ConnectorParamsMicrosoftTeams struct {
URL string `json:"url"`
}
// ConnectorParamsServiceNow definition
type ConnectorParamsServiceNow struct {
URL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
}
// ConnectorParamsSlack definition
type ConnectorParamsSlack struct{}
// ConnectorParamsZendesk definition
type ConnectorParamsZendesk struct {
URL string `json:"url"`
Email string `json:"email"`
APIKey string `json:"apiKey"`
}
// ConnectorParamsDiscord definition
type ConnectorParamsDiscord struct {
URL string `json:"url"`
}
// ConnectorParamsGithub definition
type ConnectorParamsGithub struct {
APIKey string `json:"apiKey"`
}
// ConnectorParamsTopdesk definition
type ConnectorParamsTopdesk struct {
URL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
}
// ConnectorParamsAWSLambda definition
type ConnectorParamsAWSLambda struct {
Authorization string `json:"authorization,omitempty"`
}
// ConnectorParamsAzureFunction definition
type ConnectorParamsAzureFunction struct {
Authorization string `json:"authorization,omitempty"`
}
// ConnectorParamsGoogleFunction definition
type ConnectorParamsGoogleFunction struct {
Authorization string `json:"authorization,omitempty"`
}
// ConnectorParamsSysdig definition
type ConnectorParamsSysdig struct {
APIKey string `json:"apiKey"`
}
// ConnectorParamsAutotask definition
type ConnectorParamsAutotask struct {
URL string `json:"url"`
Email string `json:"email"`
Password string `json:"password"`
}
// ConnectorParamsMattermost definition
type ConnectorParamsMattermost struct {
URL string `json:"url"`
}
// ConnectorParamsZammad definition
type ConnectorParamsZammad struct {
URL string `json:"url"`
APIKey string `json:"apiKey"`
}
// ConnectorParamsStatusPageIO definition
type ConnectorParamsStatusPageIO struct {
APIKey string `json:"apiKey"`
}
// ConnectorParamsDingTalk definition
type ConnectorParamsDingTalk struct {
URL string `json:"url,omitempty"`
Secret string `json:"secret,omitempty"`
}
// ConnectorTypes defines connector types
var ConnectorTypes = struct {
AutomationRule string
Autotask string
DingTalk string
DingTalkAction string
Discord string
Email string
Github string
Jira string
Mattermost string
MicrosoftTeams string
MicrosoftTeamsBot string
MicrosoftTeamsWebhook string
ServiceNow string
Slack string
SlackWebhook string
Telegram string
Topdesk string
Webhook string
Zammad string
Zendesk string
}{
AutomationRule: "automation_rule",
Autotask: "autotask",
DingTalk: "dingtalk",
DingTalkAction: "dingtalk_action",
Discord: "discord",
Email: "email",
Github: "github",
Jira: "jira",
Mattermost: "mattermost",
MicrosoftTeams: "microsoft_teams",
MicrosoftTeamsBot: "microsoft_teams_bot",
MicrosoftTeamsWebhook: "microsoft_teams_webhook",
ServiceNow: "servicenow",
Slack: "slack",
SlackWebhook: "slack_webhook",
Telegram: "telegram",
Topdesk: "topdesk",
Webhook: "webhook",
Zammad: "zammad",
Zendesk: "zendesk",
}
// ConnectorTypesAll defines connector all types list
var ConnectorTypesAll = []string{
ConnectorTypes.AutomationRule,
ConnectorTypes.Autotask,
ConnectorTypes.DingTalk,
ConnectorTypes.DingTalkAction,
ConnectorTypes.Discord,
ConnectorTypes.Email,
ConnectorTypes.Github,
ConnectorTypes.Jira,
ConnectorTypes.Mattermost,
ConnectorTypes.MicrosoftTeams,
ConnectorTypes.MicrosoftTeamsBot,
ConnectorTypes.MicrosoftTeamsWebhook,
ConnectorTypes.ServiceNow,
ConnectorTypes.Slack,
ConnectorTypes.SlackWebhook,
ConnectorTypes.Telegram,
ConnectorTypes.Topdesk,
ConnectorTypes.Webhook,
ConnectorTypes.Zammad,
ConnectorTypes.Zendesk,
}
// CreateConnectorInput represents the input of a CreateConnector operation.
type CreateConnectorInput struct {
_ struct{}
Connector *Connector
}
// CreateConnectorOutput represents the output of a CreateConnector operation.
type CreateConnectorOutput struct {
_ struct{}
Connector *ConnectorOutput
}
// CreateConnector creates a new connector. https://api.ilert.com/api-docs/#tag/Connectors/paths/~1connectors/post
func (c *Client) CreateConnector(input *CreateConnectorInput) (*CreateConnectorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Connector == nil {
return nil, errors.New("connector input is required")
}
resp, err := c.httpClient.R().SetBody(input.Connector).Post(apiRoutes.connectors)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
connector := &ConnectorOutput{}
err = json.Unmarshal(resp.Body(), connector)
if err != nil {
return nil, err
}
return &CreateConnectorOutput{Connector: connector}, nil
}
// GetConnectorInput represents the input of a GetConnector operation.
type GetConnectorInput struct {
_ struct{}
ConnectorID *string
}
// GetConnectorOutput represents the output of a GetConnector operation.
type GetConnectorOutput struct {
_ struct{}
Connector *ConnectorOutput
}
// GetConnector gets the connector with specified id. https://api.ilert.com/api-docs/#tag/Connectors/paths/~1connectors~1{id}/get
func (c *Client) GetConnector(input *GetConnectorInput) (*GetConnectorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ConnectorID == nil {
return nil, errors.New("connector id is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%s", apiRoutes.connectors, *input.ConnectorID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
connector := &ConnectorOutput{}
err = json.Unmarshal(resp.Body(), connector)
if err != nil {
return nil, err
}
return &GetConnectorOutput{Connector: connector}, nil
}
// GetConnectorsInput represents the input of a GetConnectors operation.
type GetConnectorsInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
// Default: 0
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Default: 50, Maximum: 100
MaxResults *int
}
// GetConnectorsOutput represents the output of a GetConnectors operation.
type GetConnectorsOutput struct {
_ struct{}
Connectors []*ConnectorOutput
}
// GetConnectors lists existing connectors. https://api.ilert.com/api-docs/#tag/Connectors/paths/~1connectors/get
func (c *Client) GetConnectors(input *GetConnectorsInput) (*GetConnectorsOutput, error) {
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
} else {
q.Add("start-index", "0")
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
} else {
q.Add("max-results", "50")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.connectors, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
connectors := make([]*ConnectorOutput, 0)
err = json.Unmarshal(resp.Body(), &connectors)
if err != nil {
return nil, err
}
return &GetConnectorsOutput{Connectors: connectors}, nil
}
// SearchConnectorInput represents the input of a SearchConnector operation.
type SearchConnectorInput struct {
_ struct{}
ConnectorName *string
}
// SearchConnectorOutput represents the output of a SearchConnector operation.
type SearchConnectorOutput struct {
_ struct{}
Connector *ConnectorOutput
}
// SearchConnector gets the connector with specified name.
func (c *Client) SearchConnector(input *SearchConnectorInput) (*SearchConnectorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ConnectorName == nil {
return nil, errors.New("connector name is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/name/%s", apiRoutes.connectors, *input.ConnectorName))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
connector := &ConnectorOutput{}
err = json.Unmarshal(resp.Body(), connector)
if err != nil {
return nil, err
}
return &SearchConnectorOutput{Connector: connector}, nil
}
// UpdateConnectorInput represents the input of a UpdateConnector operation.
type UpdateConnectorInput struct {
_ struct{}
ConnectorID *string
Connector *Connector
}
// UpdateConnectorOutput represents the output of a UpdateConnector operation.
type UpdateConnectorOutput struct {
_ struct{}
Connector *ConnectorOutput
}
// UpdateConnector updates an existing connector. https://api.ilert.com/api-docs/#tag/Connectors/paths/~1connectors~1{id}/put
func (c *Client) UpdateConnector(input *UpdateConnectorInput) (*UpdateConnectorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Connector == nil {
return nil, errors.New("connector input is required")
}
if input.ConnectorID == nil {
return nil, errors.New("connector id is required")
}
resp, err := c.httpClient.R().SetBody(input.Connector).Put(fmt.Sprintf("%s/%s", apiRoutes.connectors, *input.ConnectorID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
connector := &ConnectorOutput{}
err = json.Unmarshal(resp.Body(), connector)
if err != nil {
return nil, err
}
return &UpdateConnectorOutput{Connector: connector}, nil
}
// DeleteConnectorInput represents the input of a DeleteConnector operation.
type DeleteConnectorInput struct {
_ struct{}
ConnectorID *string
}
// DeleteConnectorOutput represents the output of a DeleteConnector operation.
type DeleteConnectorOutput struct {
_ struct{}
}
// DeleteConnector deletes the specified connector. https://api.ilert.com/api-docs/#tag/Connectors/paths/~1connectors~1{id}/delete
func (c *Client) DeleteConnector(input *DeleteConnectorInput) (*DeleteConnectorOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ConnectorID == nil {
return nil, errors.New("connector id is required")
}
resp, err := c.httpClient.R().Delete(fmt.Sprintf("%s/%s", apiRoutes.connectors, *input.ConnectorID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteConnectorOutput{}, nil
}