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

Allow getting projects by organization/folder #2958

Closed
ghost opened this issue Jan 29, 2019 · 11 comments
Closed

Allow getting projects by organization/folder #2958

ghost opened this issue Jan 29, 2019 · 11 comments
Assignees
Milestone

Comments

@ghost
Copy link

ghost commented Jan 29, 2019

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment. If the issue is assigned to the "modular-magician" user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If the issue is assigned to a user, that user is claiming responsibility for the issue. If the issue is assigned to "hashibot", a community member has claimed the issue already.

Description

Currently project data source only supports getting a project by ID. But it would be nice to be able to find a project using organization/folder in case the ID is not known right away. As shown here: https://cloud.google.com/resource-manager/docs/creating-managing-folders

screen shot 2019-01-29 at 5 07 20 pm

Seems like this should be possible since folder_id and org_id are already used in the read function

New or Affected Resource(s)

  • google_project data source

Potential Terraform Configuration

data "google_project" "dev" {
   organization = "org"
   folder       = "folder/path/to/it"
   name         = "dev-project"
}

References

  • #0000
@ghost ghost added the enhancement label Jan 29, 2019
@rileykarson rileykarson self-assigned this Jan 29, 2019
@rileykarson
Copy link
Collaborator

Out of curiosity, what kind of use cases would you use this for? This is available under the https://cloud.google.com/resource-manager/reference/rest/v1/projects/list API, but searching could have unintended consequences- what if a new project with a display name that matches is added, for example?

@ghost
Copy link
Author

ghost commented Jan 30, 2019

The use case is to apply similar resources within a folder: e.g. I have main, dev1, dev2, dev3 projects that all might share some setup to function (like sandbox envs).

To clarify implementation:

This would be similar to what AWS provider has for subnets: https://www.terraform.io/docs/providers/aws/d/subnet_ids.html

It's true that name might cause a conflict, so maybe use some other filter (afaik there's labels):

data "google_projects" "dev" {
   organization = "org"
   folder       = "folder/path/to/it"
   labels         = ["dev"]
}

I want to be able to get a list of project IDs and then I can go do things with them.

@lucazz
Copy link

lucazz commented Jan 30, 2019

Heya, this is great and will fit like a glove for the module bundles I've been writing.

Ideally we would create a pre-defined GCP folder structure w/ their given projects in it and control it all using terraform's google_project and google_folder.

That way, a person using these modules will only need to state the project name once, have everything provisioned like so and the subsequent modules like, create me a GCP VPC, create me a GKE cluster, create me a cloudSQL instance, etc could all benefit of using this datasource to fetch it's proper project_id.

@rileykarson
Copy link
Collaborator

Thanks for the insight! I'm looking at implementing this so it could be configured like so:

data "google_projects" "my-org-projects" {
  filter = "parent.id:012345678910 lifecycleState:DELETE_REQUESTED"
}

data "google_project" "deletion-candidate" {
   project_id = "${data.google_projects.my-org-projects.projects.0.project_id}"
}

Directly exposing the filter field so that we support every filter type without writing a DSL in the provider, and meaning if new filters are ever added we pick them up for free.

Does this match both of your expectations/use cases?

@lucazz
Copy link

lucazz commented Jan 30, 2019

@rileykarson what would work if we count interpolate variables in the filters. Do you know wether or not that possible?

@rileykarson
Copy link
Collaborator

Oh yup! Interpolations will work as you'd expect, including the dependency graph if you're using a resource in your config. For an example, using an organization_id variable would look like:

data "google_projects" "my-org-projects" {
  filter = "parent.id:${var.organization_id} lifecycleState:DELETE_REQUESTED"
}

data "google_project" "deletion-candidate" {
   project_id = "${data.google_projects.my-org-projects.projects.0.project_id}"
}

@lucazz
Copy link

lucazz commented Jan 30, 2019

Perfect! sign me up 'cause I'm on board!

@rileykarson
Copy link
Collaborator

Done! This will be available in 2.2.0.

@rileykarson rileykarson added this to the 2.2.0 milestone Mar 4, 2019
@lucazz
Copy link

lucazz commented Mar 5, 2019 via email

@ghost
Copy link

ghost commented Apr 4, 2019

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 4, 2019
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

2 participants