In [ ]:
#gcloud version
!gcloud -v
In [ ]:
#gcloud config
!gcloud config list
In [ ]:
#list projects
!gcloud projects list --format='[box]'
In [ ]:
#list the project in a more firendly way for python ;-)
!gcloud projects list --format='value[no-heading](name,project_ID)'
In [ ]:
#put the project list in a dictionnary
projects=!gcloud projects list --format='value[no-heading](name,project_ID)'
projectsDict={project[0]:project[1] for project in [line.split('\t')for line in projects.n.split('\n')]}
In [ ]:
# choose the projectId with a nice widget
import ipywidgets as widgets
#projectId is the variable that will contains the projectId that will be used in the API calls
projectId=None
projectsDict['None']='invalid'
#the dropdownlist widget
projectWidget=widgets.Dropdown(options=projectsDict,description='Choose your Project',value='invalid')
#a valid widget that get valid when a project is selected
projectIdValid=widgets.Valid(value=False,description='')
display(widgets.Box([projectWidget,projectIdValid]))
def projectValueChange(sender):
if projectWidget.value!='invalid':
#when a valid project is selected ,the gloabl variable projectId is set
projectIdValid.value=True
projectIdValid.description=projectWidget.value
global projectId
projectId=projectWidget.value
else:
projectIdValid.value=False
projectIdValid.description=''
projectWidget.observe(projectValueChange, 'value')
In [ ]:
#we set the project in the gcloud config
!gcloud config set project {projectId}
In [ ]:
#we check it's correct
!gcloud config list
In [1]:
#we can now use gcloud without passing the project in the parameters!
!gcloud compute zones list
In [ ]: