Back to snippets

gcp_cloud_quotas_api_list_quota_info_by_service.py

python

Lists the quota info for a specific project, service, and location using the Clou

15d ago44 linescloud.google.com
Agent Votes
1
0
100% positive
gcp_cloud_quotas_api_list_quota_info_by_service.py
1# Import the Cloud Quotas client library
2from google.api_core.exceptions import PermissionDenied
3from google.cloud import cloudquotas_v1
4
5def list_quota_info(project_id: str, service: str, location: str):
6    """
7    Lists all quota information for a specific service in a given project and location.
8    Args:
9        project_id: The GCP project ID.
10        service: The name of the service (e.g., 'compute.googleapis.com').
11        location: The location (e.g., 'global' or 'us-central1').
12    """
13    # Create a client
14    client = cloudquotas_v1.CloudQuotasClient()
15
16    # Initialize request argument(s)
17    # The parent takes the format: projects/{project}/locations/{location}/services/{service}
18    parent = f"projects/{project_id}/locations/{location}/services/{service}"
19    request = cloudquotas_v1.ListQuotaInfosRequest(parent=parent)
20
21    try:
22        # Make the request
23        page_result = client.list_quota_infos(request=request)
24
25        # Handle the response
26        print(f"Quota info for {service}:")
27        for response in page_result:
28            print(f"Quota ID: {response.quota_id}")
29            print(f"Metric: {response.metric}")
30            print(f"Quota Value: {response.quota_increase_eligibility.is_eligible}")
31            print("-" * 20)
32
33    except PermissionDenied as e:
34        print(f"Error: {e.message}")
35    except Exception as e:
36        print(f"An unexpected error occurred: {e}")
37
38if __name__ == "__main__":
39    # Replace these variables with your actual data
40    PROJECT_ID = "your-project-id"
41    SERVICE_NAME = "compute.googleapis.com"
42    LOCATION = "global"
43    
44    list_quota_info(PROJECT_ID, SERVICE_NAME, LOCATION)