From 651ff2f230004554e952ff444c73f7e763eafe22 Mon Sep 17 00:00:00 2001 From: skarimo <40482491+skarimo@users.noreply.github.com> Date: Fri, 16 Jun 2023 13:22:59 -0400 Subject: [PATCH] [datadog_user] Add support for `exact_match` in data source (#1968) * add support for exact match * fix docs --- datadog/data_source_datadog_user.go | 34 ++- ...DatadogUserDatasourceWithExactMatch.freeze | 1 + ...ccDatadogUserDatasourceWithExactMatch.yaml | 264 ++++++++++++++++++ ...ogUserDatasourceWithExactMatchError.freeze | 1 + ...adogUserDatasourceWithExactMatchError.yaml | 162 +++++++++++ .../tests/data_source_datadog_user_test.go | 70 ++++- docs/data-sources/user.md | 10 + .../{datadog_user.tf => data-source.tf} | 0 8 files changed, 533 insertions(+), 9 deletions(-) create mode 100644 datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.freeze create mode 100644 datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.yaml create mode 100644 datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.freeze create mode 100644 datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.yaml rename examples/data-sources/datadog_user/{datadog_user.tf => data-source.tf} (100%) diff --git a/datadog/data_source_datadog_user.go b/datadog/data_source_datadog_user.go index d6a96c578..35acb9435 100644 --- a/datadog/data_source_datadog_user.go +++ b/datadog/data_source_datadog_user.go @@ -21,6 +21,12 @@ func dataSourceDatadogUser() *schema.Resource { Type: schema.TypeString, Required: true, }, + "exact_match": { + Description: "When true, `filter` string is exact matched againts the users `email`, followed by `name` attribute.", + Type: schema.TypeBool, + Default: false, + Optional: true, + }, // Computed values "email": { Description: "Email of the user.", @@ -41,6 +47,7 @@ func dataSourceDatadogUserRead(ctx context.Context, d *schema.ResourceData, meta apiInstances := providerConf.DatadogApiInstances auth := providerConf.Auth filter := d.Get("filter").(string) // string | Filter all users by the given string. Defaults to no filtering. (optional) // string | Filter on status attribute. Comma separated list, with possible values `Active`, `Pending`, and `Disabled`. Defaults to no filtering. (optional) + exactMatch := d.Get("exact_match").(bool) optionalParams := datadogV2.ListUsersOptionalParameters{ Filter: &filter, } @@ -53,14 +60,39 @@ func dataSourceDatadogUserRead(ctx context.Context, d *schema.ResourceData, meta return diag.FromErr(err) } users := res.GetData() - if len(users) > 1 { + if len(users) > 1 && !exactMatch { return diag.Errorf("your query returned more than one result for filter \"%s\", please try a more specific search criteria", filter, ) } else if len(users) == 0 { return diag.Errorf("didn't find any user matching filter string \"%s\"", filter) } + matchedUser := users[0] + if exactMatch { + matchCount := 0 + for _, user := range users { + if user.Attributes.GetEmail() == filter { + matchedUser = user + matchCount++ + continue + } + if user.Attributes.GetName() == filter { + matchedUser = user + matchCount++ + continue + } + } + if matchCount > 1 { + return diag.Errorf("your query returned more than one result for filter with exact match \"%s\", please try a more specific search criteria", + filter, + ) + } + if matchCount == 0 { + return diag.Errorf("didn't find any user matching filter string with exact match \"%s\"", filter) + } + } + d.SetId(matchedUser.GetId()) if err := d.Set("name", matchedUser.Attributes.GetName()); err != nil { return diag.FromErr(err) diff --git a/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.freeze b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.freeze new file mode 100644 index 000000000..91f0cfc65 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.freeze @@ -0,0 +1 @@ +2023-06-15T16:12:30.013645-04:00 \ No newline at end of file diff --git a/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.yaml b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.yaml new file mode 100644 index 000000000..10156f566 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatch.yaml @@ -0,0 +1,264 @@ +--- +version: 1 +interactions: +- request: + body: | + {"data":{"attributes":{"email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com"},"relationships":{"roles":{"data":[]}},"type":"users"}} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/users + method: POST + response: + body: | + {"data":{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:31.726355+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: | + {"data":{"attributes":{"email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com"},"relationships":{"roles":{"data":[]}},"type":"users"}} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/users + method: POST + response: + body: | + {"data":{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:31.733418+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: | + {"data":[{"relationships":{"user":{"data":{"id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","type":"users"}}},"type":"user_invitations"}]} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/user_invitations + method: POST + response: + body: | + {"data":[{"type":"user_invitations","id":"f5c3c2fc-0bb8-11ee-8a3f-da7ad0900002","attributes":{"uuid":"f5c3c2fc-0bb8-11ee-8a3f-da7ad0900002","login_method":null,"invite_type":"basic_invite","created_at":"2023-06-15T20:12:32.019091+00:00","expires_at":"2023-06-17T20:12:31.933187+00:00"},"relationships":{"user":{"data":{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7"}}}}]} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: | + {"data":[{"relationships":{"user":{"data":{"id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","type":"users"}}},"type":"user_invitations"}]} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/user_invitations + method: POST + response: + body: | + {"data":[{"type":"user_invitations","id":"f5d27edc-0bb8-11ee-a33e-da7ad0900002","attributes":{"uuid":"f5d27edc-0bb8-11ee-a33e-da7ad0900002","login_method":null,"invite_type":"basic_invite","created_at":"2023-06-15T20:12:32.116193+00:00","expires_at":"2023-06-17T20:12:31.927775+00:00"},"relationships":{"user":{"data":{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1"}}}}]} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users?filter=tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950%40example.com + method: GET + response: + body: | + {"data":[{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:31.733418+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}},{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:31.726355+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}],"meta":{"page":{"total_count":175482,"total_filtered_count":2}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users?filter=tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950%40example.com + method: GET + response: + body: | + {"data":[{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:31.733418+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}},{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:31.726355+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}],"meta":{"page":{"total_count":175482,"total_filtered_count":2}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/f5968a75-0bb8-11ee-872b-928a47bbfdc7 + method: GET + response: + body: | + {"data":{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:31.733418+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/f595a029-0bb8-11ee-9e5c-fada1df30af1 + method: GET + response: + body: | + {"data":{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:31.726355+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users?filter=tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950%40example.com + method: GET + response: + body: | + {"data":[{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:31.733418+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}},{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:31.726355+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}],"meta":{"page":{"total_count":175482,"total_filtered_count":2}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users?filter=tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950%40example.com + method: GET + response: + body: | + {"data":[{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:31.733418+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}},{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:31.726355+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}],"meta":{"page":{"total_count":175482,"total_filtered_count":2}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - '*/*' + url: https://api.datadoghq.com/api/v2/users/f595a029-0bb8-11ee-9e5c-fada1df30af1 + method: DELETE + response: + body: "" + headers: {} + status: 204 No Content + code: 204 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - '*/*' + url: https://api.datadoghq.com/api/v2/users/f5968a75-0bb8-11ee-872b-928a47bbfdc7 + method: DELETE + response: + body: "" + headers: {} + status: 204 No Content + code: 204 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/f595a029-0bb8-11ee-9e5c-fada1df30af1 + method: GET + response: + body: | + {"data":{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:55.707906+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":true,"allowed_login_methods":[],"status":"Disabled"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/f5968a75-0bb8-11ee-872b-928a47bbfdc7 + method: GET + response: + body: | + {"data":{"type":"users","id":"f5968a75-0bb8-11ee-872b-928a47bbfdc7","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.725437+00:00","modified_at":"2023-06-15T20:12:55.714858+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/f6f65245d5386d40616c5100481410a9?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":true,"allowed_login_methods":[],"status":"Disabled"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/f595a029-0bb8-11ee-9e5c-fada1df30af1 + method: GET + response: + body: | + {"data":{"type":"users","id":"f595a029-0bb8-11ee-9e5c-fada1df30af1","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","created_at":"2023-06-15T20:12:31.718742+00:00","modified_at":"2023-06-15T20:12:55.707906+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatch-local-1686859950@example.com","icon":"https://secure.gravatar.com/avatar/6c751c8fee162e213b2336bc467d39d4?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":true,"allowed_login_methods":[],"status":"Disabled"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" diff --git a/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.freeze b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.freeze new file mode 100644 index 000000000..134d5d0c5 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.freeze @@ -0,0 +1 @@ +2023-06-15T16:14:26.293469-04:00 \ No newline at end of file diff --git a/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.yaml b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.yaml new file mode 100644 index 000000000..0c6cfeba3 --- /dev/null +++ b/datadog/tests/cassettes/TestAccDatadogUserDatasourceWithExactMatchError.yaml @@ -0,0 +1,162 @@ +--- +version: 1 +interactions: +- request: + body: | + {"data":{"attributes":{"email":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com"},"relationships":{"roles":{"data":[]}},"type":"users"}} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/users + method: POST + response: + body: | + {"data":{"type":"users","id":"3ab23d20-0bb9-11ee-bbf8-f23132112677","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","created_at":"2023-06-15T20:14:27.668685+00:00","modified_at":"2023-06-15T20:14:27.670771+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","icon":"https://secure.gravatar.com/avatar/4b98fced45ce7e33e9b2ff6a0fcc420f?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: | + {"data":{"attributes":{"email":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com"},"relationships":{"roles":{"data":[]}},"type":"users"}} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/users + method: POST + response: + body: | + {"data":{"type":"users","id":"3ab78abe-0bb9-11ee-bbf8-f23132112677","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","created_at":"2023-06-15T20:14:27.703650+00:00","modified_at":"2023-06-15T20:14:27.708243+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","icon":"https://secure.gravatar.com/avatar/37cbde901c4ff41f71273bfa9ac34c15?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: | + {"data":[{"relationships":{"user":{"data":{"id":"3ab23d20-0bb9-11ee-bbf8-f23132112677","type":"users"}}},"type":"user_invitations"}]} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/user_invitations + method: POST + response: + body: | + {"data":[{"type":"user_invitations","id":"3adc6fba-0bb9-11ee-93a7-da7ad0900002","attributes":{"uuid":"3adc6fba-0bb9-11ee-93a7-da7ad0900002","login_method":null,"invite_type":"basic_invite","created_at":"2023-06-15T20:14:27.943629+00:00","expires_at":"2023-06-17T20:14:27.863220+00:00"},"relationships":{"user":{"data":{"type":"users","id":"3ab23d20-0bb9-11ee-bbf8-f23132112677"}}}}]} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: | + {"data":[{"relationships":{"user":{"data":{"id":"3ab78abe-0bb9-11ee-bbf8-f23132112677","type":"users"}}},"type":"user_invitations"}]} + form: {} + headers: + Accept: + - application/json + Content-Type: + - application/json + url: https://api.datadoghq.com/api/v2/user_invitations + method: POST + response: + body: | + {"data":[{"type":"user_invitations","id":"3adf13b4-0bb9-11ee-a6cc-da7ad0900002","attributes":{"uuid":"3adf13b4-0bb9-11ee-a6cc-da7ad0900002","login_method":null,"invite_type":"basic_invite","created_at":"2023-06-15T20:14:27.961604+00:00","expires_at":"2023-06-17T20:14:27.857120+00:00"},"relationships":{"user":{"data":{"type":"users","id":"3ab78abe-0bb9-11ee-bbf8-f23132112677"}}}}]} + headers: + Content-Type: + - application/json + status: 201 Created + code: 201 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users?filter=tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066%40example.com + method: GET + response: + body: | + {"data":[{"type":"users","id":"3ab23d20-0bb9-11ee-bbf8-f23132112677","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","created_at":"2023-06-15T20:14:27.668685+00:00","modified_at":"2023-06-15T20:14:27.670771+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","icon":"https://secure.gravatar.com/avatar/4b98fced45ce7e33e9b2ff6a0fcc420f?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}},{"type":"users","id":"3ab78abe-0bb9-11ee-bbf8-f23132112677","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","created_at":"2023-06-15T20:14:27.703650+00:00","modified_at":"2023-06-15T20:14:27.708243+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","icon":"https://secure.gravatar.com/avatar/37cbde901c4ff41f71273bfa9ac34c15?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":false,"allowed_login_methods":[],"status":"Pending"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}],"meta":{"page":{"total_count":175486,"total_filtered_count":2}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - '*/*' + url: https://api.datadoghq.com/api/v2/users/3ab23d20-0bb9-11ee-bbf8-f23132112677 + method: DELETE + response: + body: "" + headers: {} + status: 204 No Content + code: 204 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - '*/*' + url: https://api.datadoghq.com/api/v2/users/3ab78abe-0bb9-11ee-bbf8-f23132112677 + method: DELETE + response: + body: "" + headers: {} + status: 204 No Content + code: 204 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/3ab23d20-0bb9-11ee-bbf8-f23132112677 + method: GET + response: + body: | + {"data":{"type":"users","id":"3ab23d20-0bb9-11ee-bbf8-f23132112677","attributes":{"name":null,"handle":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","created_at":"2023-06-15T20:14:27.668685+00:00","modified_at":"2023-06-15T20:14:34.176472+00:00","email":"othertf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","icon":"https://secure.gravatar.com/avatar/4b98fced45ce7e33e9b2ff6a0fcc420f?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":true,"allowed_login_methods":[],"status":"Disabled"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Accept: + - application/json + url: https://api.datadoghq.com/api/v2/users/3ab78abe-0bb9-11ee-bbf8-f23132112677 + method: GET + response: + body: | + {"data":{"type":"users","id":"3ab78abe-0bb9-11ee-bbf8-f23132112677","attributes":{"name":null,"handle":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","created_at":"2023-06-15T20:14:27.703650+00:00","modified_at":"2023-06-15T20:14:34.250169+00:00","email":"tf-testaccdatadoguserdatasourcewithexactmatcherror-local-1686860066@example.com","icon":"https://secure.gravatar.com/avatar/37cbde901c4ff41f71273bfa9ac34c15?s=48&d=retro","title":null,"verified":false,"service_account":false,"disabled":true,"allowed_login_methods":[],"status":"Disabled"},"relationships":{"roles":{"data":[]},"org":{"data":{"type":"orgs","id":"4dee724d-00cc-11ea-a77b-570c9d03c6c5"}}}}} + headers: + Content-Type: + - application/json + status: 200 OK + code: 200 + duration: "" diff --git a/datadog/tests/data_source_datadog_user_test.go b/datadog/tests/data_source_datadog_user_test.go index e6e61bf81..1565aa97b 100644 --- a/datadog/tests/data_source_datadog_user_test.go +++ b/datadog/tests/data_source_datadog_user_test.go @@ -45,17 +45,71 @@ func TestAccDatadogUserDatasourceError(t *testing.T) { }) } +func TestAccDatadogUserDatasourceWithExactMatch(t *testing.T) { + t.Parallel() + ctx, accProviders := testAccProviders(context.Background(), t) + email := strings.ToLower(uniqueEntityName(ctx, t)) + "@example.com" + accProvider := testAccProvider(t, accProviders) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: accProviders, + CheckDestroy: testAccCheckDatadogUserV2Destroy(accProvider), + Steps: []resource.TestStep{ + { + Config: testAccDatasourceUserWithExactMatchConfig(email, "true"), + Check: resource.TestCheckResourceAttr("data.datadog_user.test", "email", email), + }, + }, + }) +} + +func TestAccDatadogUserDatasourceWithExactMatchError(t *testing.T) { + t.Parallel() + ctx, accProviders := testAccProviders(context.Background(), t) + email := strings.ToLower(uniqueEntityName(ctx, t)) + "@example.com" + accProvider := testAccProvider(t, accProviders) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: accProviders, + CheckDestroy: testAccCheckDatadogUserV2Destroy(accProvider), + Steps: []resource.TestStep{ + { + Config: testAccDatasourceUserWithExactMatchConfig(email, "false"), + ExpectError: regexp.MustCompile("your query returned more than one result for filter"), + }, + }, + }) +} + +func testAccDatasourceUserWithExactMatchConfig(uniq, exactMatch string) string { + return fmt.Sprintf(` +data "datadog_user" "test" { + filter = "%[1]s" + exact_match = %[2]s + depends_on = [ datadog_user.foo, datadog_user.bar ] +} + +resource "datadog_user" "foo" { + email = "%[1]s" +} +resource "datadog_user" "bar" { + email = "other%[1]s" +}`, uniq, exactMatch) +} + func testAccDatasourceUserConfig(uniq string) string { return fmt.Sprintf(` - data "datadog_user" "test" { - filter = "%s" - depends_on = [ - datadog_user.foo - ] - } - resource "datadog_user" "foo" { +data "datadog_user" "test" { + filter = "%s" + depends_on = [ + datadog_user.foo + ] +} +resource "datadog_user" "foo" { email = "%s" - }`, uniq, uniq) +}`, uniq, uniq) } func testAccDatasourceUserError() string { diff --git a/docs/data-sources/user.md b/docs/data-sources/user.md index c2e20b082..a14ad02a9 100644 --- a/docs/data-sources/user.md +++ b/docs/data-sources/user.md @@ -10,7 +10,13 @@ description: |- Use this data source to retrieve information about an existing user to use it in an other resources. +## Example Usage +```terraform +data "datadog_user" "test" { + filter = "user.name@company.com" +} +``` ## Schema @@ -19,6 +25,10 @@ Use this data source to retrieve information about an existing user to use it in - `filter` (String) Filter all users by the given string. +### Optional + +- `exact_match` (Boolean) When true, `filter` string is exact matched againts the users `email`, followed by `name` attribute. + ### Read-Only - `email` (String) Email of the user. diff --git a/examples/data-sources/datadog_user/datadog_user.tf b/examples/data-sources/datadog_user/data-source.tf similarity index 100% rename from examples/data-sources/datadog_user/datadog_user.tf rename to examples/data-sources/datadog_user/data-source.tf