Skip to content

[datadog_integration_slack_channel] Add datasource for slack channels #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions datadog/data_source_datadog_integration_slack_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package datadog

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDatadogIntegrationSlackChannel() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve individual Slack channel objects of Datadog Slack integrations. " +
"Note that the Datadog Slack Integration must be activated in the Datadog UI in order for this resource to be usable.",
ReadContext: dataSourceDatadogIntegrationSlackChannelRead,
Schema: map[string]*schema.Schema{
"account_name": {
Description: "Your Account name in Slack.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"channel_name": {
Description: "Your Channel name in Slack.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}

func dataSourceDatadogIntegrationSlackChannelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth

err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
searchedAccountName := d.Get("account_name")
searchedChannelName := d.Get("channel_name")

resp, httpresp, err := apiInstances.GetSlackIntegrationApiV1().GetSlackIntegrationChannel(auth, searchedAccountName.(string), searchedChannelName.(string))

if err != nil {
if httpresp != nil && (httpresp.StatusCode == 504 || httpresp.StatusCode == 502) {
return resource.RetryableError(utils.TranslateClientError(err, httpresp, "error querying slack integrations, retrying"))
}
if httpresp != nil && httpresp.StatusCode == 404 {
d.SetId("")
return nil
}
return resource.NonRetryableError(utils.TranslateClientError(err, httpresp, "error querying slack integrations"))
}

if channelName, ok := resp.GetNameOk(); !ok {
d.SetId("")
return resource.NonRetryableError(fmt.Errorf("couldn't find a slack integration channel named %s", *channelName))
} else {
d.SetId(*channelName)
}

return nil
})
if err != nil {
return diag.FromErr(err)
}

return nil
}
1 change: 1 addition & 0 deletions datadog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func Provider() *schema.Provider {
"datadog_dashboard": dataSourceDatadogDashboard(),
"datadog_dashboard_list": dataSourceDatadogDashboardList(),
"datadog_integration_aws_logs_services": dataSourceDatadogIntegrationAWSLogsServices(),
"datadog_integration_slack_channel": dataSourceDatadogIntegrationSlackChannel(),
"datadog_ip_ranges": dataSourceDatadogIPRanges(),
"datadog_logs_archives_order": dataSourceDatadogLogsArchivesOrder(),
"datadog_logs_indexes": dataSourceDatadogLogsIndexes(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-02-13T13:38:42.873198-05:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
version: 1
interactions:
- request:
body: |
{"display":{"message":false,"notified":false,"snapshot":false,"tags":false},"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522"}
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels
method: POST
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 201 Created
code: 201
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: GET
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/%23tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: GET
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: GET
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/%23tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: GET
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: GET
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: GET
response:
body: |
{"name":"#tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522","display":{"message":false,"snapshot":false,"tags":false,"notified":false}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- '*/*'
url: https://api.datadoghq.com/api/v1/integration/slack/configuration/accounts/test_account/channels/%23tf-TestAccDatadogIntegrationSlackChannelDatasource-local-1676313522
method: DELETE
response:
body: ""
headers:
Content-Type:
- text/html; charset=utf-8
status: 204 No Content
code: 204
duration: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func TestAccDatadogIntegrationSlackChannelDatasource(t *testing.T) {
t.Parallel()
ctx, accProviders := testAccProviders(context.Background(), t)
uniq := uniqueEntityName(ctx, t)
accProvider := testAccProvider(t, accProviders)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testAccCheckDatadogIntegrationSlackChannelDestroy(accProvider),
Steps: []resource.TestStep{
{
Config: testAccDatasourceSlackIntegrationChannelConfig(uniq),
Check: checkDatasourceSlackIntegrationChannelAttrs(accProvider, uniq),
},
},
})
}

func checkDatasourceSlackIntegrationChannelAttrs(accProvider func() (*schema.Provider, error), uniq string) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
testAccCheckDatadogIntegrationSlackChannelExists(accProvider, "datadog_integration_slack_channel.foo"),
resource.TestCheckResourceAttr("data.datadog_integration_slack_channel.foo", "channel_name", uniq),
)
}

func testAccSlackIntegrationChannelConfig(uniq string) string {
return fmt.Sprintf(`
resource "datadog_integration_slack_channel" "foo" {
account_name = "test_account"
channel_name = "#%s"
display {
message = false
notified = false
snapshot = false
tags = false
}
}
`, uniq)
}

func testAccDatasourceSlackIntegrationChannelConfig(uniq string) string {
return fmt.Sprintf(`
%s
data "datadog_integration_slack_channel" "foo" {
depends_on = [
datadog_integration_slack_channel.foo
]
account_name = "test_account"
channel_name = "%s"
}`, testAccSlackIntegrationChannelConfig(uniq), uniq)
}
1 change: 1 addition & 0 deletions datadog/tests/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var testFiles2EndpointTags = map[string]string{
"tests/data_source_datadog_security_monitoring_filters_test": "security-monitoring",
"tests/data_source_datadog_service_level_objective_test": "service-level-objectives",
"tests/data_source_datadog_service_level_objectives_test": "service-level-objectives",
"tests/data_source_datadog_integration_slack_channel_test": "integration-slack-channel",
"tests/data_source_datadog_synthetics_locations_test": "synthetics",
"tests/data_source_datadog_synthetics_global_variable_test": "synthetics",
"tests/data_source_datadog_synthetics_test_test": "synthetics",
Expand Down
47 changes: 47 additions & 0 deletions docs/data-sources/integration_slack_channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "datadog_integration_slack_channel Data Source - terraform-provider-datadog"
subcategory: ""
description: |-
Use this data source to retrieve individual Slack channel objects of Datadog Slack integrations. Note that the Datadog Slack Integration must be activated in the Datadog UI in order for this resource to be usable.
---

# datadog_integration_slack_channel (Data Source)

Use this data source to retrieve individual Slack channel objects of Datadog Slack integrations. Note that the Datadog Slack Integration must be activated in the Datadog UI in order for this resource to be usable.

## Example Usage

```terraform
data "datadog_integration_slack_channel" "foo" {
account_name = "test_account"
channel_name = "foo"

}

resource "datadog_integration_slack_channel" "foo" {
count = length(datadog_integration_slack_channel.foo) > 0 ? 1 : 0
account_name = "test_account"
channel_name = "foo"
display {
message = false
notified = false
snapshot = false
tags = false
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `account_name` (String) Your Account name in Slack.
- `channel_name` (String) Your Channel name in Slack.

### Read-Only

- `id` (String) The ID of this resource.


Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
data "datadog_integration_slack_channel" "foo" {
account_name = "test_account"
channel_name = "foo"

}

resource "datadog_integration_slack_channel" "foo" {
count = length(datadog_integration_slack_channel.foo) > 0 ? 1 : 0
account_name = "test_account"
channel_name = "foo"
display {
message = false
notified = false
snapshot = false
tags = false
}
}