From dd1831a3d0cfaed3de4227c538c792e93f9c8d34 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Fri, 7 Oct 2022 15:48:26 +0200 Subject: [PATCH] Fix small issue with log stream filters --- .../provider/resource_auth0_log_stream.go | 23 +- .../resource_auth0_log_stream_test.go | 27 ++- .../data/recordings/TestAccLogStreamSumo.yaml | 220 +++++++++++++++--- 3 files changed, 218 insertions(+), 52 deletions(-) diff --git a/internal/provider/resource_auth0_log_stream.go b/internal/provider/resource_auth0_log_stream.go index f92903573..38310e849 100644 --- a/internal/provider/resource_auth0_log_stream.go +++ b/internal/provider/resource_auth0_log_stream.go @@ -235,12 +235,12 @@ func createLogStream(ctx context.Context, d *schema.ResourceData, m interface{}) d.SetId(logStream.GetID()) - // The Management API only allows updating a log stream's status. Therefore, - // if the status field was present in the configuration, we perform an - // additional operation to modify it. - status := String(d, "status") - if status != nil && *status != logStream.GetStatus() { - if err := api.LogStream.Update(logStream.GetID(), &management.LogStream{Status: status}); err != nil { + // The Management API only allows updating a log stream's status. + // Therefore, if the status field was present in the configuration, + // we perform an additional operation to modify it. + status := d.Get("status").(string) + if status != "" && status != logStream.GetStatus() { + if err := api.LogStream.Update(logStream.GetID(), &management.LogStream{Status: &status}); err != nil { return diag.FromErr(err) } } @@ -369,12 +369,11 @@ func flattenLogStreamSinkSumo(o *management.LogStreamSinkSumo) interface{} { func expandLogStream(d *schema.ResourceData) *management.LogStream { config := d.GetRawConfig() - logStreamType := value.String(config.GetAttr("type")) - logStream := &management.LogStream{ Name: value.String(config.GetAttr("name")), } + logStreamType := value.String(config.GetAttr("type")) if d.IsNewResource() { logStream.Type = logStreamType } @@ -385,16 +384,14 @@ func expandLogStream(d *schema.ResourceData) *management.LogStream { filtersConfig := config.GetAttr("filters") if !filtersConfig.IsNull() { - var filters []map[string]string + filters := make([]map[string]string, 0) filtersConfig.ForEachElement(func(_ cty.Value, filter cty.Value) (stop bool) { filters = append(filters, *value.MapOfStrings(filter)) - return true + return stop }) - if len(filters) > 0 { - logStream.Filters = &filters - } + logStream.Filters = &filters } config.GetAttr("sink").ForEachElement(func(_ cty.Value, sink cty.Value) (stop bool) { diff --git a/internal/provider/resource_auth0_log_stream_test.go b/internal/provider/resource_auth0_log_stream_test.go index da8d50d73..fbf4161e8 100644 --- a/internal/provider/resource_auth0_log_stream_test.go +++ b/internal/provider/resource_auth0_log_stream_test.go @@ -542,6 +542,15 @@ func TestAccLogStreamSumo(t *testing.T) { resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.sumo_source_address", "prod.sumo.com"), ), }, + { + Config: template.ParseTestName(logStreamSumoConfigUpdateWithEmptyFilters, t.Name()), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "name", fmt.Sprintf("Acceptance-Test-LogStream-sumo-%s", t.Name())), + resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "type", "sumo"), + resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.#", "0"), + resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.sumo_source_address", "prod.sumo.com"), + ), + }, }, }) } @@ -564,10 +573,12 @@ resource "auth0_log_stream" "my_log_stream" { } } ` + const logStreamSumoConfigUpdateWithFilters = ` resource "auth0_log_stream" "my_log_stream" { name = "Acceptance-Test-LogStream-sumo-{{.testName}}" type = "sumo" + filters = [ { type = "category" @@ -578,8 +589,22 @@ resource "auth0_log_stream" "my_log_stream" { name = "auth.signup.fail" } ] + sink { - sumo_source_address = "prod.sumo.com" + sumo_source_address = "prod.sumo.com" + } +} +` + +const logStreamSumoConfigUpdateWithEmptyFilters = ` +resource "auth0_log_stream" "my_log_stream" { + name = "Acceptance-Test-LogStream-sumo-{{.testName}}" + type = "sumo" + + filters = [ ] + + sink { + sumo_source_address = "prod.sumo.com" } } ` diff --git a/test/data/recordings/TestAccLogStreamSumo.yaml b/test/data/recordings/TestAccLogStreamSumo.yaml index f4127d46f..e3d134d0c 100644 --- a/test/data/recordings/TestAccLogStreamSumo.yaml +++ b/test/data/recordings/TestAccLogStreamSumo.yaml @@ -19,7 +19,7 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 + - Go-Auth0-SDK/0.11.0 url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams method: POST response: @@ -30,7 +30,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -55,8 +55,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -66,7 +66,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -91,8 +91,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -102,7 +102,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -127,8 +127,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -138,7 +138,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"demo.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -150,21 +150,21 @@ interactions: proto: HTTP/1.1 proto_major: 1 proto_minor: 1 - content_length: 126 + content_length: 108 transfer_encoding: [ ] trailer: { } host: terraform-provider-auth0-dev.eu.auth0.com remote_addr: "" request_uri: "" body: | - {"name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}} + {"name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","sink":{"sumoSourceAddress":"prod.sumo.com"}} form: { } headers: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: PATCH response: proto: HTTP/2.0 @@ -174,7 +174,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -199,8 +199,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -210,7 +210,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -235,8 +235,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -246,7 +246,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -271,8 +271,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -282,7 +282,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' headers: Content-Type: - application/json; charset=utf-8 @@ -294,21 +294,21 @@ interactions: proto: HTTP/1.1 proto_major: 1 proto_minor: 1 - content_length: 229 + content_length: 211 transfer_encoding: [ ] trailer: { } host: terraform-provider-auth0-dev.eu.auth0.com remote_addr: "" request_uri: "" body: | - {"name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","status":"active","filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}],"sink":{"sumoSourceAddress":"prod.sumo.com"}} + {"name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}],"sink":{"sumoSourceAddress":"prod.sumo.com"}} form: { } headers: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: PATCH response: proto: HTTP/2.0 @@ -318,7 +318,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' headers: Content-Type: - application/json; charset=utf-8 @@ -343,8 +343,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -354,7 +354,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' headers: Content-Type: - application/json; charset=utf-8 @@ -379,8 +379,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: GET response: proto: HTTP/2.0 @@ -390,7 +390,7 @@ interactions: trailer: { } content_length: -1 uncompressed: true - body: '{"id":"lst_0000000000010036","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' headers: Content-Type: - application/json; charset=utf-8 @@ -398,6 +398,150 @@ interactions: code: 200 duration: 1ms - id: 11 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [ ] + trailer: { } + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: { } + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [ ] + trailer: { } + content_length: -1 + uncompressed: true + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"},"filters":[{"name":"auth.login.fail","type":"category"},{"name":"auth.signup.fail","type":"category"}]}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - id: 12 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 121 + transfer_encoding: [ ] + trailer: { } + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + {"name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","filters":[],"sink":{"sumoSourceAddress":"prod.sumo.com"}} + form: { } + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 + method: PATCH + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [ ] + trailer: { } + content_length: -1 + uncompressed: true + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - id: 13 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [ ] + trailer: { } + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: { } + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [ ] + trailer: { } + content_length: -1 + uncompressed: true + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - id: 14 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 5 + transfer_encoding: [ ] + trailer: { } + host: terraform-provider-auth0-dev.eu.auth0.com + remote_addr: "" + request_uri: "" + body: | + null + form: { } + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [ ] + trailer: { } + content_length: -1 + uncompressed: true + body: '{"id":"lst_0000000000010354","name":"Acceptance-Test-LogStream-sumo-TestAccLogStreamSumo","type":"sumo","status":"active","sink":{"sumoSourceAddress":"prod.sumo.com"}}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms + - id: 15 request: proto: HTTP/1.1 proto_major: 1 @@ -414,8 +558,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.10.0 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010036 + - Go-Auth0-SDK/0.11.0 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/log-streams/lst_0000000000010354 method: DELETE response: proto: HTTP/2.0