建立 API 請求和處理回應

本文說明如何建構 API 要求,以及處理 Compute Engine API 的 API 回應。涵蓋下列工作:

  • 如何建構要求內文。
  • 如何決定要求所需的資源 URI。
  • 如何處理 API 回應。
  • 判斷 API 要求是否成功。

本文不說明如何向 API 進行驗證。如要瞭解如何向 API 進行驗證,請參閱「向 Compute Engine 進行驗證」。

事前準備

建立 API 要求

Compute Engine API 預期 API 要求主體採用 JSON 格式。如要提出 API 要求,您可以使用 curl 或 httplib2 等工具直接提出 HTTP 要求,也可以使用可用的用戶端程式庫。

提出直接 HTTP 要求時,您可以指定伺服器必須回應的版本。建議您指定 API 版本,這樣應用程式會更穩定,您也能控管何時採用新的服務功能。

對於需要要求主體的 API 要求 (例如 POST、PUT 或 PATCH 要求),要求主體會包含您要在這項要求中設定的資源屬性。

API 要求範例

下列 curl 指令會使用 API 版本 2026-09-01,向 instances.insert 方法發出 POST 要求。這項要求會建立執行個體,並在網址中定義專案和可用區參數,以及在要求主體中定義執行個體的屬性。要求主體會以 -d 旗標表示。如要指定 API 版本,可以使用 $apiVersion 查詢參數或 X-Goog-Api-Version 標頭。詳情請參閱 AIP-184。

查詢參數中的版本

如要在查詢參數中指定 API 版本,請加入 %24apiVersion 查詢參數和所選 API 版本。

curl -X POST -H "Authorization: Bearer [OAUTH_TOKEN]" \
-H "Content-Type: application/json" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances?%24apiVersion=2026-09-01" -d \
'{
"disks":[
  {
    "boot":"true",
    "initializeParams":{
      "sourceImage":"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20210122"
    }
  }
],
"machineType":"https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/e2-standard-2",
"name":"VM_NAME",
"networkInterfaces":[
  {
    "accessConfigs":[
      {
        "name":"external-nat",
        "type":"ONE_TO_ONE_NAT"
      }
    ],
    "network":"https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/default"
  }
]
}'

標頭中的版本

如要在標頭中指定 API 版本,請加入 X-Goog-Api-Version 標頭和所選 API 版本。

curl -X POST -H "Authorization: Bearer [OAUTH_TOKEN]" \
-H "Content-Type: application/json" \
-H "X-Goog-Api-Version: 2026-09-01" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances" -d \
'{
"disks":[
  {
    "boot":"true",
    "initializeParams":{
      "sourceImage":"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20210122"
    }
  }
],
"machineType":"https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/e2-standard-2",
"name":"VM_NAME",
"networkInterfaces":[
  {
    "accessConfigs":[
      {
        "name":"external-nat",
        "type":"ONE_TO_ONE_NAT"
      }
    ],
    "network":"https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/default"
  }
]
}'

映像檔 URI 的專案 ID (debian-cloud) 與您的專案 ID 不同,因為映像檔屬於不同專案,具體取決於映像檔類型。舉例來說,Compute Engine 提供的所有公開 Debian 映像檔,都託管在 debian-cloud 專案中。

參照其他資源時,請使用完整的資源 URI。舉例來說,network 屬性會使用 default 網路的完整 URI。

使用用戶端程式庫發出 API 要求

用戶端程式庫可讓您更輕鬆地建構 API 要求。支援穩定版和預先發布版的 API。預先發布版 API 支援某些語言,做為預先發布版用戶端版本,並加上 -preview.1 或 -rc.1 等後置字串。

Python

from __future__ import annotations

import re
import sys
from typing import Any
import warnings

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def get_image_from_family(project: str, family: str) -> compute_v1.Image:
    """
    Retrieve the newest image that is part of a given family in a project.

    Args:
        project: project ID or project number of the Cloud project you want to get image from.
        family: name of the image family you want to get image from.

    Returns:
        An Image object.
    """
    image_client = compute_v1.ImagesClient()
    # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
    newest_image = image_client.get_from_family(project=project, family=family)
    return newest_image


def disk_from_image(
    disk_type: str,
    disk_size_gb: int,
    boot: bool,
    source_image: str,
    auto_delete: bool = True,
) -> compute_v1.AttachedDisk:
    """
    Create an AttachedDisk object to be used in VM instance creation. Uses an image as the
    source for the new disk.

    Args:
         disk_type: the type of disk you want to create. This value uses the following format:
            "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
            For example: "zones/us-west3-b/diskTypes/pd-ssd"
        disk_size_gb: size of the new disk in gigabytes
        boot: boolean flag indicating whether this disk should be used as a boot disk of an instance
        source_image: source image to use when creating this disk. You must have read access to this disk. This can be one
            of the publicly available images or an image from one of your projects.
            This value uses the following format: "projects/{project_name}/global/images/{image_name}"
        auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it

    Returns:
        AttachedDisk object configured to be created using the specified image.
    """
    boot_disk = compute_v1.AttachedDisk()
    initialize_params = compute_v1.AttachedDiskInitializeParams()
    initialize_params.source_image = source_image
    initialize_params.disk_size_gb = disk_size_gb
    initialize_params.disk_type = disk_type
    boot_disk.initialize_params = initialize_params
    # Remember to set auto_delete to True if you want the disk to be deleted when you delete
    # your VM instance.
    boot_disk.auto_delete = auto_delete
    boot_disk.boot = boot
    return boot_disk


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def create_instance(
    project_id: str,
    zone: str,
    instance_name: str,
    disks: list[compute_v1.AttachedDisk],
    machine_type: str = "n1-standard-1",
    network_link: str = "global/networks/default",
    subnetwork_link: str = None,
    internal_ip: str = None,
    external_access: bool = False,
    external_ipv4: str = None,
    accelerators: list[compute_v1.AcceleratorConfig] = None,
    preemptible: bool = False,
    spot: bool = False,
    instance_termination_action: str = "STOP",
    custom_hostname: str = None,
    delete_protection: bool = False,
) -> compute_v1.Instance:
    """
    Send an instance creation request to the Compute Engine API and wait for it to complete.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        zone: name of the zone to create the instance in. For example: "us-west3-b"
        instance_name: name of the new virtual machine (VM) instance.
        disks: a list of compute_v1.AttachedDisk objects describing the disks
            you want to attach to your new instance.
        machine_type: machine type of the VM being created. This value uses the
            following format: "zones/{zone}/machineTypes/{type_name}".
            For example: "zones/europe-west3-c/machineTypes/f1-micro"
        network_link: name of the network you want the new instance to use.
            For example: "global/networks/default" represents the network
            named "default", which is created automatically for each project.
        subnetwork_link: name of the subnetwork you want the new instance to use.
            This value uses the following format:
            "regions/{region}/subnetworks/{subnetwork_name}"
        internal_ip: internal IP address you want to assign to the new instance.
            By default, a free address from the pool of available internal IP addresses of
            used subnet will be used.
        external_access: boolean flag indicating if the instance should have an external IPv4
            address assigned.
        external_ipv4: external IPv4 address to be assigned to this instance. If you specify
            an external IP address, it must live in the same region as the zone of the instance.
            This setting requires `external_access` to be set to True to work.
        accelerators: a list of AcceleratorConfig objects describing the accelerators that will
            be attached to the new instance.
        preemptible: boolean value indicating if the new instance should be preemptible
            or not. Preemptible VMs have been deprecated and you should now use Spot VMs.
        spot: boolean value indicating if the new instance should be a Spot VM or not.
        instance_termination_action: What action should be taken once a Spot VM is terminated.
            Possible values: "STOP", "DELETE"
        custom_hostname: Custom hostname of the new VM instance.
            Custom hostnames must conform to RFC 1035 requirements for valid hostnames.
        delete_protection: boolean value indicating if the new virtual machine should be
            protected against deletion or not.
    Returns:
        Instance object.
    """
    instance_client = compute_v1.InstancesClient()

    # Use the network interface provided in the network_link argument.
    network_interface = compute_v1.NetworkInterface()
    network_interface.network = network_link
    if subnetwork_link:
        network_interface.subnetwork = subnetwork_link

    if internal_ip:
        network_interface.network_i_p = internal_ip

    if external_access:
        access = compute_v1.AccessConfig()
        access.type_ = compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.name
        access.name = "External NAT"
        access.network_tier = access.NetworkTier.PREMIUM.name
        if external_ipv4:
            access.nat_i_p = external_ipv4
        network_interface.access_configs = [access]

    # Collect information into the Instance object.
    instance = compute_v1.Instance()
    instance.network_interfaces = [network_interface]
    instance.name = instance_name
    instance.disks = disks
    if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type):
        instance.machine_type = machine_type
    else:
        instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}"

    instance.scheduling = compute_v1.Scheduling()
    if accelerators:
        instance.guest_accelerators = accelerators
        instance.scheduling.on_host_maintenance = (
            compute_v1.Scheduling.OnHostMaintenance.TERMINATE.name
        )

    if preemptible:
        # Set the preemptible setting
        warnings.warn(
            "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning
        )
        instance.scheduling = compute_v1.Scheduling()
        instance.scheduling.preemptible = True

    if spot:
        # Set the Spot VM setting
        instance.scheduling.provisioning_model = (
            compute_v1.Scheduling.ProvisioningModel.SPOT.name
        )
        instance.scheduling.instance_termination_action = instance_termination_action

    if custom_hostname is not None:
        # Set the custom hostname for the instance
        instance.hostname = custom_hostname

    if delete_protection:
        # Set the delete protection bit
        instance.deletion_protection = True

    # Prepare the request to insert an instance.
    request = compute_v1.InsertInstanceRequest()
    request.zone = zone
    request.project = project_id
    request.instance_resource = instance

    # Wait for the create operation to complete.
    print(f"Creating the {instance_name} instance in {zone}...")

    operation = instance_client.insert(request=request)

    wait_for_extended_operation(operation, "instance creation")

    print(f"Instance {instance_name} created.")
    return instance_client.get(project=project_id, zone=zone, instance=instance_name)

Java


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.compute.v1.AttachedDisk;
import com.google.cloud.compute.v1.AttachedDisk.Type;
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
import com.google.cloud.compute.v1.InsertInstanceRequest;
import com.google.cloud.compute.v1.Instance;
import com.google.cloud.compute.v1.InstancesClient;
import com.google.cloud.compute.v1.NetworkInterface;
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateInstance {

  public static void main(String[] args)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "your-project-id";
    String zone = "zone-name";
    String instanceName = "instance-name";
    createInstance(project, zone, instanceName);
  }


  // Create a new instance with the provided "instanceName" value in the specified project and zone.
  public static void createInstance(String project, String zone, String instanceName)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // Below are sample values that can be replaced.
    // machineType: machine type of the VM being created.
    // *   This value uses the format zones/{zone}/machineTypes/{type_name}.
    // *   For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
    // sourceImage: path to the operating system image to mount.
    // *   For details about images you can mount, see https://cloud.google.com/compute/docs/images
    // diskSizeGb: storage size of the boot disk to attach to the instance.
    // networkName: network interface to associate with the instance.
    String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
    String sourceImage = String
        .format("projects/debian-cloud/global/images/family/%s", "debian-13");
    long diskSizeGb = 10L;
    String networkName = "default";

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `instancesClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
      // Instance creation requires at least one persistent disk and one network interface.
      AttachedDisk disk =
          AttachedDisk.newBuilder()
              .setBoot(true)
              .setAutoDelete(true)
              .setType(Type.PERSISTENT.toString())
              .setDeviceName("disk-1")
              .setInitializeParams(
                  AttachedDiskInitializeParams.newBuilder()
                      .setSourceImage(sourceImage)
                      .setDiskSizeGb(diskSizeGb)
                      .build())
              .build();

      // Use the network interface provided in the networkName argument.
      NetworkInterface networkInterface = NetworkInterface.newBuilder()
          .setName(networkName)
          .build();

      // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
      Instance instanceResource =
          Instance.newBuilder()
              .setName(instanceName)
              .setMachineType(machineType)
              .addDisks(disk)
              .addNetworkInterfaces(networkInterface)
              .build();

      System.out.printf("Creating instance: %s at %s %n", instanceName, zone);

      // Insert the instance in the specified project and zone.
      InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
          .setProject(project)
          .setZone(zone)
          .setInstanceResource(instanceResource)
          .build();

      OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
          insertInstanceRequest);

      // Wait for the operation to complete.
      Operation response = operation.get(3, TimeUnit.MINUTES);

      if (response.hasError()) {
        System.out.println("Instance creation failed ! ! " + response);
        return;
      }
      System.out.println("Operation Status: " + response.getStatus());
    }
  }
}

建立資源 URI

在 Compute Engine API 中,另一個 Google Cloud 資源的參照會以完整 URI 的形式提供:

https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/RESOURCE_TYPE/SPECIFIC_RESOURCE

每當您指定映像檔、機器類型、網路或任何其他資源時,都必須在使用 API 時提供資源的 URI。Google Cloud CLI 和 Google Cloud 控制台等用戶端工具會隱藏這項複雜性,並為您建立這些資源 URI,但如果直接與 API 互動,您就必須自行建立這些資源 URI。

不同類型的資源,其資源 URI 略有不同。例如,可用區資源在 URI 中有 zone 規格:

https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/e2-standard-2

區域資源會將 zone 規格替換為 region 規格:

https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses/ADDRESS_NAME

同樣地,全域資源具有 global 規格:

https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images/VM_NAME

Compute Engine API 也接受部分 URI,因為服務可以推斷專案 ID 等資訊。因此,下列先前 URI 的部分版本也適用:

zones/ZONE/machineTypes/e2-standard-2
regions/REGION/addresses/ADDRESS_NAME
project/PROJECT_ID/global/images/VM_NAME

在部分 URI 中,可用區和區域 URI 都省略了專案 ID,但映像檔 URI 則未省略。這是因為 Compute Engine 提供的公開映像檔託管在其他專案中,例如所有 Debian 映像檔託管在 debian-cloud,所有 Ubuntu 映像檔託管在 ubuntu-os-cloud。您必須提供適當的專案 ID,才能使用這些映像檔。如果省略映像檔的專案 ID,Compute Engine 會嘗試在專案中尋找映像檔,但由於映像檔不存在,要求會失敗。

不過,如果您使用屬於專案 (與您建立這項資源的專案相同) 的自訂映像檔,則可在提供映像檔 URI 時省略專案規格。

判斷必要屬性

Compute Engine API 參考文件適用於 IBV API 和 CBV API v1 和 Beta 版,說明瞭您可以為特定資源設定的所有可能屬性。參考文件會區分可變動和不可變動的屬性 (在屬性說明中以 [Output Only] 標示),但如要判斷資源的必要屬性,您需要查看該工作的專屬說明文件。

舉例來說,如果您要建立執行個體,請參閱「從映像檔建立執行個體」說明文件,瞭解要求所需的 API 屬性。如要在 API 中建立靜態外部 IP 位址,請參閱靜態外部 IP 位址說明文件。

驗證 API 要求

如要驗證 API 要求,請按照下列步驟操作:

  1. 在 Compute Engine API 參考資料中,找出程式碼呼叫的方法。例如 v1/compute.instances.insert 或 2026-09-01/compute.instances.insert。

  2. 在目錄選單中,按一下「Try it!」「Try this method」(試試這個方法) 視窗隨即開啟。

    內容選單中的「試試看!」按鈕。

  3. 在「Request parameters」(要求參數) 下方,您不需要提供 project 或 zone,因為驗證作業不需要提交要求。

  4. 僅限 IBV 方法:在「X-Goog-Api-Version」X-Goog-Api-Version下方選取 API 版本。

  5. 在「Request body」下方貼上要求。

要求中格式錯誤的元素會以藍色底線標示。點選各個加底線的部分,即可進一步瞭解要解決的問題。

處理 API 回應

如果您提出會變更 (修改) 資料的要求,Compute Engine 會傳回 Operation 物件,您可以輪詢該物件,取得要求作業的狀態。Operation 資源如下所示:

{
 "kind": "compute#operation",
 "id": "7127550864823803662",
 "name": "operation-1458856416788-52ed27a803e22-1c3bd86a-9e95017b",
 "zone": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE",
 "operationType": "insert",
 "targetLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/EXAMPLE_VM",
 "targetId": "4132355614508179214",
 "status": "PENDING",
 "user": "user@example.com",
 "progress": 0,
 "insertTime": "2016-03-24T14:53:37.788-07:00",
 "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/operations/operation-1458856416788-52ed27a803e22-1c3bd86a-9e95017b"
}

如果原始要求是變更可用區資源 (例如建立磁碟快照或停止執行個體),Compute Engine 會傳回 zoneOperations 物件。同樣地,區域和全域資源會分別傳回 regionOperations 或 globalOperations 物件。如要取得作業狀態,請對特定 Operation 資源使用 get 或 wait 方法執行要求,並提供作業的 name。

Operation 資源的狀態傳回 DONE 後,要求才算完成。視要求性質而定,這項作業可能需要一些時間。接著,在 Operation 資源的狀態傳回 DONE 後,您可以檢查作業是否成功,以及是否有任何錯誤。

舉例來說,下列回應指出上述作業已完成,並由 DONE 狀態指定:

endTime: '2016-03-24T14:54:07.119-07:00'
id: '7127550864823803662'
insertTime: '2016-03-24T14:53:37.788-07:00'
kind: compute#operation
name: operation-1458856416788-52ed27a803e22-1c3bd86a-9e95017b
operationType: insert
progress: 100
selfLink: https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/operations/operation-1458856416788-52ed27a803e22-1c3bd86a-9e95017b
startTime: '2016-03-24T14:53:38.397-07:00'
status: DONE
targetId: '4132355614508179214'
targetLink: https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/EXAMPLE_VM
user: user@example.com
zone: https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE

如要確認,請向資源發出 get 要求,檢查資源是否存在且正在執行。例如:

GET /compute/v1/projects/PROJECT_ID/zones/ZONE/instances/EXAMPLE_VM

{
  "cpuPlatform": "Intel Haswell",
  "creationTimestamp": "2016-03-24T14:53:37.170-07:00",
  "disks": [
    ..[snip]..
  ],
  "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/EXAMPLE_VM",
  "status": "RUNNING",
  "tags": {
    "fingerprint": "42WmSpB8rSM="
  },
  "zone": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE"
}

輪詢作業

您可以編寫一些程式碼,使用 get 或 wait 要求定期輪詢作業,並在作業狀態為 DONE 時傳回。

使用 get 要求時,無論作業狀態為何,系統都會立即傳回作業。您必須定期輪詢作業,才能得知作業何時完成。

如果您提出 wait 要求,系統會在作業 DONE 時,或要求接近 2 分鐘期限時傳回要求。您可以選擇使用 wait 或 get 輪詢作業,但 wait 方法比 get 方法更具優勢:

  • 您可以將用戶端設為降低輪詢作業狀態的頻率,減少 Compute Engine API 的 QPS 用量。
  • 由於伺服器會在作業完成後立即回應,因此從作業完成到用戶端收到作業完成通知之間的平均延遲時間會大幅縮短。
  • 這個方法提供有界限的等待。這個方法最多會等待預設的 HTTP 逾時時間 (2 分鐘),然後傳回作業的目前狀態,可能是 DONE 或仍在進行中。

wait 方法是盡可能執行的 API。如果伺服器超載,要求可能會在達到預設期限前傳回,或在等待 0 秒後傳回。此外,這個方法也不保證只在作業為 DONE 時傳回。舉例來說,如果要求接近 2 分鐘的期限,即使作業尚未完成,方法也會傳回。如要檢查作業,建議您使用 wait 或 get 方法 (在重試迴圈中,中間有休眠),定期輪詢作業狀態。重試時間間隔上限不得超過作業保留期限下限。

輪詢範例

下列範例使用 get 方法。您可以使用 wait 方法取代 get 方法:

Python

import time

from google.cloud import compute_v1


def wait_for_operation(
    operation: compute_v1.Operation, project_id: str
) -> compute_v1.Operation:
    """
    This method waits for an operation to be completed. Calling this function
    will block until the operation is finished.

    Args:
        operation: The Operation object representing the operation you want to
            wait on.
        project_id: project ID or project number of the Cloud project you want to use.

    Returns:
        Finished Operation object.
    """
    kwargs = {"project": project_id, "operation": operation.name}
    if operation.zone:
        client = compute_v1.ZoneOperationsClient()
        # Operation.zone is a full URL address of a zone, so we need to extract just the name
        kwargs["zone"] = operation.zone.rsplit("/", maxsplit=1)[1]
    elif operation.region:
        client = compute_v1.RegionOperationsClient()
        # Operation.region is a full URL address of a region, so we need to extract just the name
        kwargs["region"] = operation.region.rsplit("/", maxsplit=1)[1]
    else:
        client = compute_v1.GlobalOperationsClient()

    while True:
        result = client.get(**kwargs)

        if result.status == compute_v1.Operation.Status.DONE:
            print("Operation finished.")
            if result.error:
                print(f"Error during operation: {result.error}")
            return result

        print("Waiting for operation to complete...")
        time.sleep(2)

Java


import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.ZoneOperationsClient;
import java.io.IOException;

public class WaitForOperation {

  public static void main(String[] args) throws IOException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // operation: Specify the operation to wait.
    String project = "your-project-id";
    Operation operation = Operation.newBuilder().build();

    waitForOperation(project, operation);
  }

  // Waits for the specified operation to complete.
  public static void waitForOperation(String project, Operation operation)
      throws IOException {
    try (ZoneOperationsClient zoneOperationsClient = ZoneOperationsClient.create()) {

      // Check if the operation hasn't been completed already.
      if (operation.getStatus() != Status.DONE) {
        String zone = operation.getZone();
        zone = zone.substring(zone.lastIndexOf("/") + 1);

        // Wait for the operation to complete.
        Operation response = zoneOperationsClient.wait(project, zone, operation.getName());

        // Check if the operation has errors.
        if (response.hasError()) {
          System.out.println("Error in executing the operation ! ! " + response.getError());
          return;
        }
        System.out.println("Operation Status: " + response.getStatus());
        return;
      }
      System.out.println("Operation Status: " + operation.getStatus());
    }
  }
}