Skip to content

Commit

Permalink
Add integrations/slack API Support (#151)
Browse files Browse the repository at this point in the history
* Add backwards-compatible fix to make handle optional

* Add Slack integration

* Add Slack integration and tests

* Generate Slack accessors

* Standardise PagerDuty spelling
  • Loading branch information
PauloMigAlmeida authored and zorkian committed Apr 8, 2018
1 parent fa40c76 commit ebe1e4a
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 7 deletions.
186 changes: 186 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,99 @@ func (c *ChangeWidget) SetY(v int) {
c.Y = &v
}

// GetAccount returns the Account field if non-nil, zero value otherwise.
func (c *ChannelSlackRequest) GetAccount() string {
if c == nil || c.Account == nil {
return ""
}
return *c.Account
}

// GetOkAccount returns a tuple with the Account field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (c *ChannelSlackRequest) GetAccountOk() (string, bool) {
if c == nil || c.Account == nil {
return "", false
}
return *c.Account, true
}

// HasAccount returns a boolean if a field has been set.
func (c *ChannelSlackRequest) HasAccount() bool {
if c != nil && c.Account != nil {
return true
}

return false
}

// SetAccount allocates a new c.Account and returns the pointer to it.
func (c *ChannelSlackRequest) SetAccount(v string) {
c.Account = &v
}

// GetChannelName returns the ChannelName field if non-nil, zero value otherwise.
func (c *ChannelSlackRequest) GetChannelName() string {
if c == nil || c.ChannelName == nil {
return ""
}
return *c.ChannelName
}

// GetOkChannelName returns a tuple with the ChannelName field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (c *ChannelSlackRequest) GetChannelNameOk() (string, bool) {
if c == nil || c.ChannelName == nil {
return "", false
}
return *c.ChannelName, true
}

// HasChannelName returns a boolean if a field has been set.
func (c *ChannelSlackRequest) HasChannelName() bool {
if c != nil && c.ChannelName != nil {
return true
}

return false
}

// SetChannelName allocates a new c.ChannelName and returns the pointer to it.
func (c *ChannelSlackRequest) SetChannelName(v string) {
c.ChannelName = &v
}

// GetTransferAllUserComments returns the TransferAllUserComments field if non-nil, zero value otherwise.
func (c *ChannelSlackRequest) GetTransferAllUserComments() bool {
if c == nil || c.TransferAllUserComments == nil {
return false
}
return *c.TransferAllUserComments
}

// GetOkTransferAllUserComments returns a tuple with the TransferAllUserComments field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (c *ChannelSlackRequest) GetTransferAllUserCommentsOk() (bool, bool) {
if c == nil || c.TransferAllUserComments == nil {
return false, false
}
return *c.TransferAllUserComments, true
}

// HasTransferAllUserComments returns a boolean if a field has been set.
func (c *ChannelSlackRequest) HasTransferAllUserComments() bool {
if c != nil && c.TransferAllUserComments != nil {
return true
}

return false
}

// SetTransferAllUserComments allocates a new c.TransferAllUserComments and returns the pointer to it.
func (c *ChannelSlackRequest) SetTransferAllUserComments(v bool) {
c.TransferAllUserComments = &v
}

// GetCheck returns the Check field if non-nil, zero value otherwise.
func (c *Check) GetCheck() string {
if c == nil || c.Check == nil {
Expand Down Expand Up @@ -7360,6 +7453,37 @@ func (i *IntegrationPDRequest) SetSubdomain(v string) {
i.Subdomain = &v
}

// GetRunCheck returns the RunCheck field if non-nil, zero value otherwise.
func (i *IntegrationSlackRequest) GetRunCheck() bool {
if i == nil || i.RunCheck == nil {
return false
}
return *i.RunCheck
}

// GetOkRunCheck returns a tuple with the RunCheck field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (i *IntegrationSlackRequest) GetRunCheckOk() (bool, bool) {
if i == nil || i.RunCheck == nil {
return false, false
}
return *i.RunCheck, true
}

// HasRunCheck returns a boolean if a field has been set.
func (i *IntegrationSlackRequest) HasRunCheck() bool {
if i != nil && i.RunCheck != nil {
return true
}

return false
}

// SetRunCheck allocates a new i.RunCheck and returns the pointer to it.
func (i *IntegrationSlackRequest) SetRunCheck(v bool) {
i.RunCheck = &v
}

// GetHost returns the Host field if non-nil, zero value otherwise.
func (m *Metric) GetHost() string {
if m == nil || m.Host == nil {
Expand Down Expand Up @@ -10398,6 +10522,68 @@ func (s *Series) SetUnits(v UnitPair) {
s.Units = &v
}

// GetAccount returns the Account field if non-nil, zero value otherwise.
func (s *ServiceHookSlackRequest) GetAccount() string {
if s == nil || s.Account == nil {
return ""
}
return *s.Account
}

// GetOkAccount returns a tuple with the Account field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (s *ServiceHookSlackRequest) GetAccountOk() (string, bool) {
if s == nil || s.Account == nil {
return "", false
}
return *s.Account, true
}

// HasAccount returns a boolean if a field has been set.
func (s *ServiceHookSlackRequest) HasAccount() bool {
if s != nil && s.Account != nil {
return true
}

return false
}

// SetAccount allocates a new s.Account and returns the pointer to it.
func (s *ServiceHookSlackRequest) SetAccount(v string) {
s.Account = &v
}

// GetUrl returns the Url field if non-nil, zero value otherwise.
func (s *ServiceHookSlackRequest) GetUrl() string {
if s == nil || s.Url == nil {
return ""
}
return *s.Url
}

// GetOkUrl returns a tuple with the Url field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (s *ServiceHookSlackRequest) GetUrlOk() (string, bool) {
if s == nil || s.Url == nil {
return "", false
}
return *s.Url, true
}

// HasUrl returns a boolean if a field has been set.
func (s *ServiceHookSlackRequest) HasUrl() bool {
if s != nil && s.Url != nil {
return true
}

return false
}

// SetUrl allocates a new s.Url and returns the pointer to it.
func (s *ServiceHookSlackRequest) SetUrl(v string) {
s.Url = &v
}

// GetServiceKey returns the ServiceKey field if non-nil, zero value otherwise.
func (s *servicePD) GetServiceKey() string {
if s == nil || s.ServiceKey == nil {
Expand Down
139 changes: 133 additions & 6 deletions integration/integrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ func init() {
client = initTest()
}

/*
PagerDuty Integration
*/

func TestIntegrationPDCreateAndDelete(t *testing.T) {
expected := createTestIntegrationPD(t)
defer cleanUpIntegrationPD(t)

actual, err := client.GetIntegrationPD()
if err != nil {
t.Fatalf("Retrieving a pagerduty integration failed when it shouldn't: (%s)", err)
t.Fatalf("Retrieving a PagerDuty integration failed when it shouldn't: (%s)", err)
}

expectedServiceNames := make([]*string, len(expected.Services))
Expand All @@ -43,12 +47,12 @@ func TestIntegrationPDUpdate(t *testing.T) {
})

if err := client.UpdateIntegrationPD(pdIntegration); err != nil {
t.Fatalf("Updating a pagerduty integration failed when it shouldn't: %s", err)
t.Fatalf("Updating a PagerDuty integration failed when it shouldn't: %s", err)
}

actual, err := client.GetIntegrationPD()
if err != nil {
t.Fatalf("Retrieving a pagerduty integration failed when it shouldn't: %s", err)
t.Fatalf("Retrieving a PagerDuty integration failed when it shouldn't: %s", err)
}

expectedServiceNames := make([]*string, len(pdIntegration.Services))
Expand Down Expand Up @@ -106,15 +110,15 @@ func createTestIntegrationPD(t *testing.T) *datadog.IntegrationPDRequest {
pdIntegration := getTestIntegrationPD()
err := client.CreateIntegrationPD(pdIntegration)
if err != nil {
t.Fatalf("Creating a pagerduty integration failed when it shouldn't: %s", err)
t.Fatalf("Creating a PagerDuty integration failed when it shouldn't: %s", err)
}

return pdIntegration
}

func cleanUpIntegrationPD(t *testing.T) {
if err := client.DeleteIntegrationPD(); err != nil {
t.Fatalf("Deleting the pagerduty integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
t.Fatalf("Deleting the PagerDuty integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}

pdIntegration, err := client.GetIntegrationPD()
Expand All @@ -123,6 +127,129 @@ func cleanUpIntegrationPD(t *testing.T) {
}

if err == nil {
t.Fatal("Fetching deleted pagerduty integration didn't lead to an error.")
t.Fatal("Fetching deleted PagerDuty integration didn't lead to an error.")
}
}

/*
Slack Integration
*/

func TestIntegrationSlackCreateAndDelete(t *testing.T) {
expected := createTestIntegrationSlack(t)
defer cleanUpIntegrationSlack(t)

actual, err := client.GetIntegrationSlack()
if err != nil {
t.Fatalf("Retrieving a Slack integration failed when it shouldn't: (%s)", err)
}

expectedServiceHooksAccounts := make([]*string, len(expected.ServiceHooks))
for _, service := range expected.ServiceHooks {
expectedServiceHooksAccounts = append(expectedServiceHooksAccounts, service.Account)
}

actualServiceHooksAccounts := make([]*string, len(actual.ServiceHooks))
for _, service := range actual.ServiceHooks {
actualServiceHooksAccounts = append(actualServiceHooksAccounts, service.Account)
}

assert.Equal(t, expectedServiceHooksAccounts, actualServiceHooksAccounts)
}

func TestIntegrationSlackUpdate(t *testing.T) {
slackIntegration := createTestIntegrationSlack(t)
defer cleanUpIntegrationSlack(t)

slackIntegration.ServiceHooks = append(slackIntegration.ServiceHooks, datadog.ServiceHookSlackRequest{
Account: datadog.String("Main_Account_2"),
Url: datadog.String("https://hooks.slack.com/services/2/2"),
})

if err := client.UpdateIntegrationSlack(slackIntegration); err != nil {
t.Fatalf("Updating a Slack integration failed when it shouldn't: %s", err)
}

actual, err := client.GetIntegrationSlack()
if err != nil {
t.Fatalf("Retrieving a Slack integration failed when it shouldn't: %s", err)
}

expectedServiceHooksAccounts := make([]*string, len(slackIntegration.ServiceHooks))
for _, service := range slackIntegration.ServiceHooks {
expectedServiceHooksAccounts = append(expectedServiceHooksAccounts, service.Account)
}

actualServiceHooksAccounts := make([]*string, len(actual.ServiceHooks))
for _, service := range actual.ServiceHooks {
actualServiceHooksAccounts = append(actualServiceHooksAccounts, service.Account)
}

assert.Equal(t, expectedServiceHooksAccounts, actualServiceHooksAccounts)
}

func TestIntegrationSlackGet(t *testing.T) {
slackIntegration := createTestIntegrationSlack(t)
defer cleanUpIntegrationSlack(t)

actual, err := client.GetIntegrationSlack()
if err != nil {
t.Fatalf("Retrieving Slack integration failed when it shouldn't: %s", err)
}

expectedServiceHooksAccounts := make([]*string, len(slackIntegration.ServiceHooks))
for _, service := range slackIntegration.ServiceHooks {
expectedServiceHooksAccounts = append(expectedServiceHooksAccounts, service.Account)
}

actualServiceHooksAccounts := make([]*string, len(actual.ServiceHooks))
for _, service := range actual.ServiceHooks {
actualServiceHooksAccounts = append(actualServiceHooksAccounts, service.Account)
}

assert.Equal(t, expectedServiceHooksAccounts, actualServiceHooksAccounts)
}

func getTestIntegrationSlack() *datadog.IntegrationSlackRequest {
return &datadog.IntegrationSlackRequest{
ServiceHooks: []datadog.ServiceHookSlackRequest{
{
Account: datadog.String("Main_Account"),
Url: datadog.String("https://hooks.slack.com/services/1/1"),
},
},
Channels: []datadog.ChannelSlackRequest{
{
ChannelName: datadog.String("#private"),
TransferAllUserComments: datadog.Bool(true),
Account: datadog.String("Main_Account"),
},
},
}
}

func createTestIntegrationSlack(t *testing.T) *datadog.IntegrationSlackRequest {
slackIntegration := getTestIntegrationSlack()

err := client.CreateIntegrationSlack(slackIntegration)
if err != nil {
t.Fatalf("Creating a Slack integration failed when it shouldn't: %s", err)
}

return slackIntegration
}

func cleanUpIntegrationSlack(t *testing.T) {
if err := client.DeleteIntegrationSlack(); err != nil {
t.Fatalf("Deleting the Slack integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}

slackIntegration, err := client.GetIntegrationSlack()
if slackIntegration != nil {
t.Fatal("Slack Integration hasn't been deleted when it should have been. Manual cleanup needed.")
}

if err == nil {
t.Fatal("Fetching deleted Slack integration didn't lead to an error.")
}
}
Loading

0 comments on commit ebe1e4a

Please sign in to comment.