Hi I am new to google cloud ,and I want to print vm instance in json format, but print(json.dumps(instance))
raised an error: TypeError: Object of type Instance is not JSON serializable
.
My code is below:
import json
from google.oauth2 import service_account
from google.cloud import compute_v1
class VM:
def __init__(self, cred_json_path):
self.cred_json_path = cred_json_path
self.credentials = self.create_credentials()
self.page_size = 500
def create_credentials(self):
return service_account.Credentials.from_service_account_file(self.cred_json_path)
def list_vms(self):
client = compute_v1.InstancesClient(credentials=self.credentials)
for zone, instances in client.aggregated_list(request={"project": self.credentials.project_id, "max_results": self.page_size}):
for instance in instances.instances:
print(json.dumps(instance))
return
vm = VM("/tmp/1.json")
vm.list_vms()
Is there any easy way to do this? I think gcp api should have some method that I can easy to achieve this, but I cannot find. Thanks for help.