From ad059f8953ef0537be8b5c941b4ec3bebef5f69e Mon Sep 17 00:00:00 2001 From: Hippolyte HENRY Date: Tue, 27 Jul 2021 17:01:56 +0200 Subject: [PATCH] Add logs index creation (#1155) --- datadog/internal/utils/test_utils.go | 5 + datadog/resource_datadog_logs_index.go | 51 ++- .../TestAccDatadogLogsIndex_Basic.freeze | 1 + .../TestAccDatadogLogsIndex_Basic.yaml | 380 ++++++++++++++++++ datadog/tests/provider_test.go | 1 + .../tests/resource_datadog_logs_index_test.go | 130 ++++++ docs/resources/logs_index.md | 7 +- .../resources/datadog_logs_index/resource.tf | 2 +- 8 files changed, 555 insertions(+), 22 deletions(-) create mode 100644 datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.freeze create mode 100644 datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.yaml create mode 100644 datadog/tests/resource_datadog_logs_index_test.go diff --git a/datadog/internal/utils/test_utils.go b/datadog/internal/utils/test_utils.go index 27cc2e9bb1..1f53bae122 100644 --- a/datadog/internal/utils/test_utils.go +++ b/datadog/internal/utils/test_utils.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "os" "time" ) @@ -21,6 +22,10 @@ func Retry(interval time.Duration, count int, call func() error) error { return nil } else if errors.Is(err, retryErrorType) { log.Print(err.Error()) + if os.Getenv("RECORD") == "false" { + // Skip sleep in replay mode to go faster + continue + } time.Sleep(interval) } else if errors.Is(err, fatalErrorType) { log.Print(err.Error()) diff --git a/datadog/resource_datadog_logs_index.go b/datadog/resource_datadog_logs_index.go index e55aaf12e0..07154fed6b 100644 --- a/datadog/resource_datadog_logs_index.go +++ b/datadog/resource_datadog_logs_index.go @@ -3,7 +3,6 @@ package datadog import ( "context" "log" - "strings" "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils" @@ -101,7 +100,7 @@ var exclusionFilterSchema = map[string]*schema.Schema{ func resourceDatadogLogsIndex() *schema.Resource { return &schema.Resource{ - Description: "Provides a Datadog Logs Index API resource. This can be used to create and manage Datadog logs indexes.", + Description: "Provides a Datadog Logs Index API resource. This can be used to create and manage Datadog logs indexes. \n**Note:** It is not possible to delete logs indexes through Terraform, so an index remains in your account after the resource is removed from your terraform config. Reach out to support to delete a logs index.", CreateContext: resourceDatadogLogsIndexCreate, UpdateContext: resourceDatadogLogsIndexUpdate, ReadContext: resourceDatadogLogsIndexRead, @@ -114,13 +113,18 @@ func resourceDatadogLogsIndex() *schema.Resource { } func resourceDatadogLogsIndexCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - // This is a bit of a hack to ensure we fail fast if an index is about to be created, and - // to ensure we provide a useful error message (and don't panic) - // Indexes can only be updated, and the id is only set in the state if it was already imported - if _, ok := d.GetOk("id"); !ok { - return diag.Errorf("logs index creation is not allowed, please import the index first. index_name: %s", d.Get("name").(string)) + providerConf := meta.(*ProviderConfiguration) + datadogClientV1 := providerConf.DatadogClientV1 + authV1 := providerConf.AuthV1 + + ddIndex := buildDatadogIndexCreateRequest(d) + createdIndex, httpResponse, err := datadogClientV1.LogsIndexesApi.CreateLogsIndex(authV1, *ddIndex) + if err != nil { + return utils.TranslateClientErrorDiag(err, httpResponse, "error creating logs index") } - return resourceDatadogLogsIndexUpdate(ctx, d, meta) + d.SetId(createdIndex.GetName()) + + return updateLogsIndexState(d, &createdIndex) } func updateLogsIndexState(d *schema.ResourceData, index *datadogV1.LogsIndex) diag.Diagnostics { @@ -162,23 +166,16 @@ func resourceDatadogLogsIndexRead(ctx context.Context, d *schema.ResourceData, m } func resourceDatadogLogsIndexUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - ddIndex, err := buildDatadogIndex(d) - if err != nil { - return diag.Errorf("failed to parse resource configuration: %s", err.Error()) - } providerConf := meta.(*ProviderConfiguration) datadogClientV1 := providerConf.DatadogClientV1 authV1 := providerConf.AuthV1 + ddIndex := buildDatadogIndexUpdateRequest(d) tfName := d.Get("name").(string) updatedIndex, httpResponse, err := datadogClientV1.LogsIndexesApi.UpdateLogsIndex(authV1, tfName, *ddIndex) if err != nil { - if strings.Contains(err.Error(), "404 Not Found") { - return diag.Errorf("logs index creation is not allowed, index_name: %s", tfName) - } return utils.TranslateClientErrorDiag(err, httpResponse, "error updating logs index") } - d.SetId(tfName) return updateLogsIndexState(d, &updatedIndex) } @@ -186,7 +183,7 @@ func resourceDatadogLogsIndexDelete(_ context.Context, _ *schema.ResourceData, _ return nil } -func buildDatadogIndex(d *schema.ResourceData) (*datadogV1.LogsIndexUpdateRequest, error) { +func buildDatadogIndexUpdateRequest(d *schema.ResourceData) *datadogV1.LogsIndexUpdateRequest { var ddIndex datadogV1.LogsIndexUpdateRequest if tfFilter := d.Get("filter").([]interface{}); len(tfFilter) > 0 { ddIndex.SetFilter(*buildDatadogIndexFilter(tfFilter[0].(map[string]interface{}))) @@ -203,7 +200,25 @@ func buildDatadogIndex(d *schema.ResourceData) (*datadogV1.LogsIndexUpdateReques } ddIndex.ExclusionFilters = buildDatadogExclusionFilters(d.Get("exclusion_filter").([]interface{})) - return &ddIndex, nil + return &ddIndex +} + +func buildDatadogIndexCreateRequest(d *schema.ResourceData) *datadogV1.LogsIndex { + var ddIndex datadogV1.LogsIndex + + ddIndex.SetName(d.Get("name").(string)) + + if tfFilter := d.Get("filter").([]interface{}); len(tfFilter) > 0 { + ddIndex.SetFilter(*buildDatadogIndexFilter(tfFilter[0].(map[string]interface{}))) + } + if v, ok := d.GetOk("daily_limit"); ok { + ddIndex.SetDailyLimit(int64(v.(int))) + } + if v, ok := d.GetOk("retention_days"); ok { + ddIndex.SetNumRetentionDays(int64(v.(int))) + } + ddIndex.ExclusionFilters = buildDatadogExclusionFilters(d.Get("exclusion_filter").([]interface{})) + return &ddIndex } func buildDatadogIndexFilter(tfFilter map[string]interface{}) *datadogV1.LogsFilter { diff --git a/datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.freeze b/datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.freeze new file mode 100644 index 0000000000..e71545c512 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.freeze @@ -0,0 +1 @@ +2021-07-26T15:13:21.354859+02:00 \ No newline at end of file diff --git a/datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.yaml b/datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.yaml new file mode 100644 index 0000000000..04e9dc4583 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogLogsIndex_Basic.yaml @@ -0,0 +1,380 @@ +--- +version: 1 +interactions: +- request: + body: | + {"daily_limit":200000,"exclusion_filters":[],"filter":{"query":"non-existent-query"},"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","num_retention_days":15} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + Dd-Operation-Id: + - CreateLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes + method: POST + response: + body: '{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"non-existent-query"},"exclusion_filters":[],"daily_limit":200000}' + 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: + - Mon, 26 Jul 2021 13:13:24 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-Period: + - "10" + X-Ratelimit-Remaining: + - "29" + X-Ratelimit-Reset: + - "7" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - GetLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: GET + response: + body: '{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"non-existent-query"},"exclusion_filters":[],"daily_limit":200000}' + 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: + - Mon, 26 Jul 2021 13:13: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-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: + - GetLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: GET + response: + body: '{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"non-existent-query"},"exclusion_filters":[],"daily_limit":200000}' + 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: + - Mon, 26 Jul 2021 13:13: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-Period: + - "10" + X-Ratelimit-Remaining: + - "28" + X-Ratelimit-Reset: + - "5" + status: 200 OK + code: 200 + duration: "" +- request: + body: | + {"daily_limit":20000,"exclusion_filters":[{"filter":{"query":"app:coredns","sample_rate":0.97},"is_enabled":true,"name":"Filter coredns logs"}],"filter":{"query":"test:query"},"num_retention_days":15} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + Dd-Operation-Id: + - UpdateLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: PUT + response: + body: '{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[{"is_enabled":true,"filter":{"query":"app:coredns","sample_rate":0.97},"name":"Filter coredns logs"}],"daily_limit":20000}' + 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: + - Mon, 26 Jul 2021 13:13:36 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-Period: + - "10" + X-Ratelimit-Remaining: + - "29" + X-Ratelimit-Reset: + - "4" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - GetLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: GET + response: + body: '{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[{"is_enabled":true,"filter":{"query":"app:coredns","sample_rate":0.97},"name":"Filter coredns logs"}],"daily_limit":20000}' + 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: + - Mon, 26 Jul 2021 13:13:47 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-Period: + - "10" + X-Ratelimit-Remaining: + - "29" + X-Ratelimit-Reset: + - "3" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - GetLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: GET + response: + body: '{"num_retention_days":15,"name":"tf-testaccdatadoglogsindex-basic-local-1627305201","is_rate_limited":false,"filter":{"query":"test:query"},"exclusion_filters":[{"is_enabled":true,"filter":{"query":"app:coredns","sample_rate":0.97},"name":"Filter coredns logs"}],"daily_limit":20000}' + 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: + - Mon, 26 Jul 2021 13:13:48 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-Period: + - "10" + X-Ratelimit-Remaining: + - "28" + X-Ratelimit-Reset: + - "2" + status: 200 OK + code: 200 + duration: "" +- request: + body: | + {"daily_limit":20000,"disable_daily_limit":true,"exclusion_filters":[],"filter":{"query":"test:query"},"num_retention_days":15} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + Dd-Operation-Id: + - UpdateLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: PUT + response: + body: '{"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: + - Mon, 26 Jul 2021 13:13:48 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-Period: + - "10" + X-Ratelimit-Remaining: + - "29" + X-Ratelimit-Reset: + - "2" + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + Dd-Operation-Id: + - GetLogsIndex + User-Agent: + - terraform-provider-datadog/dev (terraform 2.7.0; terraform-cli 1.0.0) datadog-api-client-go/1.2.1+dev (go go1.16; os darwin; arch amd64) + url: https://api.datadoghq.com/api/v1/logs/config/indexes/tf-testaccdatadoglogsindex-basic-local-1627305201 + method: GET + response: + body: '{"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: + - Mon, 26 Jul 2021 13:13:59 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-Period: + - "10" + X-Ratelimit-Remaining: + - "29" + X-Ratelimit-Reset: + - "1" + status: 200 OK + code: 200 + duration: "" diff --git a/datadog/tests/provider_test.go b/datadog/tests/provider_test.go index 40ee9040f0..67c7605e9b 100644 --- a/datadog/tests/provider_test.go +++ b/datadog/tests/provider_test.go @@ -107,6 +107,7 @@ var testFiles2EndpointTags = map[string]string{ "tests/resource_datadog_integration_slack_channel_test": "integration-slack-channel", "tests/resource_datadog_logs_archive_test": "logs-archive", "tests/resource_datadog_logs_archive_order_test": "logs-archive-order", + "tests/resource_datadog_logs_index_test": "logs-index", "tests/resource_datadog_logs_custom_pipeline_test": "logs-pipelines", "tests/resource_datadog_logs_metric_test": "logs-metric", "tests/resource_datadog_metric_metadata_test": "metrics", diff --git a/datadog/tests/resource_datadog_logs_index_test.go b/datadog/tests/resource_datadog_logs_index_test.go new file mode 100644 index 0000000000..34af601ff2 --- /dev/null +++ b/datadog/tests/resource_datadog_logs_index_test.go @@ -0,0 +1,130 @@ +package test + +import ( + "context" + "fmt" + "log" + "strings" + "testing" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccDatadogLogsIndex_Basic(t *testing.T) { + if !isReplaying() { + // Skip in non replaying mode, since we can't delete indexes via the public API, plus the API takes a while to be consistent + // If you really need to record the interactions, comment the following return statement, and run locally. + log.Println("Skipping logs indexes tests in non replaying mode") + return + } + + ctx, accProviders := testAccProviders(context.Background(), t) + uniq := strings.ToLower(strings.ReplaceAll(uniqueEntityName(ctx, t), "_", "-")) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: accProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckDatadogCreateLogsIndexConfig(uniq), + Check: resource.ComposeTestCheckFunc( + sleep(), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "name", uniq), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "daily_limit", "200000"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "disable_daily_limit", "false"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "retention_days", "15"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "filter.#", "1"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "filter.0.query", "non-existent-query"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "exclusion_filter.#", "0"), + ), + }, + { + Config: testAccCheckDatadogUpdateLogsIndexConfig(uniq), + Check: resource.ComposeTestCheckFunc( + sleep(), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "name", uniq), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "daily_limit", "20000"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "disable_daily_limit", "false"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "retention_days", "15"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "filter.0.query", "test:query"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "exclusion_filter.0.name", "Filter coredns logs"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "exclusion_filter.0.is_enabled", "true"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "exclusion_filter.0.filter.0.query", "app:coredns"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "exclusion_filter.0.filter.0.sample_rate", "0.97"), + ), + }, + { + Config: testAccCheckDatadogUpdateLogsIndexDisableDailyLimitConfig(uniq), + PreventPostDestroyRefresh: true, + Check: resource.ComposeTestCheckFunc( + sleep(), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "name", uniq), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "disable_daily_limit", "true"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "retention_days", "15"), + resource.TestCheckResourceAttr("datadog_logs_index.sample_index", "exclusion_filter.#", "0"), + ), + }, + }, + }) +} + +func sleep() resource.TestCheckFunc { + return func(s *terraform.State) error { + if isReplaying() { + return nil + } + time.Sleep(10 * time.Second) + return nil + } +} + +func testAccCheckDatadogCreateLogsIndexConfig(name string) string { + return fmt.Sprintf(` +resource "datadog_logs_index" "sample_index" { + name = "%s" + daily_limit = 200000 + retention_days = 15 + filter { + query = "non-existent-query" + } +} +`, name) +} + +func testAccCheckDatadogUpdateLogsIndexConfig(name string) string { + return fmt.Sprintf(` +resource "datadog_logs_index" "sample_index" { + name = "%s" + daily_limit = 20000 + disable_daily_limit = false + retention_days = 15 + filter { + query = "test:query" + } + exclusion_filter { + name = "Filter coredns logs" + is_enabled = true + filter { + query = "app:coredns" + sample_rate = 0.97 + } + } +} +`, name) +} + +func testAccCheckDatadogUpdateLogsIndexDisableDailyLimitConfig(name string) string { + return fmt.Sprintf(` +resource "datadog_logs_index" "sample_index" { + name = "%s" + daily_limit = 20000 + disable_daily_limit = true + retention_days = 15 + filter { + query = "test:query" + } +} +`, name) +} diff --git a/docs/resources/logs_index.md b/docs/resources/logs_index.md index cc40451cba..ca4e8501ec 100644 --- a/docs/resources/logs_index.md +++ b/docs/resources/logs_index.md @@ -3,17 +3,18 @@ page_title: "datadog_logs_index Resource - terraform-provider-datadog" subcategory: "" description: |- - Provides a Datadog Logs Index API resource. This can be used to create and manage Datadog logs indexes. + Provides a Datadog Logs Index API resource. This can be used to create and manage Datadog logs indexes.Note: It is not possible to delete logs indexes through Terraform, so an index remains in your account after the resource is removed from your terraform config. Reach out to support to delete a logs index. --- # datadog_logs_index (Resource) -Provides a Datadog Logs Index API resource. This can be used to create and manage Datadog logs indexes. +Provides a Datadog Logs Index API resource. This can be used to create and manage Datadog logs indexes. +**Note:** It is not possible to delete logs indexes through Terraform, so an index remains in your account after the resource is removed from your terraform config. Reach out to support to delete a logs index. ## Example Usage ```terraform -# A sample Datadog logs index resource definition. Note that at this point, it is not possible to create new logs indexes through Terraform, so the name field must match a name of an already existing index. If you want to keep the current state of the index, we suggest importing it (see below). +# A sample Datadog logs index resource definition. resource "datadog_logs_index" "sample_index" { name = "your index" diff --git a/examples/resources/datadog_logs_index/resource.tf b/examples/resources/datadog_logs_index/resource.tf index 2ef0fcf692..1996dfc16a 100644 --- a/examples/resources/datadog_logs_index/resource.tf +++ b/examples/resources/datadog_logs_index/resource.tf @@ -1,4 +1,4 @@ -# A sample Datadog logs index resource definition. Note that at this point, it is not possible to create new logs indexes through Terraform, so the name field must match a name of an already existing index. If you want to keep the current state of the index, we suggest importing it (see below). +# A sample Datadog logs index resource definition. resource "datadog_logs_index" "sample_index" { name = "your index"