Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Connections] Consider null and unknown as valid in url string validator #1942

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with the early break on IsNull or IsUnknown, we still do the validation at a later point?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading