如要查看 VM 執行個體與其他 Compute Engine 資源之間的關係,請使用 listReferrers
REST API 方法。
使用 REST 時,Compute Engine 會傳回一份參考資料清單,當中會說明:
- 來源資源:指向目標資源的資源
- 目標資源:相關資源
- 參照類型:兩個資源之間的關係
舉例來說,您可以使用 REST 輕鬆檢視某個 VM 執行個體所屬的執行個體群組清單。
事前準備
-
如果尚未設定,請先設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,以便對 Compute Engine 進行驗證:
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「驗證以使用 REST」。
限制
- 您只能列出 VM 執行個體與執行個體群組 (包含代管和未代管) 之間的關係。
- 您只能使用 REST 列出參照來源。
列出單一參照網址
參照網址是指參照其他資源的資源。對 VM 執行個體來說,共用參照網址就是執行個體群組。
舉例來說,假設您在 us-central1-a
區域有個名為 example-ig-a1
的 VM 執行個體。該 VM 執行個體是同一區域中「example-ig
」執行個體群組的成員,如下圖所示:
如要查看這兩者之間的關係,請透過以下 HTTP 要求對 example-ig-a1
呼叫 listReferrers
方法:
GET https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-ig-a1/referrers
伺服器會傳回標準清單回應,其中包含以下項目:
"items": [
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-ig-a1,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroups/example-ig
}
]
在這個範例中,target
是 example-ig-a1
,也就是 MEMBER_OF
的 referrer
,也就是 example-ig instance
群組。
列出多個參照網址
如果某個資源的參照網址不只一個,回應會傳回目標資源的所有參照網址清單。如果某個執行個體屬於兩個執行個體群組,回應就會包含這兩個執行個體群組。
舉例來說,下列回應指出名為 example-instance-a2
的執行個體屬於 example-ig
和 example-ig-2
這兩個執行個體群組:
"items": [
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-instance-a2,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroups/example-ig
},
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-instance-a2,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroups/example-ig-2
}
]
列出跨範圍的參照網址
listReferrers
方法也會傳回位於其他範圍的參照網址相關資訊,例如其他地區或區域。舉例來說,假設您設有一個 VM 執行個體,隸屬於地區代管的執行個體群組 (地區 MIG):
您呼叫的 listReferrers
方法會使用下列 HTTP 要求將這個執行個體指定做為目標:
GET https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/new-instance-a3/referrers
伺服器會傳回標準清單回應,其中包含以下項目:
"items": [
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/new-instance-a3,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/regions/us-central1/instanceGroups/example-rmig
}
]
回應會在參照網址清單中顯示地區執行個體群組。
列出集合中所有資源的參照網址
只要使用萬用字元 (-
),您就能要求特定區域內所有 VM 執行個體的所有參照網址清單。您可以透過 HTTP 要求發出這項要求,格式如下:
GET https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/-/referrers
伺服器會傳回回應,其中包含區域中的 VM 執行個體清單,以及執行個體的參考資料。範例如下:
"items": [
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-ig-a1,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroups/example-ig
},
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-ig-a2,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroups/example-ig
},
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-ig-a2,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroups/example-ig-2
},
{
"target": https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/new-instance-a3,
"referenceType": MEMBER_OF,
"referrer": https://compute.googleapis.com/compute/v1/projects/myproject/regions/us-central1/instanceGroups/example-rmig
}
]