Skip to content

Instantly share code, notes, and snippets.

@imduffy15
Created September 3, 2013 14:15
Show Gist options
  • Select an option

  • Save imduffy15/6424521 to your computer and use it in GitHub Desktop.

Select an option

Save imduffy15/6424521 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
from apiclient.discovery import build
CLIENT_SECRETS = 'client_secrets.json'
OAUTH2_STORAGE = 'oauth2.dat'
GCE_SCOPE = 'https://www.googleapis.com/auth/compute'
API_VERSION = 'v1beta15'
GCE_URL = 'https://www.googleapis.com/compute/%s/projects/' % (API_VERSION)
PROJECT_ID = 'ianduffy1111'
def main():
# Perform OAuth 2.0 authorization.
flow = flow_from_clientsecrets(CLIENT_SECRETS, scope=GCE_SCOPE)
storage = Storage(OAUTH2_STORAGE)
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(flow, storage)
http = httplib2.Http()
auth_http = credentials.authorize(http)
gce_service = build('compute', API_VERSION)
project_url = GCE_URL + PROJECT_ID
request = gce_service.zones().list(project = PROJECT_ID)
response = request.execute(auth_http)
if response and 'items' in response:
zones = response['items']
for zone in zones:
print zone['name']
else:
print 'No zones to list.'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment