diff --git a/integration/integrations_test.go b/integration/integrations_test.go index 3a33b7c..1ee5644 100644 --- a/integration/integrations_test.go +++ b/integration/integrations_test.go @@ -86,6 +86,49 @@ func TestIntegrationPDGet(t *testing.T) { assert.Equal(t, expectedServiceNames, actualServiceNames) } +func TestIntegrationPDService(t *testing.T) { + // test manipulation of individual service objects in the PD integration + // when the integration is not active, manipulating service objects + // should return 404 + so := datadog.ServicePDRequest{ + ServiceName: datadog.String("testPDServiceNameIndividual"), + ServiceKey: datadog.String("testPDServiceKeyIndividual"), + } + + err := client.CreateIntegrationPDService(&so) + if err == nil { + t.Fatalf("Creating PD integration service object succeeded without active PD integration") + } + + _ = createTestIntegrationPD(t) + defer cleanUpIntegrationPD(t) + + err = client.CreateIntegrationPDService(&so) + if err != nil { + t.Fatalf("Creating PD integration service object failed when it shouldn't: %s", err) + } + + var soRead *datadog.ServicePDRequest + soRead, err = client.GetIntegrationPDService(*so.ServiceName) + if err != nil { + t.Fatalf("Reading PD integration service object failed when it shouldn't: %s", err) + } + // ServiceKey is never returned by API, so we can't test it + assert.Equal(t, *so.ServiceName, *soRead.ServiceName) + + so.SetServiceKey("other") + err = client.UpdateIntegrationPDService(&so) + if err != nil { + t.Fatalf("Updating PD integration service object failed when it shouldn't: %s", err) + } + // we can't really test anything here, since only the ServiceKey was changed + + err = client.DeleteIntegrationPDService(*so.ServiceName) + if err != nil { + t.Fatalf("Deleting PD integration service object failed when it shouldn't: %s", err) + } +} + func getTestIntegrationPD() *datadog.IntegrationPDRequest { return &datadog.IntegrationPDRequest{ Services: []datadog.ServicePDRequest{ diff --git a/integrations.go b/integrations.go index a79223e..33b1bfd 100644 --- a/integrations.go +++ b/integrations.go @@ -68,6 +68,41 @@ 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 */