Google Cloud Service Usage API client library for managing Google Cloud services programmatically
—
Query and discovery operations for listing services, retrieving service configurations, and batch information retrieval across Google Cloud projects.
Retrieves detailed information about a specific Google Cloud service, including its configuration and current state.
def get_service(
request: GetServiceRequest,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Optional[float] = None,
metadata: Sequence[Tuple[str, str]] = ()
) -> Service:
"""
Get information about a specific service.
Args:
request: The request object containing the service name
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata for the request
Returns:
Service object with full configuration details
Raises:
google.api_core.exceptions.NotFound: If the service doesn't exist
google.api_core.exceptions.GoogleAPICallError: If the request fails
"""Usage example:
from google.cloud import service_usage
client = service_usage.ServiceUsageClient()
# Get detailed information about a service
service_name = "projects/my-project-id/services/storage.googleapis.com"
request = service_usage.GetServiceRequest(name=service_name)
service = client.get_service(request=request)
print(f"Service: {service.name}")
print(f"Title: {service.config.title}")
print(f"State: {service.state}")
print(f"Documentation: {service.config.documentation.summary}")
# Access service endpoints
for endpoint in service.config.endpoints:
print(f"Endpoint: {endpoint.name} - {endpoint.target}")Lists all services available to a project with optional filtering and pagination support.
def list_services(
request: ListServicesRequest,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Optional[float] = None,
metadata: Sequence[Tuple[str, str]] = ()
) -> ListServicesPager:
"""
List services available to a project.
Args:
request: The request object with parent, filter, and pagination options
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata for the request
Returns:
Paginated iterator over Service objects
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request fails
"""Usage example:
from google.cloud import service_usage
client = service_usage.ServiceUsageClient()
# List all services for a project
parent = "projects/my-project-id"
request = service_usage.ListServicesRequest(parent=parent)
# Iterate through all services
for service in client.list_services(request=request):
print(f"Service: {service.name}")
print(f"State: {service.state.name}")
# List only enabled services with filter
request_enabled = service_usage.ListServicesRequest(
parent=parent,
filter="state:ENABLED"
)
enabled_services = client.list_services(request=request_enabled)
print("Enabled services:")
for service in enabled_services:
print(f" {service.config.title}")
# Paginated listing with page size
request_paged = service_usage.ListServicesRequest(
parent=parent,
page_size=10
)
pager = client.list_services(request=request_paged)
for page in pager.pages:
print(f"Page with {len(page.services)} services:")
for service in page.services:
print(f" {service.name}")Retrieves information about multiple services in a single request, more efficient than individual get operations.
def batch_get_services(
request: BatchGetServicesRequest,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Optional[float] = None,
metadata: Sequence[Tuple[str, str]] = ()
) -> BatchGetServicesResponse:
"""
Get information about multiple services in a single request.
Args:
request: The request object containing parent and service names
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata for the request
Returns:
BatchGetServicesResponse with list of services
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request fails
"""Usage example:
from google.cloud import service_usage
client = service_usage.ServiceUsageClient()
# Get information about multiple services at once
parent = "projects/my-project-id"
service_names = [
f"{parent}/services/storage.googleapis.com",
f"{parent}/services/compute.googleapis.com",
f"{parent}/services/bigquery.googleapis.com"
]
request = service_usage.BatchGetServicesRequest(
parent=parent,
names=service_names
)
response = client.batch_get_services(request=request)
print(f"Retrieved {len(response.services)} services:")
for service in response.services:
print(f"Service: {service.config.title}")
print(f" Name: {service.name}")
print(f" State: {service.state.name}")
print(f" APIs: {len(service.config.apis)}")class GetServiceRequest:
"""Request to get a specific service."""
name: str # Format: "projects/{project}/services/{service}"
class ListServicesRequest:
"""Request to list services."""
parent: str # Format: "projects/{project}"
page_size: int # Maximum number of services per page
page_token: str # Token for pagination
filter: str # Filter expression (e.g., "state:ENABLED")
class BatchGetServicesRequest:
"""Request to get multiple services."""
parent: str # Format: "projects/{project}"
names: List[str] # Full service resource namesclass ListServicesResponse:
"""Response from listing services."""
services: List[Service] # List of services
next_page_token: str # Token for next page
class BatchGetServicesResponse:
"""Response from batch getting services."""
services: List[Service] # List of requested servicesclass ListServicesPager:
"""Pager for iterating through list_services results."""
def __iter__(self) -> Iterator[Service]:
"""Iterate through Service objects."""
@property
def pages(self) -> Iterator[ListServicesResponse]:
"""Iterate through response pages."""
class ListServicesAsyncPager:
"""Async pager for iterating through list_services results."""
def __aiter__(self) -> AsyncIterator[Service]:
"""Async iterate through Service objects."""
@property
def pages(self) -> AsyncIterator[ListServicesResponse]:
"""Async iterate through response pages."""The filter parameter in ListServicesRequest supports the following expressions:
state:ENABLED - Only enabled servicesstate:DISABLED - Only disabled servicesstate:STATE_UNSPECIFIED - Services with unspecified stateMultiple filters can be combined with logical operators:
state:ENABLED OR state:DISABLED - All services with known stateInstall with Tessl CLI
npx tessl i tessl/pypi-google-cloud-service-usage