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 a function similar to file() that returns empty string if file doesn't exist #19361

Closed
devonbleak opened this issue Nov 13, 2018 · 2 comments

Comments

@devonbleak
Copy link
Contributor

devonbleak commented Nov 13, 2018

Current Terraform Version

0.11.10

Use-cases

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:

  1. Update existing file() behavior to return empty string rather than erroring when file does not exist
  2. 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
  3. Add a new function fileifexists()

Of the 3 I like number 1 the best.

References

@apparentlymart
Copy link
Contributor

Hi @devonbleak! Thanks for this feature request.

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:

  • Add a new function fileexists which returns true if a given path refers to a file. (lang: Add fileexists function #19086)
  • 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!

@ghost
Copy link

ghost commented Mar 31, 2020

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 ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants