0

I am trying to extract cost of our GCP cloud using api. As a first step, I am just trying to extract list of instances in given zone. However I get following only, without instances information:

XXX-MBP:gcp XXX$ gcloud beta auth application-default login Your browser has been opened to visit:

https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&prompt=select_account&response_type=code&client_id=XXX-XXX.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&access_type=offline

Credentials saved to file: [/Users/XXX/.config/gcloud/application_default_credentials.json]

These credentials will be used by any library that requests Application Default Credentials.

To generate an access token for other uses, run: gcloud auth application-default print-access-token XXX-MBP:gcp XXX$

My code is as follows

from pprint import pprint

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('compute', 'v1', credentials=credentials)

# Project ID for this request.
project = 'XXX'  # TODO: Update placeholder value.

# The name of the zone for this request.
zone = 'us-central1'  # TODO: Update placeholder value.

request = service.instances().list(project=project, zone=zone)
while request is not None:
    response = request.execute()

    for instance in response['items']:
        # TODO: Change code below to process each `instance` resource:
        pprint(instance)

    request = service.instances().list_next(previous_request=request, previous_response=response)

I was expecting list of vm in my account.

Can someone help please?

techele
  • 165
  • 3
  • 8
  • have you gone through the auth steps mentioned in the error message? – Elad Amit Aug 11 '19 at 20:41
  • Yes I did. I executed this command gcloud beta auth application-default login, I think this will help you login and then execute the script application-default. The command pops up browser authenticate and then given message mentioned in the initial post. I don't think it is executing correctly as there are instances in that region. – techele Aug 12 '19 at 02:52
  • not sure I follow, after you go through the login flow, what is the output of the script? what is the output of running the gcloud cli equivalent? – Elad Amit Aug 12 '19 at 10:27

0 Answers0