查看 VM 執行個體的參照網址


如要查看 VM 執行個體與其他 Compute Engine 資源之間的關係,請使用 listReferrers REST API 方法

使用 REST 時,Compute Engine 會傳回一份參考資料清單,當中會說明:

  • 來源資源:指向目標資源的資源
  • 目標資源:相關資源
  • 參照類型:兩個資源之間的關係

舉例來說,您可以使用 REST 輕鬆檢視某個 VM 執行個體所屬的執行個體群組清單。

事前準備

  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

限制

  • 您只能列出 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
  }
]

在這個範例中,targetexample-ig-a1,也就是 MEMBER_OFreferrer,也就是 example-ig instance 群組。

列出多個參照網址

如果某個資源的參照網址不只一個,回應會傳回目標資源的所有參照網址清單。如果某個執行個體屬於兩個執行個體群組,回應就會包含這兩個執行個體群組。

如果虛擬機器執行個體是兩個執行個體群組的成員,回應就會包含這兩個執行個體群組。

舉例來說,下列回應指出名為 example-instance-a2 的執行個體屬於 example-igexample-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):

在 us-central1 區域中,us-central1-a 區域中的 new-instance-a3 執行個體是 example-rmig 區域執行個體群組的成員。

您呼叫的 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
  }
]

後續步驟