diff --git a/internal/resources/connections/metrics_endpoint_scrape_job_test.go b/internal/resources/connections/metrics_endpoint_scrape_job_test.go index 0b8159bef..a945f31b7 100644 --- a/internal/resources/connections/metrics_endpoint_scrape_job_test.go +++ b/internal/resources/connections/metrics_endpoint_scrape_job_test.go @@ -73,6 +73,18 @@ func TestAcc_MetricsEndpointScrapeJob(t *testing.T) { RefreshState: false, ExpectError: regexp.MustCompile(`These attributes must be configured together`), }, + { + 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"), @@ -130,3 +142,39 @@ resource "grafana_connections_metrics_endpoint_scrape_job" "test" { scrape_interval_seconds = 120 } ` + +var resourceWithForEachValidURL = ` +locals { + jobs = [ + { name = "test", url = "https://google.com" } + ] +} + +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 + enabled = false + authentication_method = "bearer" + authentication_bearer_token = "test" + url = each.value +} +` 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..2538633c7 100644 --- a/internal/resources/connections/resources_test.go +++ b/internal/resources/connections/resources_test.go @@ -23,24 +23,16 @@ 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",