Google Cloud IAM Client Library

2.22.0 · active · verified Thu Apr 09

The `google-cloud-iam` client library provides access to the Google Cloud IAM API, primarily focusing on Workload Identity Federation (WIF) resources like Workload Identity Pools and Providers. It allows programmatic management of these resources. The current version is 2.22.0, and it follows the rapid release cadence typical of Google Cloud client libraries, with updates often coinciding with API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `IAMClient` and list Workload Identity Pools within a Google Cloud Organization. It requires `GCP_ORGANIZATION_ID` to be set as an environment variable and relies on Application Default Credentials for authentication. It highlights the library's focus on Workload Identity Federation.

import os
from google.cloud import iam_v2

# Set your Google Cloud Organization ID as an environment variable, e.g., GCP_ORGANIZATION_ID=1234567890
organization_id = os.environ.get("GCP_ORGANIZATION_ID", "your-organization-id")

if organization_id == "your-organization-id":
    print("Warning: Please set the GCP_ORGANIZATION_ID environment variable to run this example.")
    print("This quickstart for 'google-cloud-iam' focuses on Workload Identity Federation (WIF).")
    print("For general IAM policy management, consider 'google-cloud-iam-admin' or service-specific clients.")
else:
    try:
        # Authenticate using Application Default Credentials (ADC)
        # e.g., by running `gcloud auth application-default login` or setting `GOOGLE_APPLICATION_CREDENTIALS`.
        client = iam_v2.IAMClient()

        # The parent resource for listing Workload Identity Pools is in the format 'organizations/{organization_id}/locations/global'
        parent = client.common_location_path(organization=organization_id, location="global")

        print(f"Listing Workload Identity Pools in organization {organization_id} (location: global):")
        for pool in client.list_workload_identity_pools(parent=parent):
            print(f"- Pool Name: {pool.name}, Display Name: {pool.display_name}, State: {pool.state.name}")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure the service account or user running this code has 'iam.workloadIdentityPools.list' permission.")
        print("Also, verify that the 'GCP_ORGANIZATION_ID' is correct and accessible.")

view raw JSON →