dbt-labs / terraform-provider-dbtcloud

dbt Cloud Terraform Provider
https://registry.terraform.io/providers/dbt-labs/dbtcloud
MIT License
80 stars 18 forks source link

dbtcloud_project data-source fetch by project name enhancement #189

Closed justbldwn closed 9 months ago

justbldwn commented 10 months ago

we have a use-case where we are looking to use the dbtcloud_project data-source, but ideally are looking to pass in a project name to get back the project id. for example, for maintenance tasks like creating/updating dbtcloud_groups, we have the project name easily available to us, and would make it cleaner to understand which projects tie to which groups (instead of only having an id reference).

is there anyway this data-source could be updated to optionally allow for a project name to be passed in? or potentially a separate datasource to get project details based on name?

https://registry.terraform.io/providers/dbt-labs/dbtcloud/latest/docs/data-sources/project

thanks!

b-per commented 10 months ago

Hi @justbldwn !

This is not the first time that I get this request but until now I was a bit reluctant to add the feature, just because dbt Cloud doesn't enforce uniqueness of project names, so it would be possible to get multiple projects (and project IDs) matching a given name.

I will think again about it and see if I add the feature and raise some errors if there is more than 1 project with the given name.

In the meantime, as a workaround, you can also use the http provider with the following logic:

data "http" "example" {
  url = "https://cloud.getdbt.com/api/v2/accounts/${var.dbt_account_id}/projects/"
  request_headers = {
    Authorization = "Token ${var.dbt_token}"
  }
}

locals {
    project_name = "Analytics"
    projects = jsondecode(data.http.example.response_body)
    project_id = [for project in local.projects.data: project.id if project.name == local.project_name][0]
}

data "dbt_cloud_project" "test_project" {
  project_id = local.project_id
}

output "name" {
    # getting back the same name as local.project_name
    value = data.dbt_cloud_project.test_project.name
}
b-per commented 9 months ago

Available in 0.2.10 that should be available in a few minutes on the TF registry

justbldwn commented 9 months ago

thanks so much!