Service Usage¶
Service Usage is a Google Cloud service that allows you to manage which APIs are enabled on your Google Cloud projects. The ServiceUsage
class in gcp-pilot provides a high-level interface for interacting with Google Cloud Service Usage API.
Installation¶
To use the Service Usage functionality, you need to install gcp-pilot:
Usage¶
Initialization¶
Initialize ServiceUsage Client
from gcp_pilot.service_usage import ServiceUsage
service_usage = ServiceUsage() # (1)!
service_usage = ServiceUsage(project_id="my-project") # (2)!
service_usage = ServiceUsage(impersonate_account="service-account@project-id.iam.gserviceaccount.com") # (3)!
- Initialize with default credentials
- Initialize with specific project
- Initialize with service account impersonation
Managing Services¶
Listing Services¶
List Services
from gcp_pilot.service_usage import ServiceUsage, ServiceStatus
for service in service_usage.list_services(): # (1)!
print(f"Service: {service['name']}")
for service in service_usage.list_services(project_id="my-project"): # (2)!
print(f"Service: {service['name']}")
for service in service_usage.list_services(status=ServiceStatus.DISABLED): # (3)!
print(f"Service: {service['name']}")
- List all enabled services in a project
- List all services in a specific project
- List all disabled services
Getting a Service¶
Get a Service
service = service_usage.get_service( # (1)!
service_name="compute.googleapis.com",
project_id="my-project", # (2)!
)
- Get information about a specific service
- Optional: defaults to the project associated with credentials
Enabling a Service¶
Enable a Service
service_usage.enable_service( # (1)!
service_name="compute.googleapis.com",
project_id="my-project", # (2)!
)
- Enable a service in a project
- Optional: defaults to the project associated with credentials
Disabling a Service¶
Disable a Service
service_usage.disable_service( # (1)!
service_name="compute.googleapis.com",
project_id="my-project", # (2)!
)
- Disable a service in a project
- Optional: defaults to the project associated with credentials
Error Handling¶
The ServiceUsage class handles common errors and converts them to more specific exceptions: