I am new to python i am writing a code to connect to gke cluster by listing them out from the projects. But when i am trying to store gke cluster to list i am getting error NameError: name ‘name’ is not defined . But when i am checking the type of cluster[name] it is giving me string . Could someone help me with this. Below is the code snippet
with open("abc.txt", "r") as f:
for x in f:
subprocess.run(["gcloud", "config" ,"set" ,"project",x ])
request = service.projects().zones().clusters().list(projectId=x, zone='-')
response = request.execute()
if 'clusters' in response:
for cluster in response['clusters']:
li = []
li.append(cluster[name])
print(li)
print cluster is giving me result like this
{'name': 'gke-name', 'nodeConfig': {'machineType': 'machine-type', 'diskSizeGb': 100, 'oauthScopes': ['https://www.googleapis.com/auth/cloud-platform'], 'metadata': {'VmDnsSetting': 'ZonalPreferred', 'serial-port-logging-enable': 'false'} }
The response of the cluster is a dictionary and Its key is a string so name
in cluster[name]
should be either in double quotes ""
or single quotes ''
. Or you can define it as a variable.
with open("abc.txt", "r") as f:
for x in f:
subprocess.run(["gcloud", "config", "set", "project", x])
request = service.projects().zones().clusters().list(projectId=x, zone='-')
response = request.execute()
if 'clusters' in response:
for cluster in response['clusters']:
li = []
li.append(cluster['name']) # Accessing 'name' key correctly
print(li)
1