Skip to content
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

[#14361] Adding new generator for configuration based source template #14887

Merged
merged 5 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from airbyte_cdk.sources.declarative.datetime.min_max_datetime import MinMaxDatetime
from airbyte_cdk.sources.declarative.declarative_stream import DeclarativeStream
from airbyte_cdk.sources.declarative.extractors.jello import JelloExtractor
from airbyte_cdk.sources.declarative.extractors.record_selector import RecordSelector
from airbyte_cdk.sources.declarative.requesters.error_handlers.backoff_strategies.constant_backoff_strategy import ConstantBackoffStrategy
from airbyte_cdk.sources.declarative.requesters.error_handlers.backoff_strategies.exponential_backoff_strategy import (
ExponentialBackoffStrategy,
Expand All @@ -15,8 +16,11 @@
from airbyte_cdk.sources.declarative.requesters.error_handlers.default_error_handler import DefaultErrorHandler
from airbyte_cdk.sources.declarative.requesters.http_requester import HttpRequester
from airbyte_cdk.sources.declarative.requesters.paginators.limit_paginator import LimitPaginator
from airbyte_cdk.sources.declarative.requesters.paginators.no_pagination import NoPagination
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.cursor_pagination_strategy import CursorPaginationStrategy
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.offset_increment import OffsetIncrement
from airbyte_cdk.sources.declarative.retrievers.simple_retriever import SimpleRetriever
from airbyte_cdk.sources.declarative.schema.json_schema import JsonSchema
from airbyte_cdk.sources.declarative.stream_slicers.cartesian_product_stream_slicer import CartesianProductStreamSlicer
from airbyte_cdk.sources.declarative.stream_slicers.datetime_stream_slicer import DatetimeStreamSlicer
from airbyte_cdk.sources.declarative.stream_slicers.list_stream_slicer import ListStreamSlicer
Expand All @@ -36,10 +40,14 @@
"ExponentialBackoffStrategy": ExponentialBackoffStrategy,
"HttpRequester": HttpRequester,
"JelloExtractor": JelloExtractor,
"JsonSchema": JsonSchema,
"LimitPaginator": LimitPaginator,
"ListStreamSlicer": ListStreamSlicer,
"MinMaxDatetime": MinMaxDatetime,
"NoPagination": NoPagination,
"OffsetIncrement": OffsetIncrement,
"RecordSelector": RecordSelector,
"RemoveFields": RemoveFields,
"SimpleRetriever": SimpleRetriever,
"TokenAuthenticator": TokenAuthenticator,
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class BasicHttpAuthenticator(TokenAuthenticator):
https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#basic_authentication_scheme
"""

def __init__(self, username: str, password: str, auth_method: str = "Basic", auth_header: str = "Authorization"):
def __init__(self, username: str, password: str = "", auth_method: str = "Basic", auth_header: str = "Authorization"):
auth_string = f"{username}:{password}".encode("utf8")
b64_encoded = base64.b64encode(auth_string).decode("utf8")
super().__init__(b64_encoded, auth_method, auth_header)
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def test_full_config():
primary_key: "id"
extractor:
ref: "*ref(extractor)"
transform: ".result[]"
transform: "_.result"
retriever:
ref: "*ref(retriever)"
requester:
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_full_config():
assert type(stream._retriever._record_selector) == RecordSelector
assert type(stream._retriever._record_selector._extractor._decoder) == JsonDecoder

assert stream._retriever._record_selector._extractor._transform == ".result[]"
assert stream._retriever._record_selector._extractor._transform == "_.result"
assert type(stream._retriever._record_selector._record_filter) == RecordFilter
assert stream._retriever._record_selector._record_filter._filter_interpolator._condition == "{{ record['id'] > stream_state['id'] }}"
assert stream._schema_loader._get_json_filepath() == "./source_sendgrid/schemas/lists.json"
Expand All @@ -252,21 +252,21 @@ def test_create_record_selector():
content = """
extractor:
type: JelloExtractor
transform: "_"
transform: "_.result"
selector:
class_name: airbyte_cdk.sources.declarative.extractors.record_selector.RecordSelector
record_filter:
class_name: airbyte_cdk.sources.declarative.extractors.record_filter.RecordFilter
condition: "{{ record['id'] > stream_state['id'] }}"
extractor:
ref: "*ref(extractor)"
transform: "_"
transform: "_.result"
"""
config = parser.parse(content)
selector = factory.create_component(config["selector"], input_config)()
assert isinstance(selector, RecordSelector)
assert isinstance(selector._extractor, JelloExtractor)
assert selector._extractor._transform == "_"
assert selector._extractor._transform == "_.result"
assert isinstance(selector._record_filter, RecordFilter)


Expand Down Expand Up @@ -352,7 +352,7 @@ def test_config_with_defaults():
page_size: 10
record_selector:
extractor:
transform: ".result[]"
transform: "_.result"
streams:
- "*ref(lists_stream)"
"""
Expand All @@ -367,7 +367,7 @@ def test_config_with_defaults():
assert type(stream._retriever) == SimpleRetriever
assert stream._retriever._requester._method == HttpMethod.GET
assert stream._retriever._requester._authenticator._tokens == ["verysecrettoken"]
assert stream._retriever._record_selector._extractor._transform == ".result[]"
assert stream._retriever._record_selector._extractor._transform == "_.result"
assert stream._schema_loader._get_json_filepath() == "./source_sendgrid/schemas/lists.yaml"
assert isinstance(stream._retriever._paginator, LimitPaginator)

Expand Down Expand Up @@ -415,7 +415,7 @@ class TestCreateTransformations:
page_size: 10
record_selector:
extractor:
transform: ".result[]"
transform: "_.result"
"""

def test_no_transformations(self):
Expand Down
31 changes: 31 additions & 0 deletions airbyte-integrations/connector-templates/generator/plopfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function (plop) {
const genericSourceInputRoot = '../source-generic';
const genericJdbcSourceInputRoot = '../source-java-jdbc';
const httpApiInputRoot = '../source-python-http-api';
const lowCodeSourceInputRoot = '../source-configuration-based';
const javaDestinationInput = '../destination-java';
const pythonDestinationInputRoot = '../destination-python';

Expand Down Expand Up @@ -136,6 +137,36 @@ module.exports = function (plop) {
]
});

plop.setGenerator('Configuration Based Source', {
description: 'Generate a Source that is described using a low code configuration file',
prompts: [{type: 'input', name: 'name', message: 'Source name e.g: "google-analytics"'}],
actions: [
{
abortOnFail: true,
type:'addMany',
destination: pythonSourceOutputRoot,
base: lowCodeSourceInputRoot,
templateFiles: `${lowCodeSourceInputRoot}/**/**`,
},
// common acceptance tests
{
abortOnFail: true,
type:'addMany',
destination: pythonSourceOutputRoot,
base: sourceAcceptanceTestFilesInputRoot,
templateFiles: `${sourceAcceptanceTestFilesInputRoot}/**/**`,
},
// plop doesn't add dotfiles by default so we manually add them
{
type:'add',
abortOnFail: true,
templateFile: `${lowCodeSourceInputRoot}/.dockerignore.hbs`,
path: `${pythonSourceOutputRoot}/.dockerignore`
},
{type: 'emitSuccess', outputPath: pythonSourceOutputRoot}
]
});

plop.setGenerator('Python Singer Source', {
description: 'Generate a Singer-tap-based Airbyte Source.',
prompts: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*
!Dockerfile
!main.py
!source_{{snakeCase name}}
!setup.py
!secrets
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM python:3.9.11-alpine3.15 as base

# build and load all requirements
FROM base as builder
WORKDIR /airbyte/integration_code

# upgrade pip to the latest version
RUN apk --no-cache upgrade \
&& pip install --upgrade pip \
&& apk --no-cache add tzdata build-base


COPY setup.py ./
# install necessary packages to a temporary folder
RUN pip install --prefix=/install .

# build a clean environment
FROM base
WORKDIR /airbyte/integration_code

# copy all loaded and built libraries to a pure basic image
COPY --from=builder /install /usr/local
# add default timezone settings
COPY --from=builder /usr/share/zoneinfo/Etc/UTC /etc/localtime
RUN echo "Etc/UTC" > /etc/timezone

# bash is installed for more convenient debugging.
RUN apk --no-cache add bash

# copy payload code only
COPY main.py ./
COPY source_{{snakeCase name}} ./source_{{snakeCase name}}

ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py"
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]

LABEL io.airbyte.version=0.1.0
LABEL io.airbyte.name=airbyte/source-{{dashCase name}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# {{capitalCase name}} Source

This is the repository for the {{capitalCase name}} configuration based source connector.
For information about how to use this connector within Airbyte, see [the documentation](https://docs.airbyte.io/integrations/sources/{{dashCase name}}).

## Local development

#### Building via Gradle
You can also build the connector in Gradle. This is typically used in CI and not needed for your development workflow.

To build using Gradle, from the Airbyte repository root, run:
```
./gradlew :airbyte-integrations:connectors:source-{{dashCase name}}:build
```

#### Create credentials
**If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/sources/{{dashCase name}})
to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `source_{{snakeCase name}}/spec.yaml` file.
Note that any directory named `secrets` is gitignored across the entire Airbyte repo, so there is no danger of accidentally checking in sensitive information.
See `integration_tests/sample_config.json` for a sample config file.

**If you are an Airbyte core member**, copy the credentials in Lastpass under the secret name `source {{dashCase name}} test creds`
and place them into `secrets/config.json`.

### Locally running the connector docker image

#### Build
First, make sure you build the latest Docker image:
```
docker build . -t airbyte/source-{{dashCase name}}:dev
```

You can also build the connector image via Gradle:
```
./gradlew :airbyte-integrations:connectors:source-{{dashCase name}}:airbyteDocker
```
When building via Gradle, the docker image name and tag, respectively, are the values of the `io.airbyte.name` and `io.airbyte.version` `LABEL`s in
the Dockerfile.

#### Run
Then run any of the connector commands as follows:
```
docker run --rm airbyte/source-{{dashCase name}}:dev spec
docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-{{dashCase name}}:dev check --config /secrets/config.json
docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-{{dashCase name}}:dev discover --config /secrets/config.json
docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/integration_tests:/integration_tests airbyte/source-{{dashCase name}}:dev read --config /secrets/config.json --catalog /integration_tests/configured_catalog.json
```
## Testing

#### Acceptance Tests
Customize `acceptance-test-config.yml` file to configure tests. See [Source Acceptance Tests](https://docs.airbyte.io/connector-development/testing-connectors/source-acceptance-tests-reference) for more information.
If your connector requires to create or destroy resources for use during acceptance tests create fixtures for it and place them inside integration_tests/acceptance.py.

To run your integration tests with docker

### Using gradle to run tests
All commands should be run from airbyte project root.
To run unit tests:
```
./gradlew :airbyte-integrations:connectors:source-{{dashCase name}}:unitTest
```
To run acceptance and custom integration tests:
```
./gradlew :airbyte-integrations:connectors:source-{{dashCase name}}:integrationTest
```

## Dependency Management
All of your dependencies should go in `setup.py`, NOT `requirements.txt`. The requirements file is only used to connect internal Airbyte dependencies in the monorepo for local development.
We split dependencies between two groups, dependencies that are:
* required for your connector to work need to go to `MAIN_REQUIREMENTS` list.
* required for the testing need to go to `TEST_REQUIREMENTS` list

### Publishing a new version of the connector
You've checked out the repo, implemented a million dollar feature, and you're ready to share your changes with the world. Now what?
1. Make sure your changes are passing unit and integration tests.
1. Bump the connector version in `Dockerfile` -- just increment the value of the `LABEL io.airbyte.version` appropriately (we use [SemVer](https://semver.org/)).
1. Create a Pull Request.
1. Pat yourself on the back for being an awesome contributor.
1. Someone from Airbyte will take a look at your PR and iterate with you to merge it into master.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'airbyte-python'
id 'airbyte-docker'
id 'airbyte-source-acceptance-test'
}

airbytePython {
moduleDirectory 'source_{{snakeCase name}}'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"todo-stream-name": {
"todo-field-name": "todo-abnormal-value"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#


import pytest

pytest_plugins = ("source_acceptance_test.plugin",)


@pytest.fixture(scope="session", autouse=True)
def connector_setup():
"""This fixture is a placeholder for external resources that acceptance test might require."""
# TODO: setup test dependencies if needed. otherwise remove the TODO comments
yield
# TODO: clean up test dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"streams": [
{
"name": "TODO fix this file",
"supported_sync_modes": ["full_refresh", "incremental"],
"source_defined_cursor": true,
"default_cursor_field": "column1",
"json_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"column1": {
"type": "string"
},
"column2": {
"type": "number"
}
}
}
},
{
"name": "table1",
"supported_sync_modes": ["full_refresh", "incremental"],
"source_defined_cursor": false,
"json_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"column1": {
"type": "string"
},
"column2": {
"type": "number"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"streams": [
{
"stream": {
"name": "customers",
"json_schema": {},
"supported_sync_modes": ["full_refresh"]
},
"sync_mode": "full_refresh",
"destination_sync_mode": "overwrite"
},
{
"stream": {
"name": "employees",
"json_schema": {},
"supported_sync_modes": ["full_refresh", "incremental"]
},
"sync_mode": "incremental",
"destination_sync_mode": "append"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"todo-wrong-field": "this should be an incomplete config file, used in standard tests"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"fix-me": "TODO"
}
Loading