Google Cloud Resource Manager

1.17.0 · active · verified Sat Mar 28

The Google Cloud Resource Manager API client library provides Python methods to programmatically manage Google Cloud projects, folders, and organizations. This library (version 1.17.0) is actively maintained as part of the broader `google-cloud-python` ecosystem, with frequent releases to add features and address issues.

Warnings

Install

Imports

Quickstart

This quickstart initializes the `ProjectsClient` and then uses the `search_projects` method to list all Google Cloud projects accessible by the authenticated identity. It includes basic error handling and a reminder about setting up Application Default Credentials (ADC) for authentication.

import os
from google.cloud.resourcemanager_v3 import ProjectsClient
from google.api_core.exceptions import GoogleAPIError

def list_projects():
    # Ensure GOOGLE_APPLICATION_CREDENTIALS or gcloud auth is set up.
    # For local development, 'gcloud auth application-default login' is recommended.
    try:
        client = ProjectsClient()
        # The `search_projects` method can retrieve projects across the organization/folders
        # depending on the authenticated identity's permissions.
        # An empty query string lists all projects the user has permission to view.
        print("Listing projects:")
        for project in client.search_projects(query=''):
            print(f"  Project Name: {project.display_name} (ID: {project.project_id})")
            print(f"    State: {project.state.name}, Parent: {project.parent.type}/{project.parent.id}")
    except GoogleAPIError as e:
        print(f"An API error occurred: {e}")
        print("Ensure the Resource Manager API is enabled and you have necessary permissions.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == '__main__':
    # This assumes Application Default Credentials are set up.
    # For example, by running `gcloud auth application-default login`
    # or setting the GOOGLE_APPLICATION_CREDENTIALS environment variable.
    if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') and not os.environ.get('CLOUDSDK_CORE_ACCOUNT'):
        print("WARNING: No explicit Google Cloud credentials found. This script relies on Application Default Credentials (ADC).")
        print("         Please run 'gcloud auth application-default login' or set GOOGLE_APPLICATION_CREDENTIALS.")

    list_projects()

view raw JSON →