Google Compute Engine Operator

先決任務

要使用這些 Operator,您必須完成以下幾項工作

ComputeEngineInsertInstanceOperator

使用 ComputeEngineInsertInstanceOperator 建立新的 Google Compute Engine 例項。

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute.py

gce_instance_insert = ComputeEngineInsertInstanceOperator(
    task_id="gcp_compute_create_instance_task",
    project_id=PROJECT_ID,
    zone=LOCATION,
    body=GCE_INSTANCE_BODY,
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索

tests/system/google/cloud/compute/example_compute.py

gce_instance_insert2 = ComputeEngineInsertInstanceOperator(
    task_id="gcp_compute_create_instance_task_2",
    zone=LOCATION,
    body=GCE_INSTANCE_BODY,
)

模板化

template_fields: Sequence[str] = (
    "body",
    "project_id",
    "zone",
    "request_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
    "resource_id",
)

更多資訊

請參閱 Google Compute Engine API 文件以 插入例項

ComputeEngineInsertInstanceFromTemplateOperator

使用 ComputeEngineInsertInstanceFromTemplateOperator 基於指定的例項模板建立新的 Google Compute Engine 例項。

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute.py

gce_instance_insert_from_template = ComputeEngineInsertInstanceFromTemplateOperator(
    task_id="gcp_compute_create_instance_from_template_task",
    project_id=PROJECT_ID,
    zone=LOCATION,
    body=GCE_INSTANCE_FROM_TEMPLATE_BODY,
    source_instance_template=f"global/instanceTemplates/{TEMPLATE_NAME}",
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索

tests/system/google/cloud/compute/example_compute.py

gce_instance_insert_from_template2 = ComputeEngineInsertInstanceFromTemplateOperator(
    task_id="gcp_compute_create_instance_from_template_task_2",
    zone=LOCATION,
    body=GCE_INSTANCE_FROM_TEMPLATE_BODY,
    source_instance_template=f"global/instanceTemplates/{TEMPLATE_NAME}",
)

模板化

template_fields: Sequence[str] = (
    "body",
    "source_instance_template",
    "project_id",
    "zone",
    "request_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
    "resource_id",
)

更多資訊

請參閱 Google Compute Engine API 文件以 從模板插入例項

ComputeEngineDeleteInstanceOperator

使用 ComputeEngineDeleteInstanceOperator 刪除現有的 Google Compute Engine 例項。

使用 Operator

您可以在不指定專案 ID 的情況下建立 Operator - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索。建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute.py

gce_instance_delete = ComputeEngineDeleteInstanceOperator(
    task_id="gcp_compute_delete_instance_task",
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
    trigger_rule=TriggerRule.ALL_DONE,
)

模板化

template_fields: Sequence[str] = (
    "zone",
    "resource_id",
    "request_id",
    "project_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 刪除例項

ComputeEngineStartInstanceOperator

使用 ComputeEngineStartInstanceOperator 啟動現有的 Google Compute Engine 例項。

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute.py

gce_instance_start = ComputeEngineStartInstanceOperator(
    task_id="gcp_compute_start_task",
    project_id=PROJECT_ID,
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索

tests/system/google/cloud/compute/example_compute.py

gce_instance_start2 = ComputeEngineStartInstanceOperator(
    task_id="gcp_compute_start_task_2",
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "zone",
    "resource_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 啟動例項

ComputeEngineStopInstanceOperator

使用該 Operator 停止 Google Compute Engine 例項。

有關引數定義,請查閱 ComputeEngineStopInstanceOperator

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute.py

gce_instance_stop = ComputeEngineStopInstanceOperator(
    task_id="gcp_compute_stop_task",
    project_id=PROJECT_ID,
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
    trigger_rule=TriggerRule.ALL_DONE,
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索

tests/system/google/cloud/compute/example_compute.py

gce_instance_stop2 = ComputeEngineStopInstanceOperator(
    task_id="gcp_compute_stop_task_2",
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
    trigger_rule=TriggerRule.ALL_DONE,
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "zone",
    "resource_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 停止例項

ComputeEngineSetMachineTypeOperator

使用該 Operator 更改 Google Compute Engine 例項的機器型別。

有關引數定義,請查閱 ComputeEngineSetMachineTypeOperator

引數

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute.py

gce_set_machine_type = ComputeEngineSetMachineTypeOperator(
    task_id="gcp_compute_set_machine_type",
    project_id=PROJECT_ID,
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
    body={"machineType": f"zones/{LOCATION}/machineTypes/{SHORT_MACHINE_TYPE_NAME}"},
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索

tests/system/google/cloud/compute/example_compute.py

gce_set_machine_type2 = ComputeEngineSetMachineTypeOperator(
    zone=LOCATION,
    resource_id=GCE_INSTANCE_NAME,
    body={"machineType": f"zones/{LOCATION}/machineTypes/{SHORT_MACHINE_TYPE_NAME}"},
    task_id="gcp_compute_set_machine_type_2",
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "zone",
    "resource_id",
    "body",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 設定機器型別

ComputeEngineDeleteInstanceTemplateOperator

使用該 Operator 刪除 Google Compute Engine 例項模板。

有關引數定義,請查閱 ComputeEngineDeleteInstanceTemplateOperator

使用 Operator

建立 Operator 的程式碼

您可以在不指定專案 ID 的情況下建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索。建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_template_old_delete = ComputeEngineDeleteInstanceTemplateOperator(
    task_id="gcp_compute_delete_old_template_task",
    resource_id=TEMPLATE_NAME,
    trigger_rule=TriggerRule.ALL_DONE,
)

模板化

template_fields: Sequence[str] = (
    "resource_id",
    "request_id",
    "project_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 刪除模板

ComputeEngineInsertInstanceTemplateOperator

使用該 Operator 建立 Google Compute Engine 例項模板。

有關引數定義,請查閱 ComputeEngineInsertInstanceTemplateOperator

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_template_insert = ComputeEngineInsertInstanceTemplateOperator(
    task_id="gcp_compute_create_template_task",
    project_id=PROJECT_ID,
    body=INSTANCE_TEMPLATE_BODY,
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_template_insert2 = ComputeEngineInsertInstanceTemplateOperator(
    task_id="gcp_compute_create_template_task_2",
    body=INSTANCE_TEMPLATE_BODY,
)

模板化

template_fields: Sequence[str] = (
    "body",
    "project_id",
    "request_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
    "resource_id",
)

更多資訊

請參閱 Google Compute Engine API 文件以 建立新模板

ComputeEngineCopyInstanceTemplateOperator

使用該 Operator 複製現有的 Google Compute Engine 例項模板並對其應用補丁。

有關引數定義,請查閱 ComputeEngineCopyInstanceTemplateOperator

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_template_copy = ComputeEngineCopyInstanceTemplateOperator(
    task_id="gcp_compute_igm_copy_template_task",
    project_id=PROJECT_ID,
    resource_id=TEMPLATE_NAME,
    body_patch=INSTANCE_TEMPLATE_BODY_UPDATE,
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_template_copy2 = ComputeEngineCopyInstanceTemplateOperator(
    task_id="gcp_compute_igm_copy_template_task_2",
    resource_id=TEMPLATE_NAME,
    body_patch=INSTANCE_TEMPLATE_BODY_UPDATE,
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "resource_id",
    "request_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 使用現有模板建立新例項

ComputeEngineInsertInstanceGroupManagerOperator

使用該 Operator 建立 Compute Engine 例項組管理器。

有關引數定義,請查閱 ComputeEngineInsertInstanceGroupManagerOperator

引數

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute_igm.py

    gce_igm_insert = ComputeEngineInsertInstanceGroupManagerOperator(
        task_id="gcp_compute_create_group_task",
        zone=LOCATION,
        body=INSTANCE_GROUP_MANAGER_BODY,
        project_id=PROJECT_ID,
    )

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索

tests/system/google/cloud/compute/example_compute_igm.py

gce_igm_insert2 = ComputeEngineInsertInstanceGroupManagerOperator(
    task_id="gcp_compute_create_group_task_2",
    zone=LOCATION,
    body=INSTANCE_GROUP_MANAGER_BODY,
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "body",
    "zone",
    "request_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
    "resource_id",
)

更多資訊

請參閱 Google Compute Engine API 文件以 建立例項組

ComputeEngineDeleteInstanceGroupManagerOperator

使用該 Operator 刪除 Compute Engine 例項組管理器。

有關引數定義,請查閱 ComputeEngineDeleteInstanceGroupManagerOperator

引數

使用 Operator

您可以在不指定專案 ID 的情況下建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索。建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute_igm.py

gce_igm_delete = ComputeEngineDeleteInstanceGroupManagerOperator(
    task_id="gcp_compute_delete_group_task",
    resource_id=INSTANCE_GROUP_MANAGER_NAME,
    zone=LOCATION,
    trigger_rule=TriggerRule.ALL_DONE,
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "resource_id",
    "zone",
    "request_id",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

更多資訊

請參閱 Google Compute Engine API 文件以 刪除例項組

ComputeEngineInstanceGroupUpdateManagerTemplateOperator

使用該 Operator 更新 Google Compute Engine 例項組管理器中的模板。

有關引數定義,請查閱 ComputeEngineInstanceGroupUpdateManagerTemplateOperator

引數

使用 Operator

建立 Operator 的程式碼

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_group_manager_update_template = ComputeEngineInstanceGroupUpdateManagerTemplateOperator(
    task_id="gcp_compute_igm_group_manager_update_template",
    project_id=PROJECT_ID,
    resource_id=INSTANCE_GROUP_MANAGER_NAME,
    zone=LOCATION,
    source_template=SOURCE_TEMPLATE_URL,
    destination_template=DESTINATION_TEMPLATE_URL,
    update_policy=UPDATE_POLICY,
)

您也可以不指定專案 ID 來建立 Operator - 專案 ID 將從使用的 Google Cloud 連線中檢索

tests/system/google/cloud/compute/example_compute_igm.py

gce_instance_group_manager_update_template2 = ComputeEngineInstanceGroupUpdateManagerTemplateOperator(
    task_id="gcp_compute_igm_group_manager_update_template_2",
    resource_id=INSTANCE_GROUP_MANAGER_NAME,
    zone=LOCATION,
    source_template=SOURCE_TEMPLATE_URL,
    destination_template=DESTINATION_TEMPLATE_URL,
)

模板化

template_fields: Sequence[str] = (
    "project_id",
    "resource_id",
    "zone",
    "request_id",
    "source_template",
    "destination_template",
    "gcp_conn_id",
    "api_version",
    "impersonation_chain",
)

故障排除

您可能會發現 ComputeEngineInstanceGroupUpdateManagerTemplateOperator 因缺少許可權而失敗。要執行此操作,服務賬號需要具備 Service Account User 角色提供的許可權(透過 Google Cloud IAM 分配)。

更多資訊

請參閱 Google Compute Engine API 文件以 管理例項組

參考

欲瞭解更多資訊,請查閱

此條目有用嗎?