Registry / gcp / google-cloud-service-usage

google-cloud-service-usage

JSON →
library1.17.0pypypi✓ verified 84d ago

The `google-cloud-service-usage` library provides a Python client for the Google Cloud Service Usage API. It enables programmatic management of Google Cloud services within your projects, including listing available services, checking their status, and enabling or disabling them. Part of the `google-cloud-python` monorepo, it is currently at version 1.16.0. Releases for this library, like others in the monorepo, occur frequently, often several times a month, reflecting updates to the underlying API or client code.

pip install google-cloud-service-usage
INSTALL
IMPORT
SIG · GOOGLE-CLOUD-SERVI
G
google-cloud-service-usage
gcppythonv1.17.0
Install
5.3s avg
Import
1741ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.17.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.920 runs
installs and imports cleanly · install 0.0s · import 2.030s · 68.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 5.3s · import 1.452s · 66MB
66MB installed
● package 66MB
Code
Verified usage

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

ServiceUsageClient
from google.cloud import service_usage_v1
from google.cloud import service_usage
The client class is typically found within a versioned submodule (e.g., `_v1`) to allow for API versioning.

This quickstart demonstrates how to initialize the `ServiceUsageClient` and list all enabled Google Cloud services for a specified project. Ensure your `GOOGLE_CLOUD_PROJECT` environment variable is set or replace the placeholder project ID, and that your environment is authenticated to Google Cloud.

import os from google.cloud import service_usage_v1 # Set your Google Cloud Project ID project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-gcp-project-id") # Replace with a valid project ID # IMPORTANT: Ensure you have authenticated to GCP. # For local development, run `gcloud auth application-default login`. # For service accounts, set the GOOGLE_APPLICATION_CREDENTIALS environment variable. try: # Initialize the Service Usage client client = service_usage_v1.ServiceUsageClient() # Construct the parent resource name parent = f"projects/{project_id}" print(f"Listing enabled services for project: {project_id}") # Create a request to list services, filtered by enabled state request = service_usage_v1.ListServicesRequest( parent=parent, filter="state:ENABLED" ) # Make the API call response = client.list_services(request=request) enabled_services = [service.config.name for service in response.services] if enabled_services: print(f"Enabled services found ({len(enabled_services)}):") for service_name in enabled_services[:5]: # Print first 5 for brevity print(f"- {service_name}") if len(enabled_services) > 5: print(f"... and {len(enabled_services) - 5} more.") else: print("No enabled services found for this project or insufficient permissions.") except Exception as e: print(f"An error occurred: {e}") print("Please ensure:") print("1. Your project ID is correct.") print("2. You are authenticated (e.g., `gcloud auth application-default login`).") print("3. The authenticated principal has the 'Service Usage Consumer' IAM role or equivalent.")
Debug
Known issues
gotchaGoogle Cloud client libraries require proper authentication. If running locally, `gcloud auth application-default login` is often necessary. In deployed environments, consider Service Accounts or Workload Identity.
fix
Ensure `GOOGLE_APPLICATION_CREDENTIALS` environment variable points to a service account key, or run `gcloud auth application-default login` for user authentication. For Cloud Run/Functions/Compute Engine, default service accounts are often automatically used.
affects: All versions
gotchaInteracting with Google Cloud services requires specific IAM permissions. Common errors include 'Permission Denied'.
fix
Grant the authenticated principal appropriate roles for Service Usage. For listing, `Service Usage Consumer` (roles/serviceusage.serviceUsageConsumer) is typically sufficient. For enabling/disabling, `Service Usage Admin` (roles/serviceusage.serviceUsageAdmin) or custom roles are needed.
affects: All versions
gotchaResource names for Google Cloud APIs often follow a strict format (e.g., `projects/PROJECT_ID`, `services/SERVICE_NAME`). Incorrect formatting or IDs will lead to 'Not Found' errors.
fix
Always refer to the API documentation for the exact resource name format. For Service Usage, project resources are `projects/PROJECT_ID_OR_NUMBER` and service names are full identifiers like `compute.googleapis.com`.
affects: All versions
gotchaOperations like `enable_service` and `disable_service` return a `google.api_core.operation.Operation` object, not the final result. These are long-running operations.
fix
Call the `.result()` method on the `Operation` object to wait for the operation to complete and retrieve its final status or response. For example: `operation = client.enable_service(...)` then `response = operation.result()`.
affects: All versions
Errors
Common errors & fixes
google.api_core.exceptions.PermissionDenied: 403 Permission 'serviceusage.services.list' denied on resource 'projects/your-gcp-project-id'.
The authenticated principal (user or service account) does not have the necessary IAM permissions to perform the requested action on the specified project.
fix
Grant the `Service Usage Consumer` role (for listing) or `Service Usage Admin` role (for enabling/disabling) to the principal via the Google Cloud Console IAM page or `gcloud iam roles` commands.
AttributeError: module 'google.cloud' has no attribute 'service_usage'
The Python import statement is attempting to access a module that does not exist directly under `google.cloud`. Google Cloud client libraries typically place the client in a versioned submodule.
fix
Correct the import statement to `from google.cloud import service_usage_v1` and then instantiate `client = service_usage_v1.ServiceUsageClient()`.
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or provide a Google Cloud project ID.
The Python client library could not find any default credentials to authenticate with Google Cloud. This usually means `GOOGLE_APPLICATION_CREDENTIALS` is not set or `gcloud auth application-default login` hasn't been run.
fix
For local development, run `gcloud auth application-default login`. For production, ensure the `GOOGLE_APPLICATION_CREDENTIALS` environment variable points to a valid service account key file path.
google.api_core.exceptions.NotFound: 404 Service not found: services/invalid.service.name
The service name provided in the request (e.g., to `enable_service` or `get_service`) is incorrect or refers to a service that does not exist or is not associated with the project.
fix
Verify the exact service name (e.g., `compute.googleapis.com`, `storage.googleapis.com`) using the Google Cloud Console Service Usage page or by programmatically listing available services.
Upgrade
Version history
1.17.0latest on PyPI · released Jun 3, 2026
Audit
Dependencies
google-api-corerequiredCore dependency for Google Cloud client libraries, providing common functionality and RPC mechanisms.
grpciorequiredRequired for gRPC-based communication with the Google Cloud API endpoints.
Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources
google-cloud-service-usage — pip install google-cloud-service-usage · libregistry