{"id":7266,"library":"google-cloud-service-usage","title":"Google Cloud Service Usage","description":"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.","status":"active","version":"1.16.0","language":"en","source_language":"en","source_url":"https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-service-usage","tags":["google-cloud","gcp","service-usage","api-management","cloud-api"],"install":[{"cmd":"pip install google-cloud-service-usage","lang":"bash","label":"Install library"}],"dependencies":[{"reason":"Core dependency for Google Cloud client libraries, providing common functionality and RPC mechanisms.","package":"google-api-core","optional":false},{"reason":"Required for gRPC-based communication with the Google Cloud API endpoints.","package":"grpcio","optional":false}],"imports":[{"note":"The client class is typically found within a versioned submodule (e.g., `_v1`) to allow for API versioning.","wrong":"from google.cloud import service_usage","symbol":"ServiceUsageClient","correct":"from google.cloud import service_usage_v1"}],"quickstart":{"code":"import os\nfrom google.cloud import service_usage_v1\n\n# Set your Google Cloud Project ID\nproject_id = os.environ.get(\"GOOGLE_CLOUD_PROJECT\", \"your-gcp-project-id\") # Replace with a valid project ID\n\n# IMPORTANT: Ensure you have authenticated to GCP.\n# For local development, run `gcloud auth application-default login`.\n# For service accounts, set the GOOGLE_APPLICATION_CREDENTIALS environment variable.\n\ntry:\n    # Initialize the Service Usage client\n    client = service_usage_v1.ServiceUsageClient()\n    \n    # Construct the parent resource name\n    parent = f\"projects/{project_id}\"\n    \n    print(f\"Listing enabled services for project: {project_id}\")\n    \n    # Create a request to list services, filtered by enabled state\n    request = service_usage_v1.ListServicesRequest(\n        parent=parent,\n        filter=\"state:ENABLED\"\n    )\n    \n    # Make the API call\n    response = client.list_services(request=request)\n    \n    enabled_services = [service.config.name for service in response.services]\n    \n    if enabled_services:\n        print(f\"Enabled services found ({len(enabled_services)}):\")\n        for service_name in enabled_services[:5]: # Print first 5 for brevity\n            print(f\"- {service_name}\")\n        if len(enabled_services) > 5:\n            print(f\"... and {len(enabled_services) - 5} more.\")\n    else:\n        print(\"No enabled services found for this project or insufficient permissions.\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please ensure:\")\n    print(\"1. Your project ID is correct.\")\n    print(\"2. You are authenticated (e.g., `gcloud auth application-default login`).\")\n    print(\"3. The authenticated principal has the 'Service Usage Consumer' IAM role or equivalent.\")\n","lang":"python","description":"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."},"warnings":[{"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.","message":"Google 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Interacting with Google Cloud services requires specific IAM permissions. Common errors include 'Permission Denied'.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"Resource 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.","severity":"gotcha","affected_versions":"All versions"},{"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()`.","message":"Operations like `enable_service` and `disable_service` return a `google.api_core.operation.Operation` object, not the final result. These are long-running operations.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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.","cause":"The authenticated principal (user or service account) does not have the necessary IAM permissions to perform the requested action on the specified project.","error":"google.api_core.exceptions.PermissionDenied: 403 Permission 'serviceusage.services.list' denied on resource 'projects/your-gcp-project-id'."},{"fix":"Correct the import statement to `from google.cloud import service_usage_v1` and then instantiate `client = service_usage_v1.ServiceUsageClient()`.","cause":"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.","error":"AttributeError: module 'google.cloud' has no attribute 'service_usage'"},{"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.","cause":"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.","error":"google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or provide a Google Cloud project ID."},{"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.","cause":"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.","error":"google.api_core.exceptions.NotFound: 404 Service not found: services/invalid.service.name"}]}