Code execution hangs while trying to retrieve disk details from GCP cloud using discovery method?

Viewed 62
from google.oauth2 import service_account
from googleapiclient import discovery


def main():

    cred = service_account.Credentials.from_service_account_file("json file path")

    service = discovery.build("compute", "v1", credentials=cred)
    request = service.disks().list(project="projectid", zone="us-central1-a")
    print(request)
    while request is not None:
        response = request.execute()
    print(response)
    for disk in response["items"]:
        # TODO: Change code below to process each `disk` resource:
        print(disk)

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


if __name__ == "__main__":
    main()

The code is successfully able to print(request) data after which there is no result. I have as disk created in the project.

1 Answers

You have a while loop in the code without the termination clause. Thats where its getting stuck. You could rewrite that part in the following way:

while request is not None:
    response = request.execute()

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

    request = service.disks().list_next(previous_request=request, previous_response=response)
Related