【问题标题】:Get Instance Images name with the gcloud python api使用 gcloud python api 获取实例图像名称
【发布时间】:2019-06-06 13:07:30
【问题描述】:
如何借助 python 中的 gcloud 计算引擎 API 检索实例的图像名称?
当我列出我的实例时
compute.instances().list(project=project, zone=zone , filter ='status eq '+ instance_status).execute()
响应没有给我实例图像名称 (debian-9)。
【问题讨论】:
标签:
python
google-compute-engine
【解决方案1】:
要使用 Python API 获取源图像,请使用以下请求:
request = service.disks().get(project=project, zone=zone, disk=disk)
response = request.execute()
输出正文具有 'sourceImage' 字段以获取更多信息,请参阅公共 documentation 以及查看示例:
from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
# Project ID for this request.
project = 'my-project' # TODO: Update placeholder value.
# The name of the zone for this request.
zone = 'my-zone' # TODO: Update placeholder value.
# Name of the persistent disk to return.
disk = 'my-disk' # TODO: Update placeholder value.
request = service.disks().get(project=project, zone=zone, disk=disk)
response = request.execute()
# TODO: Change code below to process the `response` dict:
pprint(response)