terraform
terraform copied to clipboard
[WIP] Adds parseurl function
Description
Introduces the parseurl
language function which parses a URL string into a map containing the URL segments
Fixes https://github.com/hashicorp/terraform/issues/23893
Usage
locals {
url = "https://username:[email protected]:8000/search?q=items#top"
}
output "url_parts" {
value = parseurl(local.url)
}
output "port" {
value = parseurl(local.url)["Port"]
}
output "hostname" {
value = parseurl(local.url)["Hostname"]
}
Outputs:
hostname = "example.com"
port = "8000"
url_parts = tomap({
"Fragment" = "top"
"Hostname" = "example.com"
"Password" = "password"
"Path" = "/search"
"Port" = "8000"
"RawQuery" = "q=items"
"Scheme" = "https"
"Username" = "username"
})
Target Release
1.4.0
Draft CHANGELOG entry
NEW FEATURES
- Introduces
parseurl
language function which parses a URL string into a map containing the URL segments
Looks like this is missing the optional port in the URL, see https://url.spec.whatwg.org/#url-representation
Host contains the port, as it's calling url.Host and not https://pkg.go.dev/net/url#URL.Hostname.
Looks like this is missing the optional port in the URL, see https://url.spec.whatwg.org/#url-representation
Good find 💪
I think it would be useful to seperate the hostname and port, will push the changes shortly.
Thanks for this submission! I suspect this function would need to be built as a plugin function provider, as opposed to a function built into Terraform. As such it would be waiting for the implementation of plugin function providers. Please see:
- https://github.com/hashicorp/terraform/issues/2771
- https://github.com/hashicorp/terraform/issues/27696
That said, I'll bring it to triage and see if it could be considered as a built-in function. Thanks again for this submission!
This would be quite nice to have built in IMO. =)
For posterity, the core team conversation on the parseurl
function focused on concerns around details of the net/url implementation and concerns that there may be other functions that would want to share a namespace with a parseurl function. So, leaning towards provider function rather than built-in.
would love to see this!
This adds this functionality as a data source.
https://registry.terraform.io/providers/northwood-labs/corefunc/latest/docs/data-sources/url_parse