亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

列出谷歌云計算引擎活動實例

列出谷歌云計算引擎活動實例

慕沐林林 2023-11-09 21:44:44
我正在尋找所有活動資源(如計算引擎、gke 等)和各自的區域。我嘗試使用下面的 python 代碼來打印它,但它會在計算引擎可用的地方打印所有區域信息,請有人指導我可以使用哪些功能來執行此操作。    compute = googleapiclient.discovery.build('compute', 'v1')    request = compute.instances().aggregatedList(project=project)    while request is not None:        response = request.execute()        for name, instances_scoped_list in response['items'].items():            pprint((name, instances_scoped_list))            request = compute.instances().aggregatedList_next(previous_request=request, previous_response=response)
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

gcloud compute instances list?您可以使用 Cloud Console命令或instances.list()?方法列出項目中的所有實例?。

要以表格形式列出項目中的所有實例,請運行:

gcloud compute instances list

你會得到類似的東西:

NAME? ? ? ? ZONE? ? ? ? ? ?MACHINE_TYPE? ?PREEMPTIBLE? INTERNAL_IP? EXTERNAL_IP? ?STATUS

instance-1? us-central1-a? n1-standard-1? ? ? ? ? ? ? ?10.128.0.44? xx.xx.xxx.xx? RUNNING

instance-2? us-central1-b? n1-standard-1? ? ? ? ? ? ? ?10.128.0.49? xx.xx.xxx.xx? RUNNING

編輯1

正如您所提到的,aggregateList()是正確的,要獲取所需的信息,必須檢查 JSON 響應正文。

如果您需要某些特定字段,您可以檢查響應正文信息。

另外,您可以使用此代碼作為指南,我正在從實例中獲取所有信息。

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 = "{Project-ID}"? # TODO: Update placeholder value.

request = service.instances().aggregatedList(project=project)

while request is not None:

? ? response = request.execute()

? ? instance = response.get('items', {})

? ? for instance in instance.values():

? ? ? for a in instance.get('instances', []):

? ? ? ? ? print(str(instance))

? ? request = service.instances().aggregatedList_next(previous_request=request, previous_response=response)



查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號