You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This would be very useful for conditionals in modules for example:
count="${length(file("some-file")) >0?1:0}"
With the current behavior of file() if "some-file" doesn't exist in every workspace/state that attempts to use the module then plans in those workspaces will fail.
Attempted Solutions
Currently the only workaround seems to be having to add the missing file to every workspace that invokes the module.
Proposal
A few options to consider:
Update existing file() behavior to return empty string rather than erroring when file does not exist
Add a new optional flag to file() to indicate whether to error on file does not exist (false?) or return empty string (true?) with default = current behavior
Add a new function fileifexists()
Of the 3 I like number 1 the best.
References
The text was updated successfully, but these errors were encountered:
The configuration language improvements for the forthcoming v0.12.0 release give a new option which is the one we've been planning to take to address this use-case:
Use the fact that conditionals will now properly evaluate only the selected "side" of the expression in v0.12.0 to permit the following: fileexists("foo") ? file("foo") : "default value".
Add also one or more new functions for listing directories and directory tress, glob matching, etc.
The exact use-case you described in your comment could be written like this, then:
count="${fileexists("some-file") ?1:0}"
Together these should allow for various possibilities for changing behavior based on other files present on disk alongside the configuration, including both conditionals and generating a resource per file matching some criteria.
fileexists is already merged to master ready to be included in the forthcoming v0.12.0 release. The use-cases around listing directories and creating resources for each of them are covered by #16697 and #17179 together, which will not be in the initial v0.12.0 release but we hope to complete those features in a subsequent v0.12 release, having laid important groundwork for the for_each mechanism in v0.12.0.
Since the fileexists function is already merged and the additional capabilities I mentioned (which were additional to your use-case) are covered by other issues, I'm going to close this out just to keep the discussions consolidated. Thanks again for sharing this use-case!
I'm going to lock this issue because it has been closed for 30 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.
ghost
locked and limited conversation to collaborators
Mar 31, 2020
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Current Terraform Version
0.11.10
Use-cases
This would be very useful for conditionals in modules for example:
With the current behavior of file() if "some-file" doesn't exist in every workspace/state that attempts to use the module then plans in those workspaces will fail.
Attempted Solutions
Currently the only workaround seems to be having to add the missing file to every workspace that invokes the module.
Proposal
A few options to consider:
fileifexists()
Of the 3 I like number 1 the best.
References
The text was updated successfully, but these errors were encountered: