Google Cloud Platform: List available projects using api

Viewed 5206

I want to achieve the same effect of the gcloud projects list via an api call (python) specifically.

However, the only thing I can come across by browsing docs is this.

Is this operation tied specifically to the Resource Manager API?

What happens regarding visibility to other projects? If the RM is tied to a project, how can it view (and therefore list) the other ones?

3 Answers

You can only list projects for which you have permissions to access. This means that you cannot see all projects unless you have the rights to access them. In my examples below I show which scopes are required. This also means that you can list projects across accounts. This allows you to see which projects you have access to using the credentials specified in the examples. I show how to use Application Default Credentials (ADC) and Service Account Credentials (Json file format).

For more information you can read my article here about projects.

These examples have been tested with Python 3.6 on Windows 10 Professional. These examples will display the project list exactly as the CLI.

Example 1 using the Python Client Library (services discovery method):

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.oauth2 import service_account

# Example using the Python Client Library

# Documentation
# https://github.com/googleapis/google-api-python-client
# https://developers.google.com/resources/api-libraries/documentation/cloudresourcemanager/v2/python/latest/

# Library Installation
# pip install -U google-api-python-client
# pip install -U oauth2client

# Requires one of the following scopes
# https://www.googleapis.com/auth/cloud-platform
# https://www.googleapis.com/auth/cloud-platform.read-only
# https://www.googleapis.com/auth/cloudplatformprojects
# https://www.googleapis.com/auth/cloudplatformprojects.readonly

print('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))

# Uncomment to use Application Default Credentials (ADC)
credentials = GoogleCredentials.get_application_default()

# Uncomment to use Service Account Credentials in Json format
# credentials = service_account.Credentials.from_service_account_file('service-account.json')

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

request = service.projects().list()

while request is not None:
    response = request.execute()

    for project in response.get('projects', []):
        print('{:<20} {:<22} {:<21}'.format(project['projectId'], project['name'], project['projectNumber']))

    request = service.projects().list_next(previous_request=request, previous_response=response)

Example 2 using the Python Google Cloud Resource Manager API Client Library:

from google.cloud import resource_manager

# Example using the Python Google Cloud Resource Manager API Client Library

# Documentation
# https://pypi.org/project/google-cloud-resource-manager/
# https://github.com/googleapis/google-cloud-python
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/index.html
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/client.html
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/project.html

# Library Installation
# pip install -U google-cloud-resource-manager

# Requires one of the following scopes
# https://www.googleapis.com/auth/cloud-platform
# https://www.googleapis.com/auth/cloud-platform.read-only
# https://www.googleapis.com/auth/cloudplatformprojects
# https://www.googleapis.com/auth/cloudplatformprojects.readonly

print('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))

# Uncomment to use Application Default Credentials (ADC)
client = resource_manager.Client()

# Uncomment to use Service Account Credentials in Json format
# client = resource_manager.Client.from_service_account_json('service-account.json')

for project in client.list_projects():
    print('{:<20} {:<22} {:<21}'.format(project.project_id, project.name, project.number))

The client library has changed since @john-hanley's thoroughgoing answer. Here's a version using v3 of the API and v1.3.3 of the client:

from google.cloud.resourcemanager import ProjectsClient

ORGANIZATION_ID = 'your-org's-numeric-id'

client = ProjectsClient()
project_pager = client.list_projects(parent=f'organizations/{ORGANIZATION_ID}')
pjs = []
for page in project_pager:
    pjs.append(page)

pjs is a list of google.cloud.resourcemanager_v3.types.projects.Project objects.

Note that this assumes you're using Application Default Credentials (ADC), and that the user account in question has the resourcemanager.projects.list permission.

To use ADC, run gcloud auth application-default login or gcloud auth login --update-adc after changing user accounts.

If you can successfully run gcloud projects list, then you have the necessary level of access to use the code above.

In newer versions of the Python client library, ProjectsClient.search_projects has functionality similar to the pre-release Client.list_projects which in turn is similar to gcloud projects list from the command line.

Minimal Sample

from google.cloud.resourcemanager import ProjectsClient

for project in ProjectsClient().search_projects():
    print(project.display_name)
Related