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

add attribute default #1

Open
tbugfinder opened this issue Nov 15, 2019 · 5 comments
Open

add attribute default #1

tbugfinder opened this issue Nov 15, 2019 · 5 comments

Comments

@tbugfinder
Copy link

Hi @apparentlymart,

thanks for your effort on this project.
I tried to output a variable default, however this failed.
Do you think this could be added?

$ cat test.tpl
%{ for v in variables ~}
name: ${v.name}, defaultValue:  ${v.default}, description: ${v.description})
%{ endfor ~}


terraform-config-inspect . --json | hcltemplate test.tpl

Error: Unsupported attribute

  on test.tpl line 2:
   2: ${v.name}, defaultValue:  ${v.default}, description: ${v.description}

This object does not have an attribute named "default".



Thanks & Best
TB

@tbugfinder
Copy link
Author

This might require setting an empty string if default isn't set for a variable.

@apparentlymart
Copy link
Owner

Hi @tbugfinder,

It looks like default in terraform-config-inspect is omitted entirely when not set in the configuration, rather than explicitly set to null. I suppose that makes sense for terraform-config-inspect, but it does create a problem for templating with hcltemplate indeed.

In order to address this I think I would add a new function like Terraform's lookup to allow setting a default for an attribute that doesn't exist:

name: ${v.name}, defaultValue:  ${jsonencode(lookup(v, "default", null))}, description: ${v.description})

(I also added jsonencode here because not all values that can be set as default in Terraform can be directly converted to a string, which would create another situation where the template would fail to render.)

Unfortunately Terraform's lookup lives in Terraform itself rather than in the upstream library cty that is providing all of hcltemplate's functions here. The least involved way to get this done would be to copy that implementation from Terraform into hcltemplate, but I feel inclined to add that function to cty's "standard library" because there's nothing particularly Terraform-specific about it.

@tbugfinder
Copy link
Author

tbugfinder commented Nov 16, 2019

Hi @apparentlymart,
thanks for the quick answer, including thoughts to address the issue.
Wouldn't terraform-config-inspect provide proper json output of the default value (if available)?
Do you have an estimate wether such a request would be added to cty within "weeks" :-D ?

@tbugfinder
Copy link
Author

${jsonencode(lookup(v, "default", null))}would support different values instead of null.

@apparentlymart
Copy link
Owner

Hi @tbugfinder,

Unfortunately I will not have time to work on that change myself in the near future, because my focus is currently elsewhere. cty is another project of mine though, and I'd be happy to review a PR in that repository if someone else has the time and motivation to work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants