Source code for tests.system.providers.google.cloud.compute.example_compute_igm
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Example Airflow DAG that:* creates a copy of existing Instance Template* updates existing template in Instance Group Manager"""from__future__importannotationsimportosfromdatetimeimportdatetimefromairflowimportmodelsfromairflow.models.baseoperatorimportchainfromairflow.operators.bashimportBashOperatorfromairflow.providers.google.cloud.operators.computeimport(ComputeEngineCopyInstanceTemplateOperator,ComputeEngineDeleteInstanceGroupManagerOperator,ComputeEngineDeleteInstanceTemplateOperator,ComputeEngineInsertInstanceGroupManagerOperator,ComputeEngineInsertInstanceTemplateOperator,ComputeEngineInstanceGroupUpdateManagerTemplateOperator,)fromairflow.utils.trigger_ruleimportTriggerRule
)# [END howto_operator_gce_igm_insert_template]# Added to check for idempotence# [START howto_operator_gce_igm_insert_template_no_project_id]gce_instance_template_insert2=ComputeEngineInsertInstanceTemplateOperator(task_id="gcp_compute_create_template_task_2",body=INSTANCE_TEMPLATE_BODY,)# [END howto_operator_gce_igm_insert_template_no_project_id]# [START howto_operator_gce_igm_copy_template]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,)# [END howto_operator_gce_igm_copy_template]# Added to check for idempotence# [START howto_operator_gce_igm_copy_template_no_project_id]gce_instance_template_copy2=ComputeEngineCopyInstanceTemplateOperator(task_id="gcp_compute_igm_copy_template_task_2",resource_id=TEMPLATE_NAME,body_patch=INSTANCE_TEMPLATE_BODY_UPDATE,)# [END howto_operator_gce_igm_copy_template_no_project_id]# [START howto_operator_gce_insert_igm]gce_igm_insert=ComputeEngineInsertInstanceGroupManagerOperator(task_id="gcp_compute_create_group_task",zone=LOCATION,body=INSTANCE_GROUP_MANAGER_BODY,project_id=PROJECT_ID,)# [END howto_operator_gce_insert_igm]# Added to check for idempotence# [START howto_operator_gce_insert_igm_no_project_id]gce_igm_insert2=ComputeEngineInsertInstanceGroupManagerOperator(task_id="gcp_compute_create_group_task_2",zone=LOCATION,body=INSTANCE_GROUP_MANAGER_BODY,)# [END howto_operator_gce_insert_igm_no_project_id]bash_wait_operator=BashOperator(task_id="delay_bash_task",bash_command="sleep 3m")# [START howto_operator_gce_igm_update_template]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,)# [END howto_operator_gce_igm_update_template]# Added to check for idempotence (and without UPDATE_POLICY)# [START howto_operator_gce_igm_update_template_no_project_id]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,)# [END howto_operator_gce_igm_update_template_no_project_id]bash_wait_operator1=BashOperator(task_id="delay_bash_task_1",bash_command="sleep 3m")# [START howto_operator_gce_delete_old_template_no_project_id]gce_instance_template_old_delete=ComputeEngineDeleteInstanceTemplateOperator(task_id="gcp_compute_delete_old_template_task",resource_id=TEMPLATE_NAME,)# [END howto_operator_gce_delete_old_template_no_project_id]gce_instance_template_old_delete.trigger_rule=TriggerRule.ALL_DONE# [START howto_operator_gce_delete_new_template_no_project_id]gce_instance_template_new_delete=ComputeEngineDeleteInstanceTemplateOperator(task_id="gcp_compute_delete_new_template_task",resource_id=NEW_TEMPLATE_NAME,)# [END howto_operator_gce_delete_new_template_no_project_id]gce_instance_template_new_delete.trigger_rule=TriggerRule.ALL_DONE# [START howto_operator_gce_delete_igm_no_project_id]gce_igm_delete=ComputeEngineDeleteInstanceGroupManagerOperator(task_id="gcp_compute_delete_group_task",resource_id=INSTANCE_GROUP_MANAGER_NAME,zone=LOCATION,)# [END howto_operator_gce_delete_igm_no_project_id]gce_igm_delete.trigger_rule=TriggerRule.ALL_DONEbash_wait_operator2=BashOperator(task_id="delay_bash_task_2",bash_command="sleep 3m")chain(gce_instance_template_insert,gce_instance_template_insert2,gce_instance_template_copy,gce_instance_template_copy2,gce_igm_insert,gce_igm_insert2,bash_wait_operator,gce_instance_group_manager_update_template,gce_instance_group_manager_update_template2,bash_wait_operator1,gce_igm_delete,gce_instance_template_old_delete,gce_instance_template_new_delete,bash_wait_operator2,)# ### Everything below this line is not part of example #### ### Just for system tests purpose ###fromtests.system.utils.watcherimportwatcher# This test needs watcher in order to properly mark success/failure# when "tearDown" task with trigger rule is part of the DAGlist(dag.tasks)>>watcher()fromtests.system.utilsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)