Google Compute Engine Operator¶
先决任务¶
要使用这些 Operator,您必须完成以下几项工作
使用 Cloud Console 选择或创建一个 Cloud Platform 项目。
为您的项目启用结算功能,详情请参见 Google Cloud 文档。
启用 API,详情请参见 Cloud Console 文档。
通过 pip 安装 API 库。
pip install 'apache-airflow[google]'详细信息请参阅 安装。
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 文档以 管理实例组。
参考¶
欲了解更多信息,请查阅