-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ [file based cdk] S3 legacy config adapter #29145
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
60 changes: 60 additions & 0 deletions
60
airbyte-integrations/connectors/source-s3/source_s3/v4/legacy_config_transformer.py
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,60 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from datetime import datetime | ||
from typing import Any, List, Mapping | ||
|
||
from source_s3.source import SourceS3Spec | ||
|
||
SECONDS_FORMAT = "%Y-%m-%dT%H:%M:%SZ" | ||
MICROS_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ" | ||
|
||
|
||
class LegacyConfigTransformer: | ||
""" | ||
Class that takes in S3 source configs in the legacy format and transforms them into | ||
configs that can be used by the new S3 source built with the file-based CDK. | ||
""" | ||
|
||
@classmethod | ||
def convert(cls, legacy_config: SourceS3Spec) -> Mapping[str, Any]: | ||
transformed_config = { | ||
"bucket": legacy_config.provider.bucket, | ||
"streams": [ | ||
{ | ||
"name": legacy_config.dataset, | ||
"file_type": legacy_config.format.filetype, | ||
"globs": cls._create_globs(legacy_config.path_pattern, legacy_config.provider.path_prefix), | ||
"validation_policy": "Emit Record", | ||
# todo: add formats on a per-type basis in follow up PRs | ||
} | ||
], | ||
} | ||
|
||
if legacy_config.provider.start_date: | ||
transformed_config["start_date"] = cls._transform_seconds_to_micros(legacy_config.provider.start_date) | ||
if legacy_config.provider.aws_access_key_id: | ||
transformed_config["aws_access_key_id"] = legacy_config.provider.aws_access_key_id | ||
if legacy_config.provider.aws_secret_access_key: | ||
transformed_config["aws_secret_access_key"] = legacy_config.provider.aws_secret_access_key | ||
if legacy_config.provider.endpoint: | ||
transformed_config["endpoint"] = legacy_config.provider.endpoint | ||
if legacy_config.user_schema and legacy_config.user_schema != "{}": | ||
transformed_config["streams"][0]["input_schema"] = legacy_config.user_schema | ||
|
||
return transformed_config | ||
|
||
@classmethod | ||
def _create_globs(cls, path_pattern: str, path_prefix: str) -> List[str]: | ||
if path_prefix: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could also add trimming to the prefix/pattern to avoid extra |
||
return [path_prefix + path_pattern] | ||
return [path_pattern] | ||
|
||
@classmethod | ||
def _transform_seconds_to_micros(cls, datetime_str: str) -> str: | ||
try: | ||
parsed_datetime = datetime.strptime(datetime_str, SECONDS_FORMAT) | ||
return datetime.strftime(parsed_datetime, MICROS_FORMAT) | ||
except ValueError as e: | ||
raise e |
23 changes: 23 additions & 0 deletions
23
airbyte-integrations/connectors/source-s3/source_s3/v4/source.py
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,23 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from typing import Any, Mapping | ||
|
||
from airbyte_cdk.sources.file_based.file_based_source import FileBasedSource | ||
from source_s3.source import SourceS3Spec | ||
from source_s3.v4.legacy_config_transformer import LegacyConfigTransformer | ||
|
||
|
||
class SourceS3(FileBasedSource): | ||
def read_config(self, config_path: str) -> Mapping[str, Any]: | ||
""" | ||
Used to override the default read_config so that when the new file-based S3 connector processes a config | ||
in the legacy format, it can be transformed into the new config. This happens in entrypoint before we | ||
validate the config against the new spec. | ||
""" | ||
config = super().read_config(config_path) | ||
if not config.get("streams"): | ||
parsed_legacy_config = SourceS3Spec(**config) | ||
return LegacyConfigTransformer.convert(parsed_legacy_config) | ||
return config |
86 changes: 86 additions & 0 deletions
86
airbyte-integrations/connectors/source-s3/unit_tests/v4/test_legacy_config_transformer.py
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,86 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import pytest | ||
from source_s3.source import SourceS3Spec | ||
from source_s3.v4.legacy_config_transformer import LegacyConfigTransformer | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"legacy_config, expected_config", | ||
[ | ||
pytest.param( | ||
{ | ||
"dataset": "test_data", | ||
"provider": { | ||
"storage": "S3", | ||
"bucket": "test_bucket", | ||
"aws_access_key_id": "some_access_key", | ||
"aws_secret_access_key": "some_secret", | ||
"endpoint": "https://external-s3.com", | ||
"path_prefix": "a_folder/", | ||
"start_date": "2022-01-01T01:02:03Z" | ||
|
||
}, | ||
"format": { | ||
"filetype": "csv", | ||
"delimiter": "^", | ||
"quote_char": "|", | ||
"escape_char": "!", | ||
"double_quote": True, | ||
"quoting_behavior": "Quote All" | ||
}, | ||
"path_pattern": "**/*.csv", | ||
"schema": '{"col1": "string", "col2": "integer"}' | ||
}, | ||
{ | ||
"bucket": "test_bucket", | ||
"aws_access_key_id": "some_access_key", | ||
"aws_secret_access_key": "some_secret", | ||
"endpoint": "https://external-s3.com", | ||
"start_date": "2022-01-01T01:02:03.000000Z", | ||
"streams": [ | ||
{ | ||
"name": "test_data", | ||
"file_type": "csv", | ||
"globs": ["a_folder/**/*.csv"], | ||
"validation_policy": "Emit Record", | ||
"input_schema": '{"col1": "string", "col2": "integer"}', | ||
} | ||
] | ||
} | ||
, id="test_convert_legacy_config" | ||
), | ||
pytest.param( | ||
{ | ||
"dataset": "test_data", | ||
"provider": { | ||
"storage": "S3", | ||
"bucket": "test_bucket", | ||
}, | ||
"format": { | ||
"filetype": "avro", | ||
}, | ||
"path_pattern": "**/*.csv", | ||
}, | ||
{ | ||
"bucket": "test_bucket", | ||
"streams": [ | ||
{ | ||
"name": "test_data", | ||
"file_type": "avro", | ||
"globs": ["**/*.csv"], | ||
"validation_policy": "Emit Record", | ||
} | ||
] | ||
} | ||
, id="test_convert_no_optional_fields" | ||
), | ||
] | ||
) | ||
def test_convert_legacy_config(legacy_config, expected_config): | ||
parsed_legacy_config = SourceS3Spec(**legacy_config) | ||
actual_config = LegacyConfigTransformer.convert(parsed_legacy_config) | ||
|
||
assert actual_config == expected_config |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you add a comment in the spec that this file needs to be updated if the spec changes? I'm not sure how long we'll need this transformer and there are PRs in the pipeline (eg #17334)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!