-
Notifications
You must be signed in to change notification settings - Fork 384
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
[datadog_apm_retention_filter] Support updating default retention filters #2370
Draft
SalahEddineBC
wants to merge
5
commits into
master
Choose a base branch
from
salah.bachircherif/update-retention-filters-resource
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c6d657
Support updating default retention filters
SalahEddineBC f81418d
bump client version
SalahEddineBC de7de2c
bump client version
SalahEddineBC 41a825d
bump client version
SalahEddineBC e20a9db
bump client version to latest
SalahEddineBC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils" | ||
|
@@ -76,7 +77,7 @@ func (r *ApmRetentionFilterResource) Schema(_ context.Context, _ resource.Schema | |
"filter_type": schema.StringAttribute{ | ||
Description: "The type of the retention filter, currently only spans-processing-sampling is available.", | ||
Required: true, | ||
Validators: []validator.String{validators.NewEnumValidator[validator.String](datadogV2.NewRetentionFilterTypeFromValue)}, | ||
Validators: []validator.String{validators.NewEnumValidator[validator.String](datadogV2.NewRetentionFilterAllTypeFromValue)}, | ||
}, | ||
"rate": schema.StringAttribute{ | ||
Description: "Sample rate to apply to spans going through this retention filter as a string, a value of 1.0 keeps all spans matching the query.", | ||
|
@@ -132,12 +133,73 @@ func (r *ApmRetentionFilterResource) Read(ctx context.Context, request resource. | |
response.Diagnostics.Append(response.State.Set(ctx, &state)...) | ||
} | ||
|
||
type CommonRequest struct { | ||
Diagnostics diag.Diagnostics | ||
State *tfsdk.State | ||
} | ||
|
||
func NewCommonRequest(diag diag.Diagnostics, state *tfsdk.State) CommonRequest { | ||
return CommonRequest{ | ||
Diagnostics: diag, | ||
State: state, | ||
} | ||
} | ||
|
||
func (r *ApmRetentionFilterResource) getAndUpdate(state *ApmRetentionFilterModel, ctx context.Context, response CommonRequest) { | ||
resp, _, err := r.Api.ListApmRetentionFilters(r.Auth) | ||
if err != nil { | ||
return | ||
} | ||
if err := utils.CheckForUnparsed(resp); err != nil { | ||
return | ||
} | ||
var id string | ||
var filterName string | ||
for _, rfa := range resp.Data { | ||
if string(rfa.Attributes.GetFilterType()) == state.FilterType.ValueString() { | ||
state.ID = types.StringValue(rfa.Id) | ||
id = rfa.Id | ||
filterName = rfa.Attributes.GetName() | ||
break | ||
} | ||
} | ||
|
||
body, diags := r.buildApmRetentionFilterUpdateRequestBody(ctx, state) | ||
body.Data.Attributes.SetName(filterName) | ||
|
||
response.Diagnostics.Append(diags...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
apmRetentionFilterMutex.Lock() | ||
defer apmRetentionFilterMutex.Unlock() | ||
|
||
respUpdate, _, err := r.Api.UpdateApmRetentionFilter(r.Auth, id, *body) | ||
if err != nil { | ||
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error dd retention filter")) | ||
return | ||
} | ||
if err := utils.CheckForUnparsed(resp); err != nil { | ||
response.Diagnostics.AddError("response contains unparsedObject", err.Error()) | ||
return | ||
} | ||
r.updateState(ctx, state, &respUpdate) | ||
|
||
// Save data into Terraform state | ||
response.Diagnostics.Append(response.State.Set(ctx, &state)...) | ||
} | ||
|
||
func (r *ApmRetentionFilterResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { | ||
var state ApmRetentionFilterModel | ||
response.Diagnostics.Append(request.Plan.Get(ctx, &state)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
if state.FilterType.ValueString() != "spans-sampling-processor" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can default retention rules not have type
|
||
r.getAndUpdate(&state, ctx, NewCommonRequest(response.Diagnostics, &response.State)) | ||
return | ||
} | ||
|
||
body, diags := r.buildRetentionFilterCreateRequestBody(ctx, &state) | ||
response.Diagnostics.Append(diags...) | ||
|
@@ -150,14 +212,14 @@ func (r *ApmRetentionFilterResource) Create(ctx context.Context, request resourc | |
|
||
resp, _, err := r.Api.CreateApmRetentionFilter(r.Auth, *body) | ||
if err != nil { | ||
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error retrieving retention filter")) | ||
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error creating retention filter")) | ||
return | ||
} | ||
if err := utils.CheckForUnparsed(resp); err != nil { | ||
response.Diagnostics.AddError("response contains unparsedObject", err.Error()) | ||
return | ||
} | ||
r.updateState(ctx, &state, &resp) | ||
r.updateStateForCreate(ctx, &state, &resp) | ||
|
||
// Save data into Terraform state | ||
response.Diagnostics.Append(response.State.Set(ctx, &state)...) | ||
|
@@ -170,6 +232,11 @@ func (r *ApmRetentionFilterResource) Update(ctx context.Context, request resourc | |
return | ||
} | ||
|
||
if state.FilterType.ValueString() != "spans-sampling-processor" { | ||
r.getAndUpdate(&state, ctx, NewCommonRequest(response.Diagnostics, &response.State)) | ||
return | ||
} | ||
|
||
id := state.ID.ValueString() | ||
|
||
body, diags := r.buildApmRetentionFilterUpdateRequestBody(ctx, &state) | ||
|
@@ -183,7 +250,7 @@ func (r *ApmRetentionFilterResource) Update(ctx context.Context, request resourc | |
|
||
resp, _, err := r.Api.UpdateApmRetentionFilter(r.Auth, id, *body) | ||
if err != nil { | ||
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error retrieving retention filter")) | ||
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error updating retention filter")) | ||
return | ||
} | ||
if err := utils.CheckForUnparsed(resp); err != nil { | ||
|
@@ -203,6 +270,10 @@ func (r *ApmRetentionFilterResource) Delete(ctx context.Context, request resourc | |
return | ||
} | ||
|
||
// Default filters cannot be deleted, skip the deletion | ||
if state.FilterType.ValueString() != "spans-sampling-processor" { | ||
return | ||
} | ||
id := state.ID.ValueString() | ||
|
||
apmRetentionFilterMutex.Lock() | ||
|
@@ -219,6 +290,30 @@ func (r *ApmRetentionFilterResource) Delete(ctx context.Context, request resourc | |
} | ||
|
||
func (r *ApmRetentionFilterResource) updateState(ctx context.Context, state *ApmRetentionFilterModel, resp *datadogV2.RetentionFilterResponse) { | ||
state.ID = types.StringValue(resp.Data.GetId()) | ||
// Ignore the name if it is a default filter, since it is not editable | ||
if *resp.Data.Attributes.FilterType == datadogV2.RETENTIONFILTERALLTYPE_SPANS_SAMPLING_PROCESSOR { | ||
state.Name = types.StringValue(resp.Data.Attributes.GetName()) | ||
} | ||
// Make sure we maintain the same precision as config | ||
// Otherwise we will run into inconsistent state errors | ||
configVal := state.Rate.ValueString() | ||
precision := -1 | ||
if i := strings.IndexByte(configVal, '.'); i > -1 { | ||
precision = len(configVal) - i - 1 | ||
} | ||
state.Rate = types.StringValue(strconv.FormatFloat(resp.Data.Attributes.GetRate(), 'f', precision, 64)) | ||
|
||
if state.Filter == nil { | ||
filter := retentionFilterModel{} | ||
state.Filter = &filter | ||
} | ||
state.Filter.Query = types.StringValue(*resp.Data.Attributes.GetFilter().Query) | ||
state.Enabled = types.BoolValue(*resp.Data.Attributes.Enabled) | ||
state.FilterType = types.StringValue(string(resp.Data.Attributes.GetFilterType())) | ||
} | ||
|
||
func (r *ApmRetentionFilterResource) updateStateForCreate(ctx context.Context, state *ApmRetentionFilterModel, resp *datadogV2.RetentionFilterCreateResponse) { | ||
state.ID = types.StringValue(resp.Data.GetId()) | ||
state.Name = types.StringValue(resp.Data.Attributes.GetName()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2024-01-16T15:28:02.191729-05:00 | ||
2024-04-18T16:05:07.003325+02:00 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we matching my filter type here? Can't we have multiple default pipelines with the same type?