diff --git a/datadog/data_source_datadog_integration_pagerduty_service_object.go b/datadog/data_source_datadog_integration_pagerduty_service_object.go new file mode 100644 index 0000000000..d18bde5430 --- /dev/null +++ b/datadog/data_source_datadog_integration_pagerduty_service_object.go @@ -0,0 +1,67 @@ +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 dataSourceDatadogIntegrationPagerdutySO() *schema.Resource { + return &schema.Resource{ + Description: "Use this data source to retrieve individual Service Objects of Datadog PagerDuty integrations. Note that the Datadog PagerDuty integration must be activated in the Datadog UI in order for this resource to be usable.", + ReadContext: dataSourceDatadogIntegrationPagerdutySORead, + + Schema: map[string]*schema.Schema{ + "service_name": { + Description: "Your Service name in PagerDuty.", + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + }, + } +} + +func dataSourceDatadogIntegrationPagerdutySORead(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 { + searchedName := d.Get("service_name") + + resp, httpresp, err := apiInstances.GetPagerDutyIntegrationApiV1().GetPagerDutyIntegrationService(auth, searchedName.(string)) + + if err != nil { + if httpresp != nil && (httpresp.StatusCode == 504 || httpresp.StatusCode == 502) { + return resource.RetryableError(utils.TranslateClientError(err, httpresp, "error querying pagerduty integrations, retrying")) + } + if httpresp != nil && httpresp.StatusCode == 404 { + d.SetId("") + return nil + } + return resource.NonRetryableError(utils.TranslateClientError(err, httpresp, "error querying pagerduty integrations")) + } + + if serviceName, ok := resp.GetServiceNameOk(); !ok { + d.SetId("") + return resource.NonRetryableError(fmt.Errorf("couldn't find a pagerduty integration service named %s", *serviceName)) + } else { + d.SetId(*serviceName) + } + + return nil + }) + if err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/datadog/provider.go b/datadog/provider.go index f64c6c45a5..59abeae523 100644 --- a/datadog/provider.go +++ b/datadog/provider.go @@ -180,32 +180,33 @@ func Provider() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ - "datadog_api_key": dataSourceDatadogApiKey(), - "datadog_application_key": dataSourceDatadogApplicationKey(), - "datadog_cloud_workload_security_agent_rules": dataSourceDatadogCloudWorkloadSecurityAgentRules(), - "datadog_dashboard": dataSourceDatadogDashboard(), - "datadog_dashboard_list": dataSourceDatadogDashboardList(), - "datadog_integration_aws_logs_services": dataSourceDatadogIntegrationAWSLogsServices(), - "datadog_ip_ranges": dataSourceDatadogIPRanges(), - "datadog_logs_archives_order": dataSourceDatadogLogsArchivesOrder(), - "datadog_logs_indexes": dataSourceDatadogLogsIndexes(), - "datadog_logs_indexes_order": dataSourceDatadogLogsIndexesOrder(), - "datadog_logs_pipelines": dataSourceDatadogLogsPipelines(), - "datadog_monitor": dataSourceDatadogMonitor(), - "datadog_monitors": dataSourceDatadogMonitors(), - "datadog_monitor_config_policies": dataSourceDatadogMonitorConfigPolicies(), - "datadog_permissions": dataSourceDatadogPermissions(), - "datadog_role": dataSourceDatadogRole(), - "datadog_roles": dataSourceDatadogRoles(), - "datadog_rum_application": dataSourceDatadogRUMApplication(), - "datadog_security_monitoring_rules": dataSourceDatadogSecurityMonitoringRules(), - "datadog_security_monitoring_filters": dataSourceDatadogSecurityMonitoringFilters(), - "datadog_service_level_objective": dataSourceDatadogServiceLevelObjective(), - "datadog_service_level_objectives": dataSourceDatadogServiceLevelObjectives(), - "datadog_synthetics_locations": dataSourceDatadogSyntheticsLocations(), - "datadog_synthetics_global_variable": dataSourceDatadogSyntheticsGlobalVariable(), - "datadog_synthetics_test": dataSourceDatadogSyntheticsTest(), - "datadog_user": dataSourceDatadogUser(), + "datadog_api_key": dataSourceDatadogApiKey(), + "datadog_application_key": dataSourceDatadogApplicationKey(), + "datadog_cloud_workload_security_agent_rules": dataSourceDatadogCloudWorkloadSecurityAgentRules(), + "datadog_dashboard": dataSourceDatadogDashboard(), + "datadog_dashboard_list": dataSourceDatadogDashboardList(), + "datadog_integration_aws_logs_services": dataSourceDatadogIntegrationAWSLogsServices(), + "datadog_ip_ranges": dataSourceDatadogIPRanges(), + "datadog_logs_archives_order": dataSourceDatadogLogsArchivesOrder(), + "datadog_logs_indexes": dataSourceDatadogLogsIndexes(), + "datadog_logs_indexes_order": dataSourceDatadogLogsIndexesOrder(), + "datadog_logs_pipelines": dataSourceDatadogLogsPipelines(), + "datadog_monitor": dataSourceDatadogMonitor(), + "datadog_monitors": dataSourceDatadogMonitors(), + "datadog_monitor_config_policies": dataSourceDatadogMonitorConfigPolicies(), + "datadog_permissions": dataSourceDatadogPermissions(), + "datadog_role": dataSourceDatadogRole(), + "datadog_roles": dataSourceDatadogRoles(), + "datadog_rum_application": dataSourceDatadogRUMApplication(), + "datadog_security_monitoring_rules": dataSourceDatadogSecurityMonitoringRules(), + "datadog_security_monitoring_filters": dataSourceDatadogSecurityMonitoringFilters(), + "datadog_service_level_objective": dataSourceDatadogServiceLevelObjective(), + "datadog_service_level_objectives": dataSourceDatadogServiceLevelObjectives(), + "datadog_synthetics_locations": dataSourceDatadogSyntheticsLocations(), + "datadog_synthetics_global_variable": dataSourceDatadogSyntheticsGlobalVariable(), + "datadog_synthetics_test": dataSourceDatadogSyntheticsTest(), + "datadog_user": dataSourceDatadogUser(), + "datadog_integration_pagerduty_service_object": dataSourceDatadogIntegrationPagerdutySO(), }, ConfigureContextFunc: providerConfigure, diff --git a/datadog/tests/cassettes/TestAccDatadogPagerdutyIntegrationSODatasource.freeze b/datadog/tests/cassettes/TestAccDatadogPagerdutyIntegrationSODatasource.freeze new file mode 100644 index 0000000000..9058da7503 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogPagerdutyIntegrationSODatasource.freeze @@ -0,0 +1 @@ +2023-02-09T15:11:35.55929-05:00 \ No newline at end of file diff --git a/datadog/tests/cassettes/TestAccDatadogPagerdutyIntegrationSODatasource.yaml b/datadog/tests/cassettes/TestAccDatadogPagerdutyIntegrationSODatasource.yaml new file mode 100644 index 0000000000..dac0b8f98e --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogPagerdutyIntegrationSODatasource.yaml @@ -0,0 +1,154 @@ +--- +version: 1 +interactions: +- request: + body: | + {"service_key":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495","service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v1/integration/pagerduty/configuration/services + method: POST + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + 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/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: GET + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + 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/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: GET + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: {} + url: https://api.datadoghq.com/api/v1/integration/pagerduty + method: GET + response: + body: | + {"show_all_incidents":false,"filter_services":[],"schedules":["https://ddog.pagerduty.com/schedules/X123VF"],"api_token":"*****","services":[{"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495","service_key":"*****"}],"subdomain":"testdomain"} + 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/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: GET + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + 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/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: GET + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + 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/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: GET + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + 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/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: GET + response: + body: | + {"service_name":"tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495"} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - '*/*' + url: https://api.datadoghq.com/api/v1/integration/pagerduty/configuration/services/tf-TestAccDatadogPagerdutyIntegrationSODatasource-local-1675973495 + method: DELETE + response: + body: "" + headers: {} + status: 204 No Content + code: 204 + duration: "" diff --git a/datadog/tests/data_source_datadog_pagerduty_service_object_test.go b/datadog/tests/data_source_datadog_pagerduty_service_object_test.go new file mode 100644 index 0000000000..adb89689d8 --- /dev/null +++ b/datadog/tests/data_source_datadog_pagerduty_service_object_test.go @@ -0,0 +1,56 @@ +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 TestAccDatadogPagerdutyIntegrationSODatasource(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: testAccCheckDatadogIntegrationPagerdutyDestroy(accProvider), + Steps: []resource.TestStep{ + { + Config: testAccDatasourcePagerdutyIntegrationSOConfig(uniq), + Check: checkDatasourcePagerdutyIntegrationSOAttrs(accProvider, uniq), + }, + }, + }) +} + +func checkDatasourcePagerdutyIntegrationSOAttrs(accProvider func() (*schema.Provider, error), uniq string) resource.TestCheckFunc { + return resource.ComposeTestCheckFunc( + testAccCheckDatadogIntegrationPagerdutyExists(accProvider), + resource.TestCheckResourceAttr("data.datadog_integration_pagerduty_service_object.foo", "service_name", uniq), + ) +} + +func testAccPagerdutyIntegrationSOConfig(uniq string) string { + return fmt.Sprintf(` +resource "datadog_integration_pagerduty_service_object" "foo" { + service_name = "%s" + service_key = "%s" +} +`, uniq, uniq) +} + +func testAccDatasourcePagerdutyIntegrationSOConfig(uniq string) string { + return fmt.Sprintf(` +%s +data "datadog_integration_pagerduty_service_object" "foo" { + depends_on = [ + datadog_integration_pagerduty_service_object.foo + ] + service_name = "%s" +}`, testAccPagerdutyIntegrationSOConfig(uniq), uniq) +} diff --git a/datadog/tests/provider_test.go b/datadog/tests/provider_test.go index b5c14cb03e..004d0f15ac 100644 --- a/datadog/tests/provider_test.go +++ b/datadog/tests/provider_test.go @@ -65,6 +65,7 @@ var testFiles2EndpointTags = map[string]string{ "tests/data_source_datadog_monitors_test": "monitors", "tests/data_source_datadog_monitor_config_policy_test": "monitor-config-policies", "tests/data_source_datadog_monitor_config_policies_test": "monitor-config-policies", + "tests/data_source_datadog_pagerduty_service_object_test": "integration-pagerduty", "tests/data_source_datadog_permissions_test": "permissions", "tests/data_source_datadog_role_test": "roles", "tests/data_source_datadog_roles_test": "roles", diff --git a/docs/data-sources/integration_pagerduty_service_object.md b/docs/data-sources/integration_pagerduty_service_object.md new file mode 100644 index 0000000000..a8e2d1a420 --- /dev/null +++ b/docs/data-sources/integration_pagerduty_service_object.md @@ -0,0 +1,38 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "datadog_integration_pagerduty_service_object Data Source - terraform-provider-datadog" +subcategory: "" +description: |- + Use this data source to retrieve individual Service Objects of Datadog PagerDuty integrations. Note that the Datadog PagerDuty integration must be activated in the Datadog UI in order for this resource to be usable. +--- + +# datadog_integration_pagerduty_service_object (Data Source) + +Use this data source to retrieve individual Service Objects of Datadog PagerDuty integrations. Note that the Datadog PagerDuty integration must be activated in the Datadog UI in order for this resource to be usable. + +## Example Usage + +```terraform +data "datadog_integration_pagerduty_service_object" "foo" { + service_name = "foo" +} + +resource "datadog_integration_pagerduty_service_object" "foo" { + count = length(datadog_integration_pagerduty_service_object.foo) > 0 ? 1 : 0 + service_name = "foo" + service_key = "foo" +} +``` + + +## Schema + +### Required + +- `service_name` (String) Your Service name in PagerDuty. + +### Read-Only + +- `id` (String) The ID of this resource. + + diff --git a/examples/data-sources/datadog_integration_pagerduty_service_object/data-source.tf b/examples/data-sources/datadog_integration_pagerduty_service_object/data-source.tf new file mode 100644 index 0000000000..5e29a9b0fd --- /dev/null +++ b/examples/data-sources/datadog_integration_pagerduty_service_object/data-source.tf @@ -0,0 +1,9 @@ +data "datadog_integration_pagerduty_service_object" "foo" { + service_name = "foo" +} + +resource "datadog_integration_pagerduty_service_object" "foo" { + count = length(datadog_integration_pagerduty_service_object.foo) > 0 ? 1 : 0 + service_name = "foo" + service_key = "foo" +} \ No newline at end of file