简体   繁体   中英

Python gcloud api client : How to get instance price, uptime and name of user that created a particular instance

Am using python googleapi client library to get instance data for a project

Am getting instances like this:

from googleapiclient import discovery
from google.oauth2 import service_account

scopes = ['https://www.googleapis.com/auth/cloud-platform']
service_cred_file = 'service-credentials.json'
zone = 'us-central1-c'
project_id = 'my_project_id'

credentials = service_account.Credentials.from_service_account_file(service_cred_file, scopes=scopes)

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

request = service.instances().list(project=project_id, zone=zone)
while request is not None:
    response = request.execute()
    for instance in response['items']:
        print(instance)

Instance response does not contain instance price, uptime and user data (data of user that created instance) How can i get these attributes?

The shortest answer to your question is that, currently, it is not possible to get this information from the “compute” API.

However, there are other APIs which can give you this information, even if not as easy as just retrieving a property from an instance.

For uptime and user data you could use the Monitoring Client Library .

For user data you can use this client library to look for the logs of the “insert” protoPayload.methodName for a specific instance, and get the information about the user from the protoPayload.authenticationInfo property.

To get information about the uptime, you would need to set uptime checks and calculate the uptime from the logs generated by the check you created.

For information about pricing however, it's not possible to do the same.

I was looking through different possible solutions and I even found the page for the Cloud Billing API , which has a skus() method, however since the documentation is scarce, and there is no filter for specific instances as resources, this would probably be even harder to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM