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

Consider local_directory Data Source #216

Open
1 task done
bflad opened this issue Jun 15, 2023 · 0 comments
Open
1 task done

Consider local_directory Data Source #216

bflad opened this issue Jun 15, 2023 · 0 comments

Comments

@bflad
Copy link
Contributor

bflad commented Jun 15, 2023

Terraform CLI and Provider Versions

N/A

Use Cases or Problem Statement

The Terraform configuration language has the fileexists() function which returns successfully if a file exists at a given path, however it does not currently handle directories. Even if it was adjusted to support directories (or created as a separate function), as with most configuration language functions, the result is evaluated before the rest of the graph. This can cause issues for practitioners wishing to check/manage directories generated/updated during the graph for purposes of clearer error messaging or other operations which might need a listing of all files within a directory.

While today we generally rely on the function documentation to warn practitioners about this potential issue, it may make sense to provide a graph-aware solution for the use case.

Proposal

Create a new local_directory data source which attempts to read a local filesystem directory. It should error if the directory does not exist otherwise return a set of file paths, for potential usage elsewhere in a Terraform configuration.

As a design sketch of potential usage:

# Ensure example directory exists, potentially due to
# another graph node with side effects creating the directory
# or as a precondition for other graph nodes
data "local_directory" "example" {
  path = "${path.module}/example"
}

# Another way to check directory exists and has files, using new check block functionality to only raise warning
check {
  data "local_directory" "node_modules" {
    path = "${path.module}/node_modules"
  }

  assert {
    condition = length(data.local_directory.node_modules.file_paths) > 0
    error_message = "${data.local_directory.node_modules.path} does not contain any files"
  }
}

Proposed data source configurable attributes:

  • path - String, required, the path to the directory

Proposed data source computed attributes:

  • file_paths - Set of strings, the paths to all files within directory

How much impact is this issue causing?

Medium

Additional Information

Code of Conduct

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant