-
Notifications
You must be signed in to change notification settings - Fork 766
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
feat: add tree
data source
#1027
Conversation
Returns a single tree using the SHA1 value for that tree.
Interesting! Do you mind explaining a little bit more about the use case for this? Also, would you mind providing relevant updates to the website docs? Also also: it looks like the build is red right now due to a |
Hey @kfcampbell, My use case may be really specific, so if it doesn't create much value to add this data source, then it's no problem. Our team uses env0 to manage Terraform operations like Regarding my use case; I wrote a Terraform module for creating projects in env0 using their official provider. The module reads a YAML file which contains an array of desired projects to create in env0. For example, the configuration file could look something like the following: - repository: hashicorp/hashicat-aws
terraform_version: 1.1.2
variables:
- category: env
key: AWS_DEFAULT_REGION
value: us-east-1 Which would create a new project named The Terraform config for discovering these files looks like the following: locals {
// Filters the elements in the tree to only include files ending in `.tf` or `.tf.json`.
templates = toset([
for element in jsondecode(data.http.this.body)["tree"] :
dirname(element.path)
if element.type == "blob" && length(regexall("\\w+(.tf|.tf.json)$", element.path)) > 0
])
}
// Fetches the tree for a specific commit. By default the latest commit of
// the default branch (e.g., `main`).
data "http" "this" {
request_headers = {
Accept = "application/vnd.github.v3+json"
Authorization = "token ${var.token}"
}
url = "https://api.github.com/repos/${local.owner}/${local.repository.name}/git/trees/${data.github_branch.this.sha}?recursive=true"
}
// Creates an env0 template for each directory that contains `.tf` or `.tf.json` files.
// For example, the `hashicorp/hashicat-aws` repository contains `.tf` files in the
// root of the repository, so a single env0 template is created: `env0_template.this["."]`.
resource "env0_template" "this" {
for_each = local.templates
description = "Managed by Terraform"
github_installation_id = 21147410
name = basename(each.value) == "." ? env0_project.this.name : basename(each.value)
path = each.value
repository = local.repository.html_url
revision = local.repository.default_branch
terraform_version = var.terraform_version
type = "terraform"
}
In order to run Terraform, I need to input the
Since this new data source would be part of the GitHub provider, it would remove the need to define a I hope I was able to explain this well. If something doesn't make sense, please let me know and I'll do my best to explain further. I'll also run Thank you! |
When I run |
@jasonwalsh I ran I can be convinced to include the data source when the docs are updated! Thanks for contributing. |
@kfcampbell ran |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I missed one more thing earlier! Do you mind linking to the new docs page you added on this doc? That way the sweet new docs page you added will be discoverable. Thank you!
@kfcampbell done |
* feat: add `tree` data source Returns a single tree using the SHA1 value for that tree. * test: add acceptance test for `github_tree` * syle: run `make fmt` command * docs: add documentation for new data source * docs: add link to website
* feat: add `tree` data source Returns a single tree using the SHA1 value for that tree. * test: add acceptance test for `github_tree` * syle: run `make fmt` command * docs: add documentation for new data source * docs: add link to website
Not sure if this is a desired data source, but I had a use case where I needed to find specific files in a GitHub repository using this provider and there was no such way. I ended up using the
http
data source to issue a request to the Get a tree endpoint, but decided to write a data source for it.This is my first time contributing to a Terraform provider, so if I need to make any changes to the data source code or the acceptance tests, please let me know.
This pull request adds a new data source
github_tree
which returns a single tree given a SHA1 value. See Get a tree from the GitHub documentation.An example is as follows:
Which would output something like: