Skip to content

Commit 0cb6577

Browse files
authored
[Connections] Consider null and unknown as valid in url string validator (#1942)
1 parent 7ee2754 commit 0cb6577

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

internal/resources/connections/metrics_endpoint_scrape_job_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ func TestAcc_MetricsEndpointScrapeJob(t *testing.T) {
7373
RefreshState: false,
7474
ExpectError: regexp.MustCompile(`These attributes must be configured together`),
7575
},
76+
{
77+
Config: resourceWithForEachValidURL,
78+
PlanOnly: true,
79+
RefreshState: false,
80+
ExpectNonEmptyPlan: true,
81+
},
82+
{
83+
Config: resourceWithForEachInvalidURL,
84+
PlanOnly: true,
85+
RefreshState: false,
86+
ExpectError: regexp.MustCompile(`A valid URL is required`),
87+
},
7688
{
7789
// Creates a managed resource
7890
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" {
130142
scrape_interval_seconds = 120
131143
}
132144
`
145+
146+
var resourceWithForEachValidURL = `
147+
locals {
148+
jobs = [
149+
{ name = "test", url = "https://google.com" }
150+
]
151+
}
152+
153+
resource "grafana_connections_metrics_endpoint_scrape_job" "valid_url" {
154+
for_each = { for j in local.jobs : j.name => j.url }
155+
stack_id = "......"
156+
name = each.key
157+
enabled = false
158+
authentication_method = "bearer"
159+
authentication_bearer_token = "test"
160+
url = each.value
161+
}
162+
`
163+
164+
var resourceWithForEachInvalidURL = `
165+
locals {
166+
jobs = [
167+
{ name = "test", url = "" }
168+
]
169+
}
170+
171+
resource "grafana_connections_metrics_endpoint_scrape_job" "invalid_url" {
172+
for_each = { for j in local.jobs : j.name => j.url }
173+
stack_id = "......"
174+
name = each.key
175+
enabled = false
176+
authentication_method = "bearer"
177+
authentication_bearer_token = "test"
178+
url = each.value
179+
}
180+
`

internal/resources/connections/resources.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func (v HTTPSURLValidator) MarkdownDescription(_ context.Context) string {
3232
}
3333

3434
func (v HTTPSURLValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
35+
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
36+
return
37+
}
38+
3539
value := request.ConfigValue.ValueString()
3640

3741
if value == "" {

internal/resources/connections/resources_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,16 @@ func Test_httpsURLValidator(t *testing.T) {
2323
providedURL: types.StringValue("https://dev.my-metrics-endpoint-url.com:9000/metrics"),
2424
expectedDiags: nil,
2525
},
26-
"invalid empty string": {
27-
providedURL: types.StringValue(""),
28-
expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic(
29-
path.Root("test"),
30-
"value must be valid URL with HTTPS",
31-
"A valid URL is required.\n\nGiven Value: \"\"\n",
32-
)},
26+
"null is considered valid": {
27+
providedURL: types.StringNull(),
28+
expectedDiags: diag.Diagnostics(nil),
3329
},
34-
"invalid null": {
35-
providedURL: types.StringNull(),
36-
expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic(
37-
path.Root("test"),
38-
"value must be valid URL with HTTPS",
39-
"A valid URL is required.\n\nGiven Value: \"\"\n",
40-
)},
30+
"unknown is considered valid": {
31+
providedURL: types.StringUnknown(),
32+
expectedDiags: diag.Diagnostics(nil),
4133
},
42-
"invalid unknown": {
43-
providedURL: types.StringUnknown(),
34+
"invalid empty string": {
35+
providedURL: types.StringValue(""),
4436
expectedDiags: diag.Diagnostics{diag.NewAttributeErrorDiagnostic(
4537
path.Root("test"),
4638
"value must be valid URL with HTTPS",

0 commit comments

Comments
 (0)