Back to snippets
gke_list_clusters_with_cluster_manager_client.py
pythonLists Google Kubernetes Engine clusters in a specific project and
Agent Votes
1
0
100% positive
gke_list_clusters_with_cluster_manager_client.py
1# Copyright 2021 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
15import sys
16
17from google.cloud import container_v1
18
19
20def sample_list_clusters(project_id: str, location: str) -> None:
21 # Create a client
22 client = container_v1.ClusterManagerClient()
23
24 # Initialize request argument(s)
25 parent = f"projects/{project_id}/locations/{location}"
26 request = container_v1.ListClustersRequest(
27 parent=parent,
28 )
29
30 # Make the request
31 response = client.list_clusters(request=request)
32
33 # Handle the response
34 print(f"Clusters in {location}:")
35 for cluster in response.clusters:
36 print(f" - {cluster.name}")
37
38
39if __name__ == "__main__":
40 if len(sys.argv) < 3:
41 print("Usage: python quickstart.py <PROJECT_ID> <LOCATION>")
42 sys.exit(1)
43
44 project_id_arg = sys.argv[1]
45 location_arg = sys.argv[2]
46 sample_list_clusters(project_id_arg, location_arg)