Skip to content

Commit

Permalink
[datadog_service_account] Implement exact match filtering (#2447)
Browse files Browse the repository at this point in the history
* add exact match

* make docs
  • Loading branch information
retsguj authored Jun 20, 2024
1 parent 9928ec0 commit 15ec09e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
34 changes: 32 additions & 2 deletions datadog/fwprovider/data_source_datadog_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type datadogServiceAccountDatasourceModel struct {
ID types.String `tfsdk:"id"`
Filter types.String `tfsdk:"filter"`
FilterStatus types.String `tfsdk:"filter_status"`
ExactMatch types.Bool `tfsdk:"exact_match"`
// Results
Disabled types.Bool `tfsdk:"disabled"`
Email types.String `tfsdk:"email"`
Expand Down Expand Up @@ -69,6 +70,10 @@ func (d *datadogServiceAccountDatasource) Schema(_ context.Context, _ datasource
Description: "Filter on status attribute. Comma separated list, with possible values `Active`, `Pending`, and `Disabled`.",
Optional: true,
},
"exact_match": schema.BoolAttribute{
Description: "When true, `filter` string is exact matched against the user's `email`, followed by `name` attribute.",
Optional: true,
},
// Computed values
"disabled": schema.BoolAttribute{
Computed: true,
Expand Down Expand Up @@ -133,7 +138,8 @@ func (d *datadogServiceAccountDatasource) Read(ctx context.Context, req datasour
userData = ddResp.Data
} else {
optionalParams := datadogV2.ListUsersOptionalParameters{}
optionalParams.WithFilter(state.Filter.ValueString())
filter := state.Filter.ValueString()
optionalParams.WithFilter(filter)
if !state.FilterStatus.IsNull() {
optionalParams.WithFilterStatus(state.FilterStatus.ValueString())
}
Expand All @@ -151,7 +157,8 @@ func (d *datadogServiceAccountDatasource) Read(ctx context.Context, req datasour
serviceAccounts = append(serviceAccounts, user)
}
}
if len(serviceAccounts) > 1 {
isExactMatch := state.ExactMatch.ValueBool()
if len(serviceAccounts) > 1 && !isExactMatch {
resp.Diagnostics.AddError("filter keyword returned more than one result, use more specific search criteria", "")
return
}
Expand All @@ -160,6 +167,29 @@ func (d *datadogServiceAccountDatasource) Read(ctx context.Context, req datasour
return
}
userData = &serviceAccounts[0]
if isExactMatch {
matchCount := 0
for _, serviceAccount := range serviceAccounts {
if *serviceAccount.GetAttributes().Email == filter {
userData = &serviceAccount
matchCount++
continue
}
if *serviceAccount.GetAttributes().Name.Get() == filter {
userData = &serviceAccount
matchCount++
continue
}
}
if matchCount > 1 {
resp.Diagnostics.AddError("your query returned more than one result for filter with exact match, please try a more specific search criteria", "")
return
}
if matchCount == 0 {
resp.Diagnostics.AddError("didn't find any service account matching filter string with exact match", "")
return
}
}
}
d.updateState(ctx, &state, userData)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/service_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Use this data source to retrieve information about an existing Datadog service a

### Optional

- `exact_match` (Boolean) When true, `filter` string is exact matched against the user's `email`, followed by `name` attribute.
- `filter` (String) Filter all users and service accounts by name, email, or role.
- `filter_status` (String) Filter on status attribute. Comma separated list, with possible values `Active`, `Pending`, and `Disabled`.
- `id` (String) The service account's ID.
Expand Down

0 comments on commit 15ec09e

Please sign in to comment.