Skip to content
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

Add logs index creation #1155

Merged
merged 6 commits into from
Jul 27, 2021
Merged
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
5 changes: 5 additions & 0 deletions datadog/internal/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"os"
"time"
)

Expand All @@ -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())
Expand Down
51 changes: 33 additions & 18 deletions datadog/resource_datadog_logs_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package datadog
import (
"context"
"log"
"strings"

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

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -162,31 +166,24 @@ 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)
}

func resourceDatadogLogsIndexDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
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{})))
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2021-07-26T15:13:21.354859+02:00
Loading