From 340c6908a98ced498eee023261220a682003d471 Mon Sep 17 00:00:00 2001 From: Shirley Leu <4163034+fridgepoet@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:50:20 +0100 Subject: [PATCH 1/5] Consider null and unknown as valid in url string validator --- internal/resources/connections/resources.go | 4 +++ .../resources/connections/resources_test.go | 25 +++++++------------ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/internal/resources/connections/resources.go b/internal/resources/connections/resources.go index f4732d612..9c36cf69e 100644 --- a/internal/resources/connections/resources.go +++ b/internal/resources/connections/resources.go @@ -32,6 +32,10 @@ func (v HTTPSURLValidator) MarkdownDescription(_ context.Context) string { } func (v HTTPSURLValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { + if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { + return + } + value := request.ConfigValue.ValueString() if value == "" { diff --git a/internal/resources/connections/resources_test.go b/internal/resources/connections/resources_test.go index 23ab61df9..7dca9ee5a 100644 --- a/internal/resources/connections/resources_test.go +++ b/internal/resources/connections/resources_test.go @@ -23,30 +23,23 @@ func Test_httpsURLValidator(t *testing.T) { providedURL: types.StringValue("https://dev.my-metrics-endpoint-url.com:9000/metrics"), expectedDiags: nil, }, - "invalid empty string": { - providedURL: types.StringValue(""), - expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic( - path.Root("test"), - "value must be valid URL with HTTPS", - "A valid URL is required.\n\nGiven Value: \"\"\n", - )}, + "null is considered valid": { + providedURL: types.StringNull(), + expectedDiags: diag.Diagnostics(nil), }, - "invalid null": { - providedURL: types.StringNull(), - expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic( - path.Root("test"), - "value must be valid URL with HTTPS", - "A valid URL is required.\n\nGiven Value: \"\"\n", - )}, + "unknown is considered valid": { + providedURL: types.StringUnknown(), + expectedDiags: diag.Diagnostics(nil), }, - "invalid unknown": { - providedURL: types.StringUnknown(), + "invalid empty string": { + providedURL: types.StringValue(""), expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic( path.Root("test"), "value must be valid URL with HTTPS", "A valid URL is required.\n\nGiven Value: \"\"\n", )}, }, + "invalid not a url": { providedURL: types.StringValue("this is not a url"), expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic( From fc1a2b23d781565675732fd869d432e483859fd9 Mon Sep 17 00:00:00 2001 From: Shirley Leu <4163034+fridgepoet@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:02:38 +0100 Subject: [PATCH 2/5] WIP test --- .../metrics_endpoint_scrape_job_test.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/resources/connections/metrics_endpoint_scrape_job_test.go b/internal/resources/connections/metrics_endpoint_scrape_job_test.go index 0b8159bef..ca6af441a 100644 --- a/internal/resources/connections/metrics_endpoint_scrape_job_test.go +++ b/internal/resources/connections/metrics_endpoint_scrape_job_test.go @@ -73,6 +73,12 @@ func TestAcc_MetricsEndpointScrapeJob(t *testing.T) { RefreshState: false, ExpectError: regexp.MustCompile(`These attributes must be configured together`), }, + { + Config: resourceWithForEach, + PlanOnly: true, + RefreshState: false, + ExpectNonEmptyPlan: true, + }, { // Creates a managed resource Config: testutils.TestAccExample(t, "resources/grafana_connections_metrics_endpoint_scrape_job/resource.tf"), @@ -130,3 +136,21 @@ resource "grafana_connections_metrics_endpoint_scrape_job" "test" { scrape_interval_seconds = 120 } ` + +var resourceWithForEach = ` +locals { + jobs = [ + { name = "test", url = "https://google.com" } + ] +} + +resource "grafana_connections_metrics_endpoint_scrape_job" "this" { + for_each = { for j in local.jobs : j.name => j.url } + stack_id = "......" + name = each.key + enabled = false + authentication_method = "bearer" + authentication_bearer_token = "test" + url = each.value +} +` From bfd8e5bbcdce1e4222000a0f215e3677a259876f Mon Sep 17 00:00:00 2001 From: Shirley Leu <4163034+fridgepoet@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:12:39 +0100 Subject: [PATCH 3/5] Add an invalid url test --- .../metrics_endpoint_scrape_job_test.go | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/resources/connections/metrics_endpoint_scrape_job_test.go b/internal/resources/connections/metrics_endpoint_scrape_job_test.go index ca6af441a..c9bd6a3d7 100644 --- a/internal/resources/connections/metrics_endpoint_scrape_job_test.go +++ b/internal/resources/connections/metrics_endpoint_scrape_job_test.go @@ -74,11 +74,17 @@ func TestAcc_MetricsEndpointScrapeJob(t *testing.T) { ExpectError: regexp.MustCompile(`These attributes must be configured together`), }, { - Config: resourceWithForEach, + Config: resourceWithForEachValidUrl, PlanOnly: true, RefreshState: false, ExpectNonEmptyPlan: true, }, + { + Config: resourceWithForEachInvalidUrl, + PlanOnly: true, + RefreshState: false, + ExpectError: regexp.MustCompile(`A valid URL is required`), + }, { // Creates a managed resource Config: testutils.TestAccExample(t, "resources/grafana_connections_metrics_endpoint_scrape_job/resource.tf"), @@ -137,14 +143,32 @@ resource "grafana_connections_metrics_endpoint_scrape_job" "test" { } ` -var resourceWithForEach = ` +var resourceWithForEachValidUrl = ` locals { jobs = [ { name = "test", url = "https://google.com" } ] } -resource "grafana_connections_metrics_endpoint_scrape_job" "this" { +resource "grafana_connections_metrics_endpoint_scrape_job" "valid_url" { + for_each = { for j in local.jobs : j.name => j.url } + stack_id = "......" + name = each.key + enabled = false + authentication_method = "bearer" + authentication_bearer_token = "test" + url = each.value +} +` + +var resourceWithForEachInvalidUrl = ` +locals { + jobs = [ + { name = "test", url = "" } + ] +} + +resource "grafana_connections_metrics_endpoint_scrape_job" "invalid_url" { for_each = { for j in local.jobs : j.name => j.url } stack_id = "......" name = each.key From e5e3eb356e1d2e4c99b3d862dee1889ffd5ca9af Mon Sep 17 00:00:00 2001 From: Shirley Leu <4163034+fridgepoet@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:13:28 +0100 Subject: [PATCH 4/5] Remove empty line --- internal/resources/connections/resources_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/resources/connections/resources_test.go b/internal/resources/connections/resources_test.go index 7dca9ee5a..2538633c7 100644 --- a/internal/resources/connections/resources_test.go +++ b/internal/resources/connections/resources_test.go @@ -39,7 +39,6 @@ func Test_httpsURLValidator(t *testing.T) { "A valid URL is required.\n\nGiven Value: \"\"\n", )}, }, - "invalid not a url": { providedURL: types.StringValue("this is not a url"), expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic( From ee02a3f9a88357bbf08f20175f8bf2a00d9c84f7 Mon Sep 17 00:00:00 2001 From: Shirley Leu <4163034+fridgepoet@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:49:30 +0100 Subject: [PATCH 5/5] Fix lint error --- .../connections/metrics_endpoint_scrape_job_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/resources/connections/metrics_endpoint_scrape_job_test.go b/internal/resources/connections/metrics_endpoint_scrape_job_test.go index c9bd6a3d7..a945f31b7 100644 --- a/internal/resources/connections/metrics_endpoint_scrape_job_test.go +++ b/internal/resources/connections/metrics_endpoint_scrape_job_test.go @@ -74,13 +74,13 @@ func TestAcc_MetricsEndpointScrapeJob(t *testing.T) { ExpectError: regexp.MustCompile(`These attributes must be configured together`), }, { - Config: resourceWithForEachValidUrl, + Config: resourceWithForEachValidURL, PlanOnly: true, RefreshState: false, ExpectNonEmptyPlan: true, }, { - Config: resourceWithForEachInvalidUrl, + Config: resourceWithForEachInvalidURL, PlanOnly: true, RefreshState: false, ExpectError: regexp.MustCompile(`A valid URL is required`), @@ -143,7 +143,7 @@ resource "grafana_connections_metrics_endpoint_scrape_job" "test" { } ` -var resourceWithForEachValidUrl = ` +var resourceWithForEachValidURL = ` locals { jobs = [ { name = "test", url = "https://google.com" } @@ -161,7 +161,7 @@ resource "grafana_connections_metrics_endpoint_scrape_job" "valid_url" { } ` -var resourceWithForEachInvalidUrl = ` +var resourceWithForEachInvalidURL = ` locals { jobs = [ { name = "test", url = "" }