The Google Cloud Org Policy API client library allows users to configure governance rules on their GCP resources across the Cloud Resource Hierarchy. It is currently at version 1.17.0 and maintains a regular release cadence with updates often for new features or bug fixes.
pip install google-cloud-org-policyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate the `OrgPolicyClient` and list all available organization constraints for a given Google Cloud organization. It highlights the use of `orgpolicy_v2` for API versioning and standard Google Cloud authentication practices.
Upgrade your Python environment to version 3.9 or higher.
For local development, use `gcloud auth application-default login`. For applications deployed on GCP (e.g., Cloud Run, GKE, Compute Engine), use attached service accounts with appropriate IAM roles. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable only when absolutely necessary and ensure secure handling.
Always refer to the official API documentation for the exact resource name format required by each method. Ensure Organization IDs are numeric and correctly prepended with `organizations/`.
Be aware of these default policies. If your workload requires exceptions, you might need to explicitly modify or disable these constraints within your organization's policy settings, ensuring to apply the narrowest possible exceptions.
Explicitly configure Python's `logging` module to capture and handle logs from `google.cloud.orgpolicy_v2`. Be cautious about where logs are stored due to potential sensitive information. Example: `import logging; logging.basicConfig(level=logging.INFO)` or configure more granular handlers.
Ensure the user account has the 'Organization Policy Administrator' role (roles/orgpolicy.policyAdmin) granted at the organization level. This role is distinct from 'Organization Administrator' (roles/resourcemanager.organizationAdmin) and must be explicitly assigned.
Review the API response error message to identify the specific constraint being violated. Modify the resource configuration to comply with the policy (e.g., configure the Cloud SQL instance to use an internal private IP address if `constraints/sql.restrictPublicIp` is enforced). If necessary, work with your security team to understand the policy's scope or to request a project-level exception.
When setting policies like `iam.allowedPolicyMemberDomains`, ensure the `allowedValues` are the Google Workspace Customer ID (DIRECTORY_CUSTOMER_ID) and not the domain name or a prefixed domain name. Obtain the correct Customer ID using `gcloud organizations describe ORGANIZATION_ID`.
First, ensure the `google-cloud-org-policy` library is installed: `pip install google-cloud-org-policy`. If it is already installed, verify that the Python interpreter running the code is the one where the library was installed. The correct import statement is `from google.cloud import orgpolicy_v2`.