-
Notifications
You must be signed in to change notification settings - Fork 156
/
integrations.go
390 lines (328 loc) · 15.8 KB
/
integrations.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
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2018 by authors and contributors.
*/
package datadog
import "net/url"
/*
PagerDuty Integration
*/
type servicePD struct {
ServiceName *string `json:"service_name"`
ServiceKey *string `json:"service_key"`
}
type integrationPD struct {
Services []servicePD `json:"services"`
Subdomain *string `json:"subdomain"`
Schedules []string `json:"schedules"`
APIToken *string `json:"api_token"`
}
// ServicePDRequest defines the Services struct that is part of the IntegrationPDRequest.
type ServicePDRequest struct {
ServiceName *string `json:"service_name"`
ServiceKey *string `json:"service_key"`
}
// IntegrationPDRequest defines the request payload for
// creating & updating Datadog-PagerDuty integration.
type IntegrationPDRequest struct {
Services []ServicePDRequest `json:"services,omitempty"`
Subdomain *string `json:"subdomain,omitempty"`
Schedules []string `json:"schedules,omitempty"`
APIToken *string `json:"api_token,omitempty"`
RunCheck *bool `json:"run_check,omitempty"`
}
// CreateIntegrationPD creates new PagerDuty Integrations.
// Use this if you want to setup the integration for the first time
// or to add more services/schedules.
func (client *Client) CreateIntegrationPD(pdIntegration *IntegrationPDRequest) error {
return client.doJsonRequest("PUT", "/v1/integration/pagerduty", pdIntegration, nil)
}
// UpdateIntegrationPD updates the PagerDuty Integration.
// This will replace the existing values with the new values.
func (client *Client) UpdateIntegrationPD(pdIntegration *IntegrationPDRequest) error {
return client.doJsonRequest("PUT", "/v1/integration/pagerduty", pdIntegration, nil)
}
// GetIntegrationPD gets all the PagerDuty Integrations from the system.
func (client *Client) GetIntegrationPD() (*integrationPD, error) {
var out integrationPD
if err := client.doJsonRequest("GET", "/v1/integration/pagerduty", nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteIntegrationPD removes the PagerDuty Integration from the system.
func (client *Client) DeleteIntegrationPD() error {
return client.doJsonRequest("DELETE", "/v1/integration/pagerduty", nil, nil)
}
// CreateIntegrationPDService creates a single service object in the PagerDuty integration
// Note that creating a service object requires the integration to be activated
func (client *Client) CreateIntegrationPDService(serviceObject *ServicePDRequest) error {
return client.doJsonRequest("POST", "/v1/integration/pagerduty/configuration/services", serviceObject, nil)
}
// UpdateIntegrationPDService updates a single service object in the PagerDuty integration
func (client *Client) UpdateIntegrationPDService(serviceObject *ServicePDRequest) error {
// we can only post the ServiceKey, not ServiceName
toPost := struct {
ServiceKey *string `json:"service_key,omitempty"`
}{
serviceObject.ServiceKey,
}
uri := "/v1/integration/pagerduty/configuration/services/" + *serviceObject.ServiceName
return client.doJsonRequest("PUT", uri, toPost, nil)
}
// GetIntegrationPDService gets a single service object in the PagerDuty integration
// NOTE: the service key is never returned by the API, so it won't be set
func (client *Client) GetIntegrationPDService(serviceName string) (*ServicePDRequest, error) {
uri := "/v1/integration/pagerduty/configuration/services/" + serviceName
var out ServicePDRequest
if err := client.doJsonRequest("GET", uri, nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteIntegrationPDService deletes a single service object in the PagerDuty integration
func (client *Client) DeleteIntegrationPDService(serviceName string) error {
uri := "/v1/integration/pagerduty/configuration/services/" + serviceName
return client.doJsonRequest("DELETE", uri, nil, nil)
}
/*
Slack Integration
*/
// ServiceHookSlackRequest defines the ServiceHooks struct that is part of the IntegrationSlackRequest.
type ServiceHookSlackRequest struct {
Account *string `json:"account"`
Url *string `json:"url"`
}
// ChannelSlackRequest defines the Channels struct that is part of the IntegrationSlackRequest.
type ChannelSlackRequest struct {
ChannelName *string `json:"channel_name"`
TransferAllUserComments *bool `json:"transfer_all_user_comments,omitempty,string"`
Account *string `json:"account"`
}
// IntegrationSlackRequest defines the request payload for
// creating & updating Datadog-Slack integration.
type IntegrationSlackRequest struct {
ServiceHooks []ServiceHookSlackRequest `json:"service_hooks,omitempty"`
Channels []ChannelSlackRequest `json:"channels,omitempty"`
RunCheck *bool `json:"run_check,omitempty,string"`
}
// CreateIntegrationSlack creates new Slack Integrations.
// Use this if you want to setup the integration for the first time
// or to add more channels.
func (client *Client) CreateIntegrationSlack(slackIntegration *IntegrationSlackRequest) error {
return client.doJsonRequest("POST", "/v1/integration/slack", slackIntegration, nil)
}
// UpdateIntegrationSlack updates the Slack Integration.
// This will replace the existing values with the new values.
func (client *Client) UpdateIntegrationSlack(slackIntegration *IntegrationSlackRequest) error {
return client.doJsonRequest("PUT", "/v1/integration/slack", slackIntegration, nil)
}
// GetIntegrationSlack gets all the Slack Integrations from the system.
func (client *Client) GetIntegrationSlack() (*IntegrationSlackRequest, error) {
var out IntegrationSlackRequest
if err := client.doJsonRequest("GET", "/v1/integration/slack", nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteIntegrationSlack removes the Slack Integration from the system.
func (client *Client) DeleteIntegrationSlack() error {
return client.doJsonRequest("DELETE", "/v1/integration/slack", nil, nil)
}
/*
Webhook Integration
*/
// IntegrationWebhookRequest defines the structure of the a webhook request
type IntegrationWebhookRequest struct {
Webhooks []Webhook `json:"hooks"`
}
// Webhook defines the structure of the a webhook
type Webhook struct {
Name *string `json:"name"`
URL *string `json:"url"`
UseCustomPayload *string `json:"use_custom_payload,omitempty"`
CustomPayload *string `json:"custom_payload,omitempty"`
EncodeAsForm *string `json:"encode_as_form,omitempty"`
Headers *string `json:"headers,omitempty"`
}
// CreateIntegrationWebhook creates new webhook integration object(s).
func (client *Client) CreateIntegrationWebhook(webhookIntegration *IntegrationWebhookRequest) error {
return client.doJsonRequest("POST", "/v1/integration/webhooks", webhookIntegration, nil)
}
// UpdateIntegrationWebhook updates the Webhook Integration.
// It replaces the existing configuration with the configuration sent in this
// request.
func (client *Client) UpdateIntegrationWebhook(webhookIntegration *IntegrationWebhookRequest) error {
return client.doJsonRequest("PUT", "/v1/integration/webhooks", webhookIntegration, nil)
}
// GetIntegrationWebhook gets all the Webhook Integrations from Datadog.
func (client *Client) GetIntegrationWebhook() (*IntegrationWebhookRequest, error) {
var out IntegrationWebhookRequest
if err := client.doJsonRequest("GET", "/v1/integration/webhooks", nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// DeleteIntegrationWebhook removes the Webhook Integration from Datadog.
func (client *Client) DeleteIntegrationWebhook() error {
return client.doJsonRequest("DELETE", "/v1/integration/webhooks", nil, nil)
}
/*
AWS Integration
*/
// IntegrationAWSAccount defines the request payload for
// creating & updating Datadog-AWS integration.
type IntegrationAWSAccount struct {
AccountID *string `json:"account_id"`
RoleName *string `json:"role_name"`
FilterTags []string `json:"filter_tags"`
HostTags []string `json:"host_tags"`
AccountSpecificNamespaceRules map[string]bool `json:"account_specific_namespace_rules"`
}
// IntegrationAWSAccountCreateResponse defines the response payload for
// creating & updating Datadog-AWS integration.
type IntegrationAWSAccountCreateResponse struct {
ExternalID string `json:"external_id"`
}
type IntegrationAWSAccountGetResponse struct {
Accounts []IntegrationAWSAccount `json:"accounts"`
}
type IntegrationAWSAccountDeleteRequest struct {
AccountID *string `json:"account_id"`
RoleName *string `json:"role_name"`
}
type IntegrationAWSLambdaARNRequest struct {
AccountID *string `json:"account_id"`
LambdaARN *string `json:"lambda_arn"`
}
// IntegrationAWSLambdaARN is only defined to properly parse the AWS logs GET response
type IntegrationAWSLambdaARN struct {
LambdaARN *string `json:"arn"`
}
type IntegrationAWSServicesLogCollection struct {
AccountID *string `json:"account_id"`
Services []string `json:"services"`
}
// IntegrationAWSLogs is only defined to properly parse the AWS logs GET response
type IntegrationAWSLogCollection struct {
AccountID *string `json:"account_id"`
LambdaARNs []IntegrationAWSLambdaARN `json:"lambdas"`
Services []string `json:"services"`
}
// CreateIntegrationAWS adds a new AWS Account in the AWS Integrations.
// Use this if you want to setup the integration for the first time
// or to add more accounts.
func (client *Client) CreateIntegrationAWS(awsAccount *IntegrationAWSAccount) (*IntegrationAWSAccountCreateResponse, error) {
var out IntegrationAWSAccountCreateResponse
if err := client.doJsonRequest("POST", "/v1/integration/aws", awsAccount, &out); err != nil {
return nil, err
}
return &out, nil
}
// UpdateIntegrationAWS updates an already existing AWS Account in the AWS Integration
func (client *Client) UpdateIntegrationAWS(awsAccount *IntegrationAWSAccount) error {
additionalParameters := url.Values{}
additionalParameters.Set("account_id", *awsAccount.AccountID)
additionalParameters.Add("role_name", *awsAccount.RoleName)
return client.doJsonRequest("PUT", "/v1/integration/aws?"+additionalParameters.Encode(), awsAccount, nil)
}
// GetIntegrationAWS gets all the AWS Accounts in the AWS Integrations from Datadog.
func (client *Client) GetIntegrationAWS() (*[]IntegrationAWSAccount, error) {
var response IntegrationAWSAccountGetResponse
if err := client.doJsonRequest("GET", "/v1/integration/aws", nil, &response); err != nil {
return nil, err
}
return &response.Accounts, nil
}
// DeleteIntegrationAWS removes a specific AWS Account from the AWS Integration.
func (client *Client) DeleteIntegrationAWS(awsAccount *IntegrationAWSAccountDeleteRequest) error {
return client.doJsonRequest("DELETE", "/v1/integration/aws", awsAccount, nil)
}
// AttachLambdaARNIntegrationAWS attach a lambda ARN to an AWS account ID to enable log collection
func (client *Client) AttachLambdaARNIntegrationAWS(lambdaARN *IntegrationAWSLambdaARNRequest) error {
return client.doJsonRequest("POST", "/v1/integration/aws/logs", lambdaARN, nil)
}
// EnableLogCollectionAWSServices enables the log collection for the given AWS services
func (client *Client) EnableLogCollectionAWSServices(services *IntegrationAWSServicesLogCollection) error {
return client.doJsonRequest("POST", "/v1/integration/aws/logs/services", services, nil)
}
// GetIntegrationAWSLogCollection gets all the configuration for the AWS log collection
func (client *Client) GetIntegrationAWSLogCollection() (*[]IntegrationAWSLogCollection, error) {
var response []IntegrationAWSLogCollection
if err := client.doJsonRequest("GET", "/v1/integration/aws/logs", nil, &response); err != nil {
return nil, err
}
return &response, nil
}
// DeleteAWSLogCollection removes the log collection configuration for a given ARN and AWS account
func (client *Client) DeleteAWSLogCollection(lambdaARN *IntegrationAWSLambdaARNRequest) error {
return client.doJsonRequest("DELETE", "/v1/integration/aws/logs", lambdaARN, nil)
}
/*
Google Cloud Platform Integration
*/
// IntegrationGCP defines the response for listing Datadog-Google CloudPlatform integration.
type IntegrationGCP struct {
ProjectID *string `json:"project_id"`
ClientEmail *string `json:"client_email"`
HostFilters *string `json:"host_filters"`
AutoMute *bool `json:"automute,omitempty"`
}
// IntegrationGCPCreateRequest defines the request payload for creating Datadog-Google CloudPlatform integration.
type IntegrationGCPCreateRequest struct {
Type *string `json:"type"` // Should be service_account
ProjectID *string `json:"project_id"`
PrivateKeyID *string `json:"private_key_id"`
PrivateKey *string `json:"private_key"`
ClientEmail *string `json:"client_email"`
ClientID *string `json:"client_id"`
AuthURI *string `json:"auth_uri"` // Should be https://accounts.google.com/o/oauth2/auth
TokenURI *string `json:"token_uri"` // Should be https://accounts.google.com/o/oauth2/token
AuthProviderX509CertURL *string `json:"auth_provider_x509_cert_url"` // Should be https://www.googleapis.com/oauth2/v1/certs
ClientX509CertURL *string `json:"client_x509_cert_url"` // https://www.googleapis.com/robot/v1/metadata/x509/<CLIENT_EMAIL>
HostFilters *string `json:"host_filters,omitempty"`
AutoMute *bool `json:"automute,omitempty"`
}
// IntegrationGCPUpdateRequest defines the request payload for updating Datadog-Google CloudPlatform integration.
type IntegrationGCPUpdateRequest struct {
Type *string `json:"type,omitempty"` // Should be service_account
ProjectID *string `json:"project_id"`
PrivateKeyID *string `json:"private_key_id,omitempty"`
PrivateKey *string `json:"private_key,omitempty"`
ClientEmail *string `json:"client_email"`
ClientID *string `json:"client_id,omitempty"`
AuthURI *string `json:"auth_uri,omitempty"` // Should be https://accounts.google.com/o/oauth2/auth
TokenURI *string `json:"token_uri,omitempty"` // Should be https://accounts.google.com/o/oauth2/token
AuthProviderX509CertURL *string `json:"auth_provider_x509_cert_url,omitempty"` // Should be https://www.googleapis.com/oauth2/v1/certs
ClientX509CertURL *string `json:"client_x509_cert_url,omitempty"` // https://www.googleapis.com/robot/v1/metadata/x509/<CLIENT_EMAIL>
HostFilters *string `json:"host_filters,omitempty"`
AutoMute *bool `json:"automute,omitempty"`
}
// IntegrationGCPDeleteRequest defines the request payload for deleting Datadog-Google CloudPlatform integration.
type IntegrationGCPDeleteRequest struct {
ProjectID *string `json:"project_id"`
ClientEmail *string `json:"client_email"`
}
// ListIntegrationGCP gets all Google Cloud Platform Integrations.
func (client *Client) ListIntegrationGCP() ([]*IntegrationGCP, error) {
var list []*IntegrationGCP
if err := client.doJsonRequest("GET", "/v1/integration/gcp", nil, &list); err != nil {
return nil, err
}
return list, nil
}
// CreateIntegrationGCP creates a new Google Cloud Platform Integration.
func (client *Client) CreateIntegrationGCP(cir *IntegrationGCPCreateRequest) error {
return client.doJsonRequest("POST", "/v1/integration/gcp", cir, nil)
}
// UpdateIntegrationGCP updates a Google Cloud Platform Integration project.
func (client *Client) UpdateIntegrationGCP(cir *IntegrationGCPUpdateRequest) error {
return client.doJsonRequest("PUT", "/v1/integration/gcp", cir, nil)
}
// DeleteIntegrationGCP deletes a Google Cloud Platform Integration.
func (client *Client) DeleteIntegrationGCP(cir *IntegrationGCPDeleteRequest) error {
return client.doJsonRequest("DELETE", "/v1/integration/gcp", cir, nil)
}