I would like to know the organization ID of my current project in GCP.
gcloud projects describe PROJECT_ID will show the parent which can be the organization, but if the parent is a folder, the organization ID does not appear.
I could recurse up the parent hierarchy of the folders using gcloud resource-manager folders describe FOLDER_ID, but that is a hassle. It is also impossible if I do not have organization-level permissions.
Yet I do have access to organization IDs: gcloud organizations list shows several organizations, though not their mapping to projects.
How can I achieve this?
Use the below gcloud command in cloud shell.
gcloud projects get-ancestors {projectId}
This should give the output as below.
ID TYPE
Your-project-ID project
123456789012 folder
567890123456 organization
If we read here we find that your organization is the root of the ancestor tree of your current project.
We also find that there is an API that can be called to retrieve the ancestry chain from a given project upwards. This means that we can retrieve the organization id of your project using that API.
The API is documented here.
It has the high level REST format of:
POST https://cloudresourcemanager.googleapis.com/v1/projects/{projectId}:getAncestry
A possible command might be:
curl -X POST -H "Authorization: Bearer \"$(gcloud auth application-default print-access-token)\"" \
-H "Content-Type: application/json; charset=utf-8" \
https://cloudresourcemanager.googleapis.com/v1/projects/<MY_PROJECT>:getAncestry
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With