terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

Add a data source for custom GET requests to REST API.

Open galargh opened this issue 3 years ago • 0 comments

Description

I wanted to use https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/repository_file to check if a specific file exists or not. I realised it was not possible because terraform apply would fail if a file does not exist because the underlying API call receives 404.

That's how I got an idea to introduce a general purpose API call data source which issues a GET request to a GitHub API endpoint and returns a response.

Example
data "github_rest_api" "check_if_file_exists" {
  endpoint = "repos/ORG/REPO/contents/FILE?ref=BRANCH"
}

output "does_file_exist" {
  value = data.github_rest_api.check_if_file_exists.code == 200 ? "Yes! It does!" : "No :("
}

Testing
  • [x] Ran TF_ACC=1 go test -v ./... -run ^TestAccGithubRestApiDataSource
  • [x] Installed the provider (go install), added it to my project and successfully used the newly added data source

galargh avatar Apr 19 '22 10:04 galargh