diff --git a/datadog/data_source_datadog_logs_indexes.go b/datadog/data_source_datadog_logs_indexes.go new file mode 100644 index 0000000000..b21ff4ec70 --- /dev/null +++ b/datadog/data_source_datadog_logs_indexes.go @@ -0,0 +1,129 @@ +package datadog + +import ( + "context" + + "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 dataSourceDatadogLogsIndexes() *schema.Resource { + return &schema.Resource{ + Description: "Use this data source to list several existing logs indexes for use in other resources.", + ReadContext: dataSourceDatadogLogsIndexesRead, + Schema: map[string]*schema.Schema{ + // Computed values + "logs_indexes": { + Description: "List of logs indexes", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Description: "The name of the index.", + Type: schema.TypeString, + Computed: true, + }, + "daily_limit": { + Description: "The number of log events you can send in this index per day before you are rate-limited.", + Type: schema.TypeInt, + Computed: true, + }, + "retention_days": { + Description: "The number of days before logs are deleted from this index.", + Type: schema.TypeInt, + Computed: true, + }, + "filter": { + Description: "Logs filter", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query": { + Description: "Logs filter criteria. Only logs matching this filter criteria are considered for this index.", + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, + "exclusion_filter": { + Description: "List of exclusion filters.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: dataSourceLogsIndexesExclusionFilterSchema, + }, + }, + }, + }, + }, + }, + } +} + +var dataSourceLogsIndexesExclusionFilterSchema = map[string]*schema.Schema{ + "name": { + Description: "The name of the exclusion filter.", + Type: schema.TypeString, + Computed: true, + }, + "is_enabled": { + Description: "A boolean stating if the exclusion is active or not.", + Type: schema.TypeBool, + Computed: true, + }, + "filter": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query": { + Description: "Only logs matching the filter criteria and the query of the parent index will be considered for this exclusion filter.", + Type: schema.TypeString, + Computed: true, + }, + "sample_rate": { + Description: "The fraction of logs excluded by the exclusion filter, when active.", + Type: schema.TypeFloat, + Computed: true, + }, + }, + }, + }, +} + +func dataSourceDatadogLogsIndexesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + providerConf := meta.(*ProviderConfiguration) + datadogClientV1 := providerConf.DatadogClientV1 + authV1 := providerConf.AuthV1 + + logsIndexes, httpresp, err := datadogClientV1.LogsIndexesApi.ListLogIndexes(authV1) + if err != nil { + return utils.TranslateClientErrorDiag(err, httpresp, "error querying log indexes") + } + if err := utils.CheckForUnparsed(logsIndexes); err != nil { + return diag.FromErr(err) + } + + tfLogsIndexes := make([]map[string]interface{}, len(logsIndexes.GetIndexes())) + for i, l := range logsIndexes.GetIndexes() { + tfLogsIndexes[i] = map[string]interface{}{ + "name": l.GetName(), + "daily_limit": l.GetDailyLimit(), + "retention_days": l.GetNumRetentionDays(), + "filter": buildTerraformIndexFilter(l.GetFilter()), + "exclusion_filter": buildTerraformExclusionFilters(l.GetExclusionFilters()), + } + } + if err := d.Set("logs_indexes", tfLogsIndexes); err != nil { + return diag.FromErr(err) + } + + d.SetId("log-indexes") + + return nil +} diff --git a/datadog/provider.go b/datadog/provider.go index e63cf6e6de..55a0dfeb25 100644 --- a/datadog/provider.go +++ b/datadog/provider.go @@ -180,6 +180,7 @@ func Provider() *schema.Provider { "datadog_dashboard": dataSourceDatadogDashboard(), "datadog_dashboard_list": dataSourceDatadogDashboardList(), "datadog_ip_ranges": dataSourceDatadogIPRanges(), + "datadog_logs_indexes": dataSourceDatadogLogsIndexes(), "datadog_logs_indexes_order": dataSourceDatadogLogsIndexesOrder(), "datadog_monitor": dataSourceDatadogMonitor(), "datadog_monitors": dataSourceDatadogMonitors(), diff --git a/datadog/tests/cassettes/TestAccDatadogLogsIndexesDatasource.freeze b/datadog/tests/cassettes/TestAccDatadogLogsIndexesDatasource.freeze new file mode 100644 index 0000000000..d2594595dc --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogLogsIndexesDatasource.freeze @@ -0,0 +1 @@ +2022-02-03T16:49:32.359068-05:00 \ No newline at end of file diff --git a/datadog/tests/cassettes/TestAccDatadogLogsIndexesDatasource.yaml b/datadog/tests/cassettes/TestAccDatadogLogsIndexesDatasource.yaml new file mode 100644 index 0000000000..d2a2e24ae2 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogLogsIndexesDatasource.yaml @@ -0,0 +1,291 @@ +--- +version: 1 +interactions: +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - ListLogIndexes + User-Agent: + - terraform-provider-datadog/dev (terraform 2.10.0; terraform-cli 0.14.7) datadog-api-client-go/1.8.0 (go go1.17; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: GET + response: + body: '{"indexes":[{"num_retention_days":15,"name":"main","is_rate_limited":false,"filter":{"query":""},"exclusion_filters":[],"daily_limit":null},{"num_retention_days":15,"name":"test-index","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"test-index-2","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[],"daily_limit":null}]}' + headers: + Cache-Control: + - no-cache + Connection: + - keep-alive + Content-Security-Policy: + - frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report + Content-Type: + - application/json + Date: + - Thu, 03 Feb 2022 21:49:34 GMT + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=15724800; + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Ratelimit-Limit: + - "30" + X-Ratelimit-Name: + - logs_public_config_api_indexes_read + X-Ratelimit-Period: + - "10" + X-Ratelimit-Remaining: + - "29" + X-Ratelimit-Reset: + - "6" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - ListLogIndexes + User-Agent: + - terraform-provider-datadog/dev (terraform 2.10.0; terraform-cli 0.14.7) datadog-api-client-go/1.8.0 (go go1.17; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: GET + response: + body: '{"indexes":[{"num_retention_days":15,"name":"main","is_rate_limited":false,"filter":{"query":""},"exclusion_filters":[],"daily_limit":null},{"num_retention_days":15,"name":"test-index","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"test-index-2","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[],"daily_limit":null}]}' + headers: + Cache-Control: + - no-cache + Connection: + - keep-alive + Content-Security-Policy: + - frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report + Content-Type: + - application/json + Date: + - Thu, 03 Feb 2022 21:49:34 GMT + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=15724800; + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Ratelimit-Limit: + - "30" + X-Ratelimit-Name: + - logs_public_config_api_indexes_read + X-Ratelimit-Period: + - "10" + X-Ratelimit-Remaining: + - "28" + X-Ratelimit-Reset: + - "6" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - ListLogIndexes + User-Agent: + - terraform-provider-datadog/dev (terraform 2.10.0; terraform-cli 0.14.7) datadog-api-client-go/1.8.0 (go go1.17; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: GET + response: + body: '{"indexes":[{"num_retention_days":15,"name":"main","is_rate_limited":false,"filter":{"query":""},"exclusion_filters":[],"daily_limit":null},{"num_retention_days":15,"name":"test-index","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"test-index-2","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[],"daily_limit":null}]}' + headers: + Cache-Control: + - no-cache + Connection: + - keep-alive + Content-Security-Policy: + - frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report + Content-Type: + - application/json + Date: + - Thu, 03 Feb 2022 21:49:34 GMT + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=15724800; + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Ratelimit-Limit: + - "30" + X-Ratelimit-Name: + - logs_public_config_api_indexes_read + X-Ratelimit-Period: + - "10" + X-Ratelimit-Remaining: + - "27" + X-Ratelimit-Reset: + - "6" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - ListLogIndexes + User-Agent: + - terraform-provider-datadog/dev (terraform 2.10.0; terraform-cli 0.14.7) datadog-api-client-go/1.8.0 (go go1.17; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: GET + response: + body: '{"indexes":[{"num_retention_days":15,"name":"main","is_rate_limited":false,"filter":{"query":""},"exclusion_filters":[],"daily_limit":null},{"num_retention_days":15,"name":"test-index","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"test-index-2","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[],"daily_limit":null}]}' + headers: + Cache-Control: + - no-cache + Connection: + - keep-alive + Content-Security-Policy: + - frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report + Content-Type: + - application/json + Date: + - Thu, 03 Feb 2022 21:49:34 GMT + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=15724800; + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Ratelimit-Limit: + - "30" + X-Ratelimit-Name: + - logs_public_config_api_indexes_read + X-Ratelimit-Period: + - "10" + X-Ratelimit-Remaining: + - "26" + X-Ratelimit-Reset: + - "6" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - ListLogIndexes + User-Agent: + - terraform-provider-datadog/dev (terraform 2.10.0; terraform-cli 0.14.7) datadog-api-client-go/1.8.0 (go go1.17; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: GET + response: + body: '{"indexes":[{"num_retention_days":15,"name":"main","is_rate_limited":false,"filter":{"query":""},"exclusion_filters":[],"daily_limit":null},{"num_retention_days":15,"name":"test-index","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"test-index-2","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[],"daily_limit":null}]}' + headers: + Cache-Control: + - no-cache + Connection: + - keep-alive + Content-Security-Policy: + - frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report + Content-Type: + - application/json + Date: + - Thu, 03 Feb 2022 21:49:35 GMT + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=15724800; + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Ratelimit-Limit: + - "30" + X-Ratelimit-Name: + - logs_public_config_api_indexes_read + X-Ratelimit-Period: + - "10" + X-Ratelimit-Remaining: + - "25" + X-Ratelimit-Reset: + - "5" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - ListLogIndexes + User-Agent: + - terraform-provider-datadog/dev (terraform 2.10.0; terraform-cli 0.14.7) datadog-api-client-go/1.8.0 (go go1.17; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: GET + response: + body: '{"indexes":[{"num_retention_days":15,"name":"main","is_rate_limited":false,"filter":{"query":""},"exclusion_filters":[],"daily_limit":null},{"num_retention_days":15,"name":"test-index","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"test-index-2","is_rate_limited":false,"filter":{"query":"query"},"exclusion_filters":[],"daily_limit":20202020},{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[],"daily_limit":null}]}' + headers: + Cache-Control: + - no-cache + Connection: + - keep-alive + Content-Security-Policy: + - frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report + Content-Type: + - application/json + Date: + - Thu, 03 Feb 2022 21:49:35 GMT + Pragma: + - no-cache + Strict-Transport-Security: + - max-age=15724800; + Vary: + - Accept-Encoding + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Ratelimit-Limit: + - "30" + X-Ratelimit-Name: + - logs_public_config_api_indexes_read + X-Ratelimit-Period: + - "10" + X-Ratelimit-Remaining: + - "24" + X-Ratelimit-Reset: + - "5" + status: 200 OK + code: 200 + duration: "" diff --git a/datadog/tests/data_source_datadog_logs_indexes_test.go b/datadog/tests/data_source_datadog_logs_indexes_test.go new file mode 100644 index 0000000000..cc1d2c5587 --- /dev/null +++ b/datadog/tests/data_source_datadog_logs_indexes_test.go @@ -0,0 +1,67 @@ +package test + +import ( + "context" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + + "github.com/terraform-providers/terraform-provider-datadog/datadog" + "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDatadogLogsIndexesDatasource(t *testing.T) { + _, accProviders := testAccProviders(context.Background(), t) + accProvider := testAccProvider(t, accProviders) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: accProviders, + Steps: []resource.TestStep{ + { + Config: testAccDatasourceLogsIndexesConfig(), + Check: resource.ComposeTestCheckFunc( + dataLogsIndexesCountCheck(accProvider), + ), + }, + }, + }) +} + +func testAccDatasourceLogsIndexesConfig() string { + return fmt.Sprintf(` +data "datadog_logs_indexes" "foo" { +}`) +} + +func dataLogsIndexesCountCheck(accProvider func() (*schema.Provider, error)) func(state *terraform.State) error { + return func(state *terraform.State) error { + provider, _ := accProvider() + providerConf := provider.Meta().(*datadog.ProviderConfiguration) + authV1 := providerConf.AuthV1 + client := providerConf.DatadogClientV1 + + logsIndexes, _, err := client.LogsIndexesApi.ListLogIndexes(authV1) + if err != nil { + return err + } + if err := utils.CheckForUnparsed(logsIndexes); err != nil { + return err + } + + resourceAttributes := state.RootModule().Resources["data.datadog_logs_indexes.foo"].Primary.Attributes + logIndexesCount, _ := strconv.Atoi(resourceAttributes["logs_indexes.#"]) + + if logIndexesCount != len(logsIndexes.GetIndexes()) { + return fmt.Errorf("expected %d indexes got %d indexes", + logIndexesCount, len(logsIndexes.GetIndexes())) + } + + return nil + } +} diff --git a/datadog/tests/provider_test.go b/datadog/tests/provider_test.go index 612fb68381..aa09a2d059 100644 --- a/datadog/tests/provider_test.go +++ b/datadog/tests/provider_test.go @@ -56,6 +56,7 @@ var testFiles2EndpointTags = map[string]string{ "tests/data_source_datadog_dashboard_list_test": "dashboard-lists", "tests/data_source_datadog_ip_ranges_test": "ip-ranges", "tests/data_source_datadog_logs_indexes_order_test": "logs-index", + "tests/data_source_datadog_logs_indexes_test": "logs-index", "tests/data_source_datadog_monitor_test": "monitors", "tests/data_source_datadog_monitors_test": "monitors", "tests/data_source_datadog_permissions_test": "permissions", diff --git a/docs/data-sources/logs_indexes.md b/docs/data-sources/logs_indexes.md new file mode 100644 index 0000000000..5e9201e3f1 --- /dev/null +++ b/docs/data-sources/logs_indexes.md @@ -0,0 +1,64 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "datadog_logs_indexes Data Source - terraform-provider-datadog" +subcategory: "" +description: |- + Use this data source to list several existing logs indexes for use in other resources. +--- + +# datadog_logs_indexes (Data Source) + +Use this data source to list several existing logs indexes for use in other resources. + +## Example Usage + +```terraform +data "datadog_logs_indexes" "test" {} +``` + + +## Schema + +### Read-Only + +- **id** (String) The ID of this resource. +- **logs_indexes** (List of Object) List of logs indexes (see [below for nested schema](#nestedatt--logs_indexes)) + + +### Nested Schema for `logs_indexes` + +Read-Only: + +- **daily_limit** (Number) +- **exclusion_filter** (List of Object) (see [below for nested schema](#nestedobjatt--logs_indexes--exclusion_filter)) +- **filter** (List of Object) (see [below for nested schema](#nestedobjatt--logs_indexes--filter)) +- **name** (String) +- **retention_days** (Number) + + +### Nested Schema for `logs_indexes.exclusion_filter` + +Read-Only: + +- **filter** (List of Object) (see [below for nested schema](#nestedobjatt--logs_indexes--exclusion_filter--filter)) +- **is_enabled** (Boolean) +- **name** (String) + + +### Nested Schema for `logs_indexes.exclusion_filter.filter` + +Read-Only: + +- **query** (String) +- **sample_rate** (Number) + + + + +### Nested Schema for `logs_indexes.filter` + +Read-Only: + +- **query** (String) + + diff --git a/examples/data-sources/datadog_logs_indexes/data-source.tf b/examples/data-sources/datadog_logs_indexes/data-source.tf new file mode 100644 index 0000000000..1488093b71 --- /dev/null +++ b/examples/data-sources/datadog_logs_indexes/data-source.tf @@ -0,0 +1 @@ +data "datadog_logs_indexes" "test" {}