Source code for tests.system.providers.amazon.aws.example_redshift_s3_transfers
# 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.from__future__importannotationsfromdatetimeimportdatetimeimportboto3fromairflowimportDAG,settingsfromairflow.decoratorsimporttaskfromairflow.modelsimportConnectionfromairflow.models.baseoperatorimportchainfromairflow.providers.amazon.aws.hooks.redshift_clusterimportRedshiftHookfromairflow.providers.amazon.aws.operators.redshift_clusterimport(RedshiftCreateClusterOperator,RedshiftDeleteClusterOperator,)fromairflow.providers.amazon.aws.operators.s3import(S3CreateBucketOperator,S3CreateObjectOperator,S3DeleteBucketOperator,)fromairflow.providers.amazon.aws.sensors.redshift_clusterimportRedshiftClusterSensorfromairflow.providers.amazon.aws.sensors.s3importS3KeySensorfromairflow.providers.amazon.aws.transfers.redshift_to_s3importRedshiftToS3Operatorfromairflow.providers.amazon.aws.transfers.s3_to_redshiftimportS3ToRedshiftOperatorfromairflow.providers.common.sql.operators.sqlimportSQLExecuteQueryOperatorfromairflow.utils.trigger_ruleimportTriggerRulefromtests.system.providers.amazon.aws.utilsimportENV_ID_KEY,SystemTestContextBuilderfromtests.system.providers.amazon.aws.utils.ec2importget_default_vpc_id
env_id=test_context[ENV_ID_KEY]redshift_cluster_identifier=f"{env_id}-redshift-cluster"conn_id_name=f"{env_id}-conn-id"sg_name=f"{env_id}-sg"bucket_name=f"{env_id}-bucket"get_vpc_id=get_default_vpc_id()set_up_sg=setup_security_group(sg_name,[IP_PERMISSION],get_vpc_id)create_bucket=S3CreateBucketOperator(task_id="s3_create_bucket",bucket_name=bucket_name,)create_cluster=RedshiftCreateClusterOperator(task_id="create_cluster",cluster_identifier=redshift_cluster_identifier,vpc_security_group_ids=[set_up_sg],publicly_accessible=True,cluster_type="single-node",node_type="dc2.large",master_username=DB_LOGIN,master_user_password=DB_PASS,)wait_cluster_available=RedshiftClusterSensor(task_id="wait_cluster_available",cluster_identifier=redshift_cluster_identifier,target_status="available",poke_interval=5,timeout=60*30,)set_up_connection=create_connection(conn_id_name,cluster_id=redshift_cluster_identifier)create_object=S3CreateObjectOperator(task_id="create_object",s3_bucket=bucket_name,s3_key=S3_KEY_2,data=DATA,replace=True,)create_table_redshift_data=SQLExecuteQueryOperator(task_id="create_table_redshift_data",conn_id=conn_id_name,sql=SQL_CREATE_TABLE,)insert_data=SQLExecuteQueryOperator(task_id="insert_data",conn_id=conn_id_name,sql=SQL_INSERT_DATA,)# [START howto_transfer_redshift_to_s3]transfer_redshift_to_s3=RedshiftToS3Operator(task_id="transfer_redshift_to_s3",redshift_conn_id=conn_id_name,s3_bucket=bucket_name,s3_key=S3_KEY,schema="PUBLIC",table=REDSHIFT_TABLE,)# [END howto_transfer_redshift_to_s3]check_if_key_exists=S3KeySensor(task_id="check_if_key_exists",bucket_name=bucket_name,bucket_key=f"{S3_KEY}/{REDSHIFT_TABLE}_0000_part_00",)# [START howto_transfer_s3_to_redshift]transfer_s3_to_redshift=S3ToRedshiftOperator(task_id="transfer_s3_to_redshift",redshift_conn_id=conn_id_name,s3_bucket=bucket_name,s3_key=S3_KEY_2,schema="PUBLIC",table=REDSHIFT_TABLE,copy_options=["csv"],)# [END howto_transfer_s3_to_redshift]# [START howto_transfer_s3_to_redshift_multiple_keys]transfer_s3_to_redshift_multiple=S3ToRedshiftOperator(task_id="transfer_s3_to_redshift_multiple",redshift_conn_id=conn_id_name,s3_bucket=bucket_name,s3_key=S3_KEY_PREFIX,schema="PUBLIC",table=REDSHIFT_TABLE,copy_options=["csv"],)# [END howto_transfer_s3_to_redshift_multiple_keys]drop_table=SQLExecuteQueryOperator(task_id="drop_table",conn_id=conn_id_name,sql=SQL_DROP_TABLE,trigger_rule=TriggerRule.ALL_DONE,)delete_cluster=RedshiftDeleteClusterOperator(task_id="delete_cluster",cluster_identifier=redshift_cluster_identifier,trigger_rule=TriggerRule.ALL_DONE,)delete_sg=delete_security_group(sec_group_id=set_up_sg,sec_group_name=sg_name,)delete_bucket=S3DeleteBucketOperator(task_id="delete_bucket",bucket_name=bucket_name,force_delete=True,trigger_rule=TriggerRule.ALL_DONE,)chain(# TEST SETUPtest_context,set_up_sg,create_bucket,create_cluster,wait_cluster_available,set_up_connection,create_object,create_table_redshift_data,insert_data,# TEST BODYtransfer_redshift_to_s3,check_if_key_exists,transfer_s3_to_redshift,transfer_s3_to_redshift_multiple,# TEST TEARDOWNdrop_table,delete_cluster,delete_sg,delete_bucket,)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)