Deployment Manager API を使用した長時間実行オペレーションの管理

Deployment Manager に API リクエストを送信すると、Deployment Manager は他の Google Cloud API に対応するリクエストを送信します。たとえば、Compute Engine インスタンスを含むデプロイメントを作成すると、Deployment Manager は Compute Engine API に instances.insert リクエストを送信します。

デプロイの作成、更新、削除を行う場合や、完了に数分以上かかる可能性のある他の API メソッドを使用する場合、Deployment Manager への API リクエストは長時間実行オペレーションを返します。

このページでは、Deployment Manager API を使用して長時間実行オペレーションを管理するためのガイドラインについて説明します。

始める前に

長時間実行オペレーションを開始する可能性があるメソッドには、次のようなものがあります。

これらのリクエストでは、レスポンスはオペレーション リソースです。この例は次のようになります。

{
 "kind": "deploymentmanager#operation",
 "id": "7123423453456654321",  # generated ID
 "name": "operation-11233234311678-587af2c0xyz9c-0987fa54-baccd59f",  # generated name
 "operationType": "insert",
 "targetLink": "https://www.googleapis.com/deploymentmanager/v2/projects/example-project/global/deployments/wordpress-1",
 "targetId": "5123321654456321123",
 "status": "DONE",  # possible values: RUNNING, PENDING, DONE
 "user": "example@example.com",
 "progress": 100,
 "insertTime": "2019-04-29T11:08:32.190-07:00",
 "startTime": "2019-04-29T11:08:32.200-07:00",
 "endTime": "2019-04-29T11:09:38.176-07:00",
 "selfLink": "https://www.googleapis.com/deploymentmanager/v2/projects/example-project/global/operations/operation-11233234311678-587af2c0xyz9c-0987fa54-baccd59f"
}

オペレーションのステータスを取得する

オペレーションのステータスを確認するには、オペレーションの name フィールドを使用して operations.get() リクエストを行い、オペレーションの status または progress を確認します。

statusDONEprogress100error オブジェクトが空の場合、デプロイが完了し、リソースを使用できる状態になります。

オペレーション中にエラーが発生した場合は、error オブジェクトに次の形式でエラーが表示されます。

"error": {
  "errors": [
    {
      "code": "error code"  # string,
      "location": "location of the error"  # string,
      "message": "error message"   # string
    },
    ...
  ]
}

ベスト プラクティスとして、指数バックオフでオペレーションのステータスをポーリングすることをおすすめします。たとえば、最初のリクエストは 5 秒後に、次のリクエストは 10 秒後に、3 番目のリクエストは 20 秒後に実行します。最大バックオフ時間を数分に設定することをおすすめします。最大バックオフ時間に達した後、バックオフ時間を増加させ続ける必要はありません。