Skip to content

Commit

Permalink
[Connections] Consider null and unknown as valid in url string valida…
Browse files Browse the repository at this point in the history
…tor (#1942)
  • Loading branch information
fridgepoet authored Dec 6, 2024
1 parent 7ee2754 commit 0cb6577
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
48 changes: 48 additions & 0 deletions internal/resources/connections/metrics_endpoint_scrape_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
}
`
4 changes: 4 additions & 0 deletions internal/resources/connections/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
24 changes: 8 additions & 16 deletions internal/resources/connections/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0cb6577

Please sign in to comment.