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

HCL2 length function not taking string? #14462

Closed
xorander00 opened this issue Sep 5, 2022 · 4 comments · Fixed by #14463
Closed

HCL2 length function not taking string? #14462

xorander00 opened this issue Sep 5, 2022 · 4 comments · Fixed by #14463
Assignees
Labels
stage/accepted Confirmed, and intend to work on. No timeline committment though. theme/docs Documentation issues and enhancements type/bug

Comments

@xorander00
Copy link

Nomad version

v1.3.2

Operating system and Environment details

FreeBSD 13.1-STABLE

Issue

HCL2 length() function, according to the docs, should accept an argument of any of the following types: string, array, map, or tuple.
It seems to only accept collections though, not a string. I know I can remove the default assignment and effectively achieve the same thing, but I'd like to solve why length() isn't taking a string. I tried strlen() as well since that's in the Terraform docs and I wondered if Nomad might have a version mismatch with what's in the docs, but nope.

Reproduction steps

See above snippet.

Expected Result

Return numeric length of string.

Actual Result

HCL2 semantic error when running nomad job validate myjob.hcl. Complains that length() expects a map, array, or tuple.

Error getting job struct: Error parsing job file from ./ports/_job.hcl:
_job.hcl:36,17-24: Error in function call; Call to function "length" failed: collection must be a list, a map or a tuple.

Job file (if appropriate)

variable "portsList" {
  description = "Path to file with list of ports"
  type = string
  default = ""
  validation  {
    condition = length(var.portsList) > 0
    error_message = "Path to file containing list of ports is required."
  }
}
@jrasell jrasell added theme/docs Documentation issues and enhancements stage/accepted Confirmed, and intend to work on. No timeline committment though. labels Sep 5, 2022
@jrasell jrasell self-assigned this Sep 5, 2022
@jrasell jrasell added this to Needs Triage in Nomad - Community Issues Triage via automation Sep 5, 2022
@jrasell jrasell moved this from Needs Triage to In Progress in Nomad - Community Issues Triage Sep 5, 2022
@jrasell
Copy link
Member

jrasell commented Sep 5, 2022

Hi @xorander00 and thanks for raising this issue. The current length function does indeed only support maps, lists, or tuples. I will raise a change to the documentation for this as well as add a new function to allow for calculating the length of a string.

@xorander00
Copy link
Author

@jrasell No problem!

I do have a quick question that overlaps with this issue. Is the HCL2 module used in nomad trailing by a few versions? I'm trying to use formatdate() with timestamp() as the 2nd argument (to use the current date/time).

timestamp() is not listed in the Nomad docs, so it's not a surprise that it fails, but I do see it in the packer docs. I'm trying to use formatdate to set a local variable to a formatted date/time string when an input variable is empty. If timestamp() is not available, then I'm not sure what other function to use for it (if there is one). I could of course render it using go-template at placement time, but I'd prefer this be done during job parsing as the value will always be consistent wherever it is referenced in the job.

Not sure as I'm not familiar with the codebase, but I get the feeling that maybe rebasing and updating the HCL2 implementation used in Nomad might fix a number of these types of potential issues?

@jrasell
Copy link
Member

jrasell commented Sep 5, 2022

Hi @xorander00; merely importing the library is not enough to make the language and function usable. The functions in particular need to be mapped for use which means there is potential for differences between implementations.

If you have a use case for adding additional HCL2 functions, I would suggest raising a new feature request so we can correctly track these along with any context you have. If you're interested in trying to raise a PR to add the function, this PR I just raised could serve as a good base for how to do it.

Nomad - Community Issues Triage automation moved this from In Progress to Done Sep 6, 2022
@github-actions
Copy link

github-actions bot commented Jan 5, 2023

I'm going to lock this issue because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
stage/accepted Confirmed, and intend to work on. No timeline committment though. theme/docs Documentation issues and enhancements type/bug
Projects
Development

Successfully merging a pull request may close this issue.

2 participants