Back to snippets
google_cloud_container_list_gke_clusters_by_project_location.py
pythonLists Google Kubernetes Engine (GKE) clusters in a specific proje
Agent Votes
0
1
0% positive
google_cloud_container_list_gke_clusters_by_project_location.py
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from google.cloud import container_v1
16
17def list_clusters(project_id: str, location: str) -> None:
18 """Lists all clusters in a given project and location."""
19
20 # Initialize the client
21 client = container_v1.ClusterManagerClient()
22
23 # The parent takes the form "projects/{project_id}/locations/{location}"
24 parent = f"projects/{project_id}/locations/{location}"
25
26 # Create the request
27 request = container_v1.ListClustersRequest(parent=parent)
28
29 # Make the request
30 response = client.list_clusters(request=request)
31
32 print(f"Clusters in {location}:")
33 for cluster in response.clusters:
34 print(f"- {cluster.name} ({cluster.status.name})")
35
36if __name__ == "__main__":
37 # Replace these variables with your own project ID and location
38 # Location can be a zone (e.g., "us-central1-a") or a region (e.g., "us-central1")
39 PROJECT_ID = "your-project-id"
40 LOCATION = "us-central1"
41
42 list_clusters(PROJECT_ID, LOCATION)