问题
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?.
回答1:
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"
}
回答2:
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()
回答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
来源:https://stackoverflow.com/questions/58543614/how-do-i-list-my-gcp-projects-under-no-organization-using-the-resource-manager-a