0

I use python sdk of gcp to get compute engine list and I want to judge the the operating system of google compute engine is linux or windows, but there is no any operating system informatioin in all fields of compute engine.

import sys
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_instance(self):
        compute_client = compute_v1.InstancesClient(credentials=self.credentials)
        results = []
        for _, instances in compute_client.aggregated_list(request={"project": self.credentials.project_id, "max_results": self.page_size}):
            for instance in instances.instances:
                # TODO 
                # I want to judge the operating system information for every instance is linux or windows here.
                results.append(instance)
        return results

    def show(self):
        instance_list = self.list_instance()
        for instance in instance_list:
            print(json.dumps(instance))


def main(cred_json_path):
    vm = VM(cred_json_path)
    vm.show()


if __name__ == '__main__':
    main(sys.argv[1])

Is there any solution? Thanks.

leo
  • 1,045
  • 3
  • 15
  • 27

0 Answers0