Registry / gcp / google-cloud-org-policy

google-cloud-org-policy

JSON →
library1.18.0pypypi✓ verified 22d ago

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-policy
INSTALL
IMPORT
SIG · GOOGLE-CLOUD-ORG-P
G
google-cloud-org-policy
gcppythonv1.18.0
Install
5.3s avg
Import
1370ms
Disk
68MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.18.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.580s · 69.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.3s · import 1.160s · 68MB
68MB installed
● package 68MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

OrgPolicyClient
from google.cloud import orgpolicy_v2
types
from google.cloud.orgpolicy_v2 import types
from google.cloud.orgpolicy import types
The API is versioned (v2), and the correct import path includes '_v2'. Using 'orgpolicy' directly might lead to an older or incompatible client.

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.

import os from google.cloud import orgpolicy_v2 from google.cloud.orgpolicy_v2 import types # Set your Google Cloud Project ID and Organization ID # Or ensure GOOGLE_APPLICATION_CREDENTIALS environment variable is set. # For local development, use 'gcloud auth application-default login' # For deployed apps, use attached service accounts. def list_organization_constraints(organization_id: str): """Lists all organization constraints. Args: organization_id: The numeric ID of your Google Cloud Organization (e.g., '12345678901'). """ client = orgpolicy_v2.OrgPolicyClient() # The parent resource for all constraints (e.g., 'organizations/ORGANIZATION_ID') parent_resource = f"organizations/{organization_id}" print(f"Listing constraints for organization: {parent_resource}") # Initialize request argument(s) request = types.ListConstraintsRequest(parent=parent_resource) # Make the request page_result = client.list_constraints(request=request) # Handle the response for response in page_result: print(f"Constraint: {response.name} ({response.display_name})") print(f" Description: {response.description}") print(f" Supports Conditions: {response.supports_conditions}") # Example usage (replace with your actual organization ID) if __name__ == "__main__": # It's recommended to set GOOGLE_APPLICATION_CREDENTIALS or use 'gcloud auth application-default login' # for local execution. For this example, we'll try to get it from environment or use a placeholder. # Replace 'YOUR_ORGANIZATION_ID' with your actual Google Cloud Organization ID org_id = os.environ.get('GCP_ORGANIZATION_ID', 'YOUR_ORGANIZATION_ID') if org_id == 'YOUR_ORGANIZATION_ID': print("Please set the GCP_ORGANIZATION_ID environment variable or replace 'YOUR_ORGANIZATION_ID' in the code.") else: try: list_organization_constraints(org_id) except Exception as e: print(f"An error occurred: {e}") print("Ensure you have the necessary IAM permissions (e.g., 'orgpolicy.policyViewer') and your organization ID is correct.")
Debug
Known issues
breakingThe library explicitly requires Python 3.9 or newer. Support for Python versions 3.8 and below has been dropped. Users on older Python versions must upgrade to maintain compatibility and receive updates.
fix
Upgrade your Python environment to version 3.9 or higher.
affects: <=1.16.x (for previous versions requiring <3.9), >=1.17.0 (requires >=3.9)
gotchaAuthentication to Google Cloud services should follow best practices. Avoid hardcoding credentials or committing service account key files to version control.
fix
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.
affects: All versions
gotchaGoogle Cloud resource names, especially for Organization Policy, follow specific formats (e.g., `organizations/ORGANIZATION_ID/policies/POLICY_ID`). Incorrect formatting often leads to `InvalidArgument` errors.
fix
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/`.
affects: All versions
gotchaNew Google Cloud organizations created on or after May 3, 2024, automatically enforce a stronger set of default Org Policies. These include restrictions on service account key creation and automatic IAM grants.
fix
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.
affects: All versions, specifically impacting new organizations or when interacting with newly created resources.
gotchaThe library uses standard Python logging, but logging events are not handled by default. This means you won't see debug or informational messages unless configured. Also, logs may contain sensitive information.
fix
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.
affects: All versions
Errors
Common errors & fixes
ERROR: (gcloud.resource-manager.org-policies.disable-enforce) [USER_ACCOUNT] does not have permission to access organizations instance [ORGANIZATION_ID:setOrgPolicy] (or it may not exist): The caller does not have permission.
This 'Permission Denied' error occurs when the user attempting to modify an organization policy lacks the necessary 'Organization Policy Administrator' (roles/orgpolicy.policyAdmin) IAM role at the organization level.
fix
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.
HTTPError 400: Invalid request: Organization Policy check failure: the external IP of this instance violates the constraints/sql.restrictPublicIp enforced.
This error indicates that a Google Cloud resource creation or modification request (e.g., creating a Cloud SQL instance with a public IP) is being blocked by an enforced organization policy constraint, such as `constraints/sql.restrictPublicIp`.
fix
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.
ERROR: (gcloud.org-policies.set-policy) INVALID_ARGUMENT: The policy contains invalid list value(s): [domains/example.app]
This `INVALID_ARGUMENT` error, specifically for `iam.allowedPolicyMemberDomains`, means that the provided value for the policy constraint is in an incorrect format. For domain restrictions, it often expects a Google Workspace Customer ID, not a domain name with a 'domains/' prefix.
fix
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`.
ModuleNotFoundError: No module named 'google.cloud.orgpolicy_v2'
This error occurs when the Python environment where the code is being run does not have the `google-cloud-org-policy` library installed, or the import statement is incorrect for the installed version. The `orgpolicy_v2` module is part of the `google-cloud-org-policy` library for accessing version 2 of the API.
fix
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`.
Upgrade
Version history
1.18.0latest on PyPI · released Jun 22, 2026
Audit
Dependencies
google-api-corerequiredCore dependency for Google Cloud client libraries, providing shared functionality like authentication, API design patterns, and request handling.
Agent activity
25 hits · last 30 days
node
20
OpenAI (training)
1
Resources
google-cloud-org-policy — pip install google-cloud-org-policy · libregistry