0

I am attempting to use googleapiclient.discovery.build('compute', 'v1').instances() to generate a list of devices. The below works fine but I want to know if there is a way I can pass format "json(disks,networkInterfaces,machineType,scheduling)" through cmdCompute.instances().list or even csv format. The current output is really messy.

I have gone through the docs but cannot seem so see a way. I know I can do it with gcloud but I am not able to use gcloud in a gcloud function which is where this script is going to be executed. The file will be used by other cloud functions so I would like a cleaner output.

` from datetime import datetime, timedelta from google.cloud import storage

from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials

def fncListVMs(self):
    strHostProjectID='project'
    strZone='zone'
    strBucketName = 'bucketname'

    credentials = GoogleCredentials.get_application_default()
    cmdCompute=build('compute', 'v1',credentials=credentials, static_discovery=False)

    strOutput = cmdCompute.instances().list(project=strHostProjectID, zone=strZone,filter=    ('status=RUNNING')).execute()
    strFile = strHostProjectID+"_Instance.json"

    stoClient = storage.Client()
    strBucket=stoClient.get_bucket(strBucketName)
    strBlob = strBucket.blob('scripts/output/'+strFile)
    with strBlob.open(mode='w') as strTarget:
        strTarget.write(str(strOutput))

    return "Done"

`

0 Answers0