terraform icon indicating copy to clipboard operation
terraform copied to clipboard

[WIP] Adds parseurl function

Open flynnhandley opened this issue 1 year ago • 8 comments

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

flynnhandley avatar Dec 04 '22 09:12 flynnhandley

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Dec 04 '22 09:12 hashicorp-cla

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.

icco avatar Dec 04 '22 19:12 icco

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.

flynnhandley avatar Dec 04 '22 22:12 flynnhandley

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!

crw avatar Dec 05 '22 21:12 crw

This would be quite nice to have built in IMO. =)

Manouchehri avatar Jan 31 '23 16:01 Manouchehri

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.

crw avatar Feb 01 '23 00:02 crw

would love to see this!

MauriceArikoglu avatar Oct 01 '23 19:10 MauriceArikoglu

This adds this functionality as a data source.

https://registry.terraform.io/providers/northwood-labs/corefunc/latest/docs/data-sources/url_parse

skyzyx avatar Feb 14 '24 22:02 skyzyx