Mayeru
Mayeru

Reputation: 1084

How do I list my GCP projects under No Organization using the Resource Manager API?

I can't seem to find a proper filter for the Project List API method to list only the projects which I have access to but are under No Organization.

Is there a filter for this? What would be the way to achieve this?.

Upvotes: 0

Views: 3482

Answers (3)

Cloudkollektiv
Cloudkollektiv

Reputation: 14669

According to the documentation you should be able to use filters to check whether a project belongs to a parent (organization). Through the cli this works:

gcloud projects list --filter="parent.id.yesno(yes='Yes', no='No')=No"

This would also work:

gcloud projects list --filter="parent.id:None"

The python equivalent would then be:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('cloudresourcemanager', 'v1beta1', credentials=credentials)

filter = "parent.id:None"
projects = service.projects().list(filter=filter).execute()

Upvotes: 3

Using the filter "-parent.type:folder" (with the dash at the beginning and with no parent.id parameter) should address the issue.

I have tried the API [1] using this filter and got no-organization projects.

[1] https://cloud.google.com/resource-manager/reference/rest/v1/projects/list

Upvotes: 2

Mayeru
Mayeru

Reputation: 1084

I’m afraid there’s no straightforward way to achieve this. The filter would have to match a “parent.id” or a “parent.type” property, which in the case of projects without organization it doesn’t exists (they don’t have a “parent” attribute).

It would have to be done in two steps:

1.- List all the projects using the mentioned Project List method.

2.- Go through each of the projects using the Project Get method and checking that the attribute “parent” exists, if the attribute exists it means it belongs to an organization (or a folder), else, it doesn’t.

Example of the Project Get response with organization:

{
  "projectNumber": "4444444444",
  "projectId": "my-project-id",
  "lifecycleState": "ACTIVE",
  "name": "my-project-name",
  "createTime": "2019-04-05T06:57:37.142Z"
  "parent": {
    "type": "organization",
    "id": "5555555555”
  }

}

Example of the Project Get response without organization:

{
  "projectNumber": "4444444444",
  "projectId": "my-project-id",
  "lifecycleState": "ACTIVE",
  "name": "my-project-name",
  "createTime": "2019-04-05T06:57:37.142Z"
}

Upvotes: 2

Related Questions