-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `AwsToAwsBaseOperator` followup on #29452 (comment) This PR preserve all current behavior but add the needed interface to be used for other transfer operators
- Loading branch information
Showing
6 changed files
with
163 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# 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. | ||
"""This module contains base AWS to AWS transfer operator""" | ||
from __future__ import annotations | ||
|
||
import warnings | ||
from typing import Sequence | ||
|
||
from airflow.models import BaseOperator | ||
from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook | ||
from airflow.utils.types import NOTSET, ArgNotSet | ||
|
||
_DEPRECATION_MSG = ( | ||
"The aws_conn_id parameter has been deprecated. Use the source_aws_conn_id parameter instead." | ||
) | ||
|
||
|
||
class AwsToAwsBaseOperator(BaseOperator): | ||
""" | ||
Base class for AWS to AWS transfer operators | ||
:param source_aws_conn_id: The Airflow connection used for AWS credentials | ||
to access DynamoDB. If this is None or empty then the default boto3 | ||
behaviour is used. If running Airflow in a distributed manner and | ||
source_aws_conn_id is None or empty, then default boto3 configuration | ||
would be used (and must be maintained on each worker node). | ||
:param dest_aws_conn_id: The Airflow connection used for AWS credentials | ||
to access S3. If this is not set then the source_aws_conn_id connection is used. | ||
:param aws_conn_id: The Airflow connection used for AWS credentials (deprecated; use source_aws_conn_id). | ||
""" | ||
|
||
template_fields: Sequence[str] = ( | ||
"source_aws_conn_id", | ||
"dest_aws_conn_id", | ||
) | ||
|
||
def __init__( | ||
self, | ||
*, | ||
source_aws_conn_id: str | None = AwsBaseHook.default_conn_name, | ||
dest_aws_conn_id: str | None | ArgNotSet = NOTSET, | ||
aws_conn_id: str | None | ArgNotSet = NOTSET, | ||
**kwargs, | ||
) -> None: | ||
super().__init__(**kwargs) | ||
if not isinstance(aws_conn_id, ArgNotSet): | ||
warnings.warn(_DEPRECATION_MSG, DeprecationWarning, stacklevel=3) | ||
self.source_aws_conn_id = aws_conn_id | ||
else: | ||
self.source_aws_conn_id = source_aws_conn_id | ||
self.dest_aws_conn_id = ( | ||
self.source_aws_conn_id if isinstance(dest_aws_conn_id, ArgNotSet) else dest_aws_conn_id | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# 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__ import annotations | ||
|
||
import pytest | ||
|
||
from airflow import DAG | ||
from airflow.models import DagRun, TaskInstance | ||
from airflow.providers.amazon.aws.transfers.base import AwsToAwsBaseOperator | ||
from airflow.utils import timezone | ||
|
||
DEFAULT_DATE = timezone.datetime(2020, 1, 1) | ||
|
||
|
||
class TestAwsToAwsBaseOperator: | ||
def setup_method(self): | ||
args = {"owner": "airflow", "start_date": DEFAULT_DATE} | ||
self.dag = DAG("test_dag_id", default_args=args) | ||
|
||
def test_render_template(self): | ||
operator = AwsToAwsBaseOperator( | ||
task_id="dynamodb_to_s3_test_render", | ||
dag=self.dag, | ||
source_aws_conn_id="{{ ds }}", | ||
dest_aws_conn_id="{{ ds }}", | ||
) | ||
ti = TaskInstance(operator, run_id="something") | ||
ti.dag_run = DagRun(run_id="something", execution_date=timezone.datetime(2020, 1, 1)) | ||
ti.render_templates() | ||
assert "2020-01-01" == getattr(operator, "source_aws_conn_id") | ||
assert "2020-01-01" == getattr(operator, "dest_aws_conn_id") | ||
|
||
def test_deprecation(self): | ||
with pytest.warns( | ||
DeprecationWarning, | ||
match="The aws_conn_id parameter has been deprecated." | ||
" Use the source_aws_conn_id parameter instead.", | ||
): | ||
AwsToAwsBaseOperator( | ||
task_id="transfer", | ||
dag=self.dag, | ||
aws_conn_id="my_conn", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters