Skip to content

Commit 1ece346

Browse files
author
David Bouchare
authored
Webhooks integration - contributor (#300)
1 parent 261ab16 commit 1ece346

File tree

3 files changed

+356
-0
lines changed

3 files changed

+356
-0
lines changed

datadog-accessors.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24907,6 +24907,192 @@ func (u *UserAgentParser) SetTarget(v string) {
2490724907
u.Target = &v
2490824908
}
2490924909

24910+
// GetCustomPayload returns the CustomPayload field if non-nil, zero value otherwise.
24911+
func (w *Webhook) GetCustomPayload() string {
24912+
if w == nil || w.CustomPayload == nil {
24913+
return ""
24914+
}
24915+
return *w.CustomPayload
24916+
}
24917+
24918+
// GetCustomPayloadOk returns a tuple with the CustomPayload field if it's non-nil, zero value otherwise
24919+
// and a boolean to check if the value has been set.
24920+
func (w *Webhook) GetCustomPayloadOk() (string, bool) {
24921+
if w == nil || w.CustomPayload == nil {
24922+
return "", false
24923+
}
24924+
return *w.CustomPayload, true
24925+
}
24926+
24927+
// HasCustomPayload returns a boolean if a field has been set.
24928+
func (w *Webhook) HasCustomPayload() bool {
24929+
if w != nil && w.CustomPayload != nil {
24930+
return true
24931+
}
24932+
24933+
return false
24934+
}
24935+
24936+
// SetCustomPayload allocates a new w.CustomPayload and returns the pointer to it.
24937+
func (w *Webhook) SetCustomPayload(v string) {
24938+
w.CustomPayload = &v
24939+
}
24940+
24941+
// GetEncodeAsForm returns the EncodeAsForm field if non-nil, zero value otherwise.
24942+
func (w *Webhook) GetEncodeAsForm() string {
24943+
if w == nil || w.EncodeAsForm == nil {
24944+
return ""
24945+
}
24946+
return *w.EncodeAsForm
24947+
}
24948+
24949+
// GetEncodeAsFormOk returns a tuple with the EncodeAsForm field if it's non-nil, zero value otherwise
24950+
// and a boolean to check if the value has been set.
24951+
func (w *Webhook) GetEncodeAsFormOk() (string, bool) {
24952+
if w == nil || w.EncodeAsForm == nil {
24953+
return "", false
24954+
}
24955+
return *w.EncodeAsForm, true
24956+
}
24957+
24958+
// HasEncodeAsForm returns a boolean if a field has been set.
24959+
func (w *Webhook) HasEncodeAsForm() bool {
24960+
if w != nil && w.EncodeAsForm != nil {
24961+
return true
24962+
}
24963+
24964+
return false
24965+
}
24966+
24967+
// SetEncodeAsForm allocates a new w.EncodeAsForm and returns the pointer to it.
24968+
func (w *Webhook) SetEncodeAsForm(v string) {
24969+
w.EncodeAsForm = &v
24970+
}
24971+
24972+
// GetHeaders returns the Headers field if non-nil, zero value otherwise.
24973+
func (w *Webhook) GetHeaders() string {
24974+
if w == nil || w.Headers == nil {
24975+
return ""
24976+
}
24977+
return *w.Headers
24978+
}
24979+
24980+
// GetHeadersOk returns a tuple with the Headers field if it's non-nil, zero value otherwise
24981+
// and a boolean to check if the value has been set.
24982+
func (w *Webhook) GetHeadersOk() (string, bool) {
24983+
if w == nil || w.Headers == nil {
24984+
return "", false
24985+
}
24986+
return *w.Headers, true
24987+
}
24988+
24989+
// HasHeaders returns a boolean if a field has been set.
24990+
func (w *Webhook) HasHeaders() bool {
24991+
if w != nil && w.Headers != nil {
24992+
return true
24993+
}
24994+
24995+
return false
24996+
}
24997+
24998+
// SetHeaders allocates a new w.Headers and returns the pointer to it.
24999+
func (w *Webhook) SetHeaders(v string) {
25000+
w.Headers = &v
25001+
}
25002+
25003+
// GetName returns the Name field if non-nil, zero value otherwise.
25004+
func (w *Webhook) GetName() string {
25005+
if w == nil || w.Name == nil {
25006+
return ""
25007+
}
25008+
return *w.Name
25009+
}
25010+
25011+
// GetNameOk returns a tuple with the Name field if it's non-nil, zero value otherwise
25012+
// and a boolean to check if the value has been set.
25013+
func (w *Webhook) GetNameOk() (string, bool) {
25014+
if w == nil || w.Name == nil {
25015+
return "", false
25016+
}
25017+
return *w.Name, true
25018+
}
25019+
25020+
// HasName returns a boolean if a field has been set.
25021+
func (w *Webhook) HasName() bool {
25022+
if w != nil && w.Name != nil {
25023+
return true
25024+
}
25025+
25026+
return false
25027+
}
25028+
25029+
// SetName allocates a new w.Name and returns the pointer to it.
25030+
func (w *Webhook) SetName(v string) {
25031+
w.Name = &v
25032+
}
25033+
25034+
// GetURL returns the URL field if non-nil, zero value otherwise.
25035+
func (w *Webhook) GetURL() string {
25036+
if w == nil || w.URL == nil {
25037+
return ""
25038+
}
25039+
return *w.URL
25040+
}
25041+
25042+
// GetURLOk returns a tuple with the URL field if it's non-nil, zero value otherwise
25043+
// and a boolean to check if the value has been set.
25044+
func (w *Webhook) GetURLOk() (string, bool) {
25045+
if w == nil || w.URL == nil {
25046+
return "", false
25047+
}
25048+
return *w.URL, true
25049+
}
25050+
25051+
// HasURL returns a boolean if a field has been set.
25052+
func (w *Webhook) HasURL() bool {
25053+
if w != nil && w.URL != nil {
25054+
return true
25055+
}
25056+
25057+
return false
25058+
}
25059+
25060+
// SetURL allocates a new w.URL and returns the pointer to it.
25061+
func (w *Webhook) SetURL(v string) {
25062+
w.URL = &v
25063+
}
25064+
25065+
// GetUseCustomPayload returns the UseCustomPayload field if non-nil, zero value otherwise.
25066+
func (w *Webhook) GetUseCustomPayload() string {
25067+
if w == nil || w.UseCustomPayload == nil {
25068+
return ""
25069+
}
25070+
return *w.UseCustomPayload
25071+
}
25072+
25073+
// GetUseCustomPayloadOk returns a tuple with the UseCustomPayload field if it's non-nil, zero value otherwise
25074+
// and a boolean to check if the value has been set.
25075+
func (w *Webhook) GetUseCustomPayloadOk() (string, bool) {
25076+
if w == nil || w.UseCustomPayload == nil {
25077+
return "", false
25078+
}
25079+
return *w.UseCustomPayload, true
25080+
}
25081+
25082+
// HasUseCustomPayload returns a boolean if a field has been set.
25083+
func (w *Webhook) HasUseCustomPayload() bool {
25084+
if w != nil && w.UseCustomPayload != nil {
25085+
return true
25086+
}
25087+
25088+
return false
25089+
}
25090+
25091+
// SetUseCustomPayload allocates a new w.UseCustomPayload and returns the pointer to it.
25092+
func (w *Webhook) SetUseCustomPayload(v string) {
25093+
w.UseCustomPayload = &v
25094+
}
25095+
2491025096
// GetAlertID returns the AlertID field if non-nil, zero value otherwise.
2491125097
func (w *Widget) GetAlertID() int {
2491225098
if w == nil || w.AlertID == nil {

integration/integrations_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,130 @@ func cleanUpIntegrationSlack(t *testing.T) {
293293
}
294294
}
295295

296+
/*
297+
Webhook Integration
298+
*/
299+
300+
func TestIntegrationWebhookCreateAndDelete(t *testing.T) {
301+
expected := createTestIntegrationWebhook(t)
302+
defer cleanUpIntegrationWebhook(t)
303+
304+
actual, err := client.GetIntegrationWebhook()
305+
if err != nil {
306+
t.Fatalf("Retrieving a Webhook integration failed when it shouldn't: (%s)", err)
307+
}
308+
309+
expectedWebhooks := make([]*string, len(expected.Webhooks))
310+
for _, wh := range expected.Webhooks {
311+
expectedWebhooks = append(expectedWebhooks, wh.Name)
312+
}
313+
314+
actualWebhooks := make([]*string, len(actual.Webhooks))
315+
for _, wh := range actual.Webhooks {
316+
actualWebhooks = append(actualWebhooks, wh.Name)
317+
}
318+
319+
assert.Equal(t, expectedWebhooks, actualWebhooks)
320+
}
321+
322+
func TestIntegrationWebhookUpdate(t *testing.T) {
323+
webhookIntegration := createTestIntegrationWebhook(t)
324+
defer cleanUpIntegrationWebhook(t)
325+
326+
webhookIntegration.Webhooks = append(webhookIntegration.Webhooks, datadog.Webhook{
327+
Name: datadog.String("Test_Webhook2"),
328+
URL: datadog.String("https://test.url.com/webhook"),
329+
UseCustomPayload: datadog.String("true"),
330+
CustomPayload: datadog.String("custom_payload"),
331+
EncodeAsForm: datadog.String("true"),
332+
Headers: datadog.String("{'Content-Type': 'application/text', 'Authorization': 'token'}"),
333+
})
334+
335+
if err := client.UpdateIntegrationWebhook(webhookIntegration); err != nil {
336+
t.Fatalf("Updating a Webhook integration failed when it shouldn't: %s", err)
337+
}
338+
339+
actual, err := client.GetIntegrationWebhook()
340+
if err != nil {
341+
t.Fatalf("Retrieving a Webhook integration failed when it shouldn't: %s", err)
342+
}
343+
344+
expectedWebhooks := make([]*string, len(webhookIntegration.Webhooks))
345+
for _, wh := range webhookIntegration.Webhooks {
346+
expectedWebhooks = append(expectedWebhooks, wh.Name)
347+
}
348+
349+
actualWebhooks := make([]*string, len(actual.Webhooks))
350+
for _, wh := range actual.Webhooks {
351+
actualWebhooks = append(actualWebhooks, wh.Name)
352+
}
353+
354+
assert.Equal(t, expectedWebhooks, actualWebhooks)
355+
}
356+
357+
func TestIntegrationWebhookGet(t *testing.T) {
358+
webhookIntegration := createTestIntegrationWebhook(t)
359+
defer cleanUpIntegrationWebhook(t)
360+
361+
actual, err := client.GetIntegrationWebhook()
362+
if err != nil {
363+
t.Fatalf("Retrieving Webhook integration failed when it shouldn't: %s", err)
364+
}
365+
366+
expectedWebhooks := make([]*string, len(webhookIntegration.Webhooks))
367+
for _, wh := range webhookIntegration.Webhooks {
368+
expectedWebhooks = append(expectedWebhooks, wh.Name)
369+
}
370+
371+
actualWebhooks := make([]*string, len(actual.Webhooks))
372+
for _, wh := range actual.Webhooks {
373+
actualWebhooks = append(actualWebhooks, wh.Name)
374+
}
375+
376+
assert.Equal(t, expectedWebhooks, actualWebhooks)
377+
}
378+
379+
func getTestIntegrationWebhook() *datadog.IntegrationWebhookRequest {
380+
return &datadog.IntegrationWebhookRequest{
381+
Webhooks: []datadog.Webhook{
382+
{
383+
Name: datadog.String("Test_Webhook1"),
384+
URL: datadog.String("https://test.url.com/webhook"),
385+
UseCustomPayload: datadog.String("true"),
386+
CustomPayload: datadog.String("custom_payload"),
387+
EncodeAsForm: datadog.String("true"),
388+
Headers: datadog.String("{'Content-Type': 'application/text', 'Authorization': 'token'}"),
389+
},
390+
},
391+
}
392+
}
393+
394+
func createTestIntegrationWebhook(t *testing.T) *datadog.IntegrationWebhookRequest {
395+
webhookIntegration := getTestIntegrationWebhook()
396+
397+
err := client.CreateIntegrationWebhook(webhookIntegration)
398+
if err != nil {
399+
t.Fatalf("Creating a Webhook integration failed when it shouldn't: %s", err)
400+
}
401+
402+
return webhookIntegration
403+
}
404+
405+
func cleanUpIntegrationWebhook(t *testing.T) {
406+
if err := client.DeleteIntegrationWebhook(); err != nil {
407+
t.Fatalf("Deleting the Webhook integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
408+
}
409+
410+
webhookIntegration, err := client.GetIntegrationWebhook()
411+
if webhookIntegration != nil {
412+
t.Fatal("Webhook Integration hasn't been deleted when it should have been. Manual cleanup needed.")
413+
}
414+
415+
if err == nil {
416+
t.Fatal("Fetching deleted Webhook integration didn't lead to an error.")
417+
}
418+
}
419+
296420
/*
297421
AWS Integration
298422
*/

integrations.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,52 @@ func (client *Client) DeleteIntegrationSlack() error {
158158
return client.doJsonRequest("DELETE", "/v1/integration/slack", nil, nil)
159159
}
160160

161+
/*
162+
Webhook Integration
163+
*/
164+
165+
// IntegrationWebhookRequest defines the structure of the a webhook request
166+
type IntegrationWebhookRequest struct {
167+
Webhooks []Webhook `json:"hooks"`
168+
}
169+
170+
// Webhook defines the structure of the a webhook
171+
type Webhook struct {
172+
Name *string `json:"name"`
173+
URL *string `json:"url"`
174+
UseCustomPayload *string `json:"use_custom_payload,omitempty"`
175+
CustomPayload *string `json:"custom_payload,omitempty"`
176+
EncodeAsForm *string `json:"encode_as_form,omitempty"`
177+
Headers *string `json:"headers,omitempty"`
178+
}
179+
180+
// CreateIntegrationWebhook creates new webhook integration object(s).
181+
func (client *Client) CreateIntegrationWebhook(webhookIntegration *IntegrationWebhookRequest) error {
182+
return client.doJsonRequest("POST", "/v1/integration/webhooks", webhookIntegration, nil)
183+
}
184+
185+
// UpdateIntegrationWebhook updates the Webhook Integration.
186+
// It replaces the existing configuration with the configuration sent in this
187+
// request.
188+
func (client *Client) UpdateIntegrationWebhook(webhookIntegration *IntegrationWebhookRequest) error {
189+
return client.doJsonRequest("PUT", "/v1/integration/webhooks", webhookIntegration, nil)
190+
}
191+
192+
// GetIntegrationWebhook gets all the Webhook Integrations from Datadog.
193+
func (client *Client) GetIntegrationWebhook() (*IntegrationWebhookRequest, error) {
194+
var out IntegrationWebhookRequest
195+
if err := client.doJsonRequest("GET", "/v1/integration/webhooks", nil, &out); err != nil {
196+
return nil, err
197+
}
198+
199+
return &out, nil
200+
}
201+
202+
// DeleteIntegrationWebhook removes the Webhook Integration from Datadog.
203+
func (client *Client) DeleteIntegrationWebhook() error {
204+
return client.doJsonRequest("DELETE", "/v1/integration/webhooks", nil, nil)
205+
}
206+
161207
/*
162208
AWS Integration
163209
*/

0 commit comments

Comments
 (0)