|
| 1 | +# Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 2 | +"""Defines the `source-declarative-manifest` connector, which installs alongside CDK. |
| 3 | +
|
| 4 | +This file was originally imported from the dedicated connector directory, under the |
| 5 | +`airbyte` monorepo. |
| 6 | +
|
| 7 | +Usage: |
| 8 | +
|
| 9 | +``` |
| 10 | +pipx install airbyte-cdk |
| 11 | +source-declarative-manifest --help |
| 12 | +source-declarative-manifest spec |
| 13 | +... |
| 14 | +``` |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import json |
| 20 | +import pkgutil |
| 21 | +import sys |
| 22 | +import traceback |
| 23 | +from collections.abc import Mapping |
| 24 | +from datetime import datetime |
| 25 | +from pathlib import Path |
| 26 | +from typing import Any, cast |
| 27 | + |
| 28 | +from airbyte_cdk.entrypoint import AirbyteEntrypoint, launch |
| 29 | +from airbyte_cdk.models import ( |
| 30 | + AirbyteErrorTraceMessage, |
| 31 | + AirbyteMessage, |
| 32 | + AirbyteMessageSerializer, |
| 33 | + AirbyteStateMessage, |
| 34 | + AirbyteTraceMessage, |
| 35 | + ConfiguredAirbyteCatalog, |
| 36 | + ConnectorSpecificationSerializer, |
| 37 | + TraceType, |
| 38 | + Type, |
| 39 | +) |
| 40 | +from airbyte_cdk.sources.declarative.concurrent_declarative_source import ( |
| 41 | + ConcurrentDeclarativeSource, |
| 42 | +) |
| 43 | +from airbyte_cdk.sources.declarative.yaml_declarative_source import YamlDeclarativeSource |
| 44 | +from airbyte_cdk.sources.source import TState |
| 45 | +from orjson import orjson |
| 46 | + |
| 47 | + |
| 48 | +class SourceLocalYaml(YamlDeclarativeSource): |
| 49 | + """ |
| 50 | + Declarative source defined by a yaml file in the local filesystem |
| 51 | + """ |
| 52 | + |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + catalog: ConfiguredAirbyteCatalog | None, |
| 56 | + config: Mapping[str, Any] | None, |
| 57 | + state: TState, |
| 58 | + **kwargs: Any, |
| 59 | + ) -> None: |
| 60 | + """ |
| 61 | + HACK! |
| 62 | + Problem: YamlDeclarativeSource relies on the calling module name/path to find the yaml file. |
| 63 | + Implication: If you call YamlDeclarativeSource directly it will look for the yaml file in the wrong place. (e.g. the airbyte-cdk package) |
| 64 | + Solution: Subclass YamlDeclarativeSource from the same location as the manifest to load. |
| 65 | +
|
| 66 | + When can we remove this? |
| 67 | + When the airbyte-cdk is updated to not rely on the calling module name/path to find the yaml file. |
| 68 | + When all manifest connectors are updated to use the new airbyte-cdk. |
| 69 | + When all manifest connectors are updated to use the source-declarative-manifest as the base image. |
| 70 | + """ |
| 71 | + super().__init__( |
| 72 | + catalog=catalog, |
| 73 | + config=config, |
| 74 | + state=state, |
| 75 | + path_to_yaml="manifest.yaml", |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def _is_local_manifest_command(args: list[str]) -> bool: |
| 80 | + # Check for a local manifest.yaml file |
| 81 | + return Path("/airbyte/integration_code/source_declarative_manifest/manifest.yaml").exists() |
| 82 | + |
| 83 | + |
| 84 | +def handle_command(args: list[str]) -> None: |
| 85 | + if _is_local_manifest_command(args): |
| 86 | + handle_local_manifest_command(args) |
| 87 | + else: |
| 88 | + handle_remote_manifest_command(args) |
| 89 | + |
| 90 | + |
| 91 | +def _get_local_yaml_source(args: list[str]) -> SourceLocalYaml: |
| 92 | + try: |
| 93 | + config, catalog, state = _parse_inputs_into_config_catalog_state(args) |
| 94 | + return SourceLocalYaml(config=config, catalog=catalog, state=state) |
| 95 | + except Exception as error: |
| 96 | + print( |
| 97 | + orjson.dumps( |
| 98 | + AirbyteMessageSerializer.dump( |
| 99 | + AirbyteMessage( |
| 100 | + type=Type.TRACE, |
| 101 | + trace=AirbyteTraceMessage( |
| 102 | + type=TraceType.ERROR, |
| 103 | + emitted_at=int(datetime.now().timestamp() * 1000), |
| 104 | + error=AirbyteErrorTraceMessage( |
| 105 | + message=f"Error starting the sync. This could be due to an invalid configuration or catalog. Please contact Support for assistance. Error: {error}", |
| 106 | + stack_trace=traceback.format_exc(), |
| 107 | + ), |
| 108 | + ), |
| 109 | + ) |
| 110 | + ) |
| 111 | + ).decode() |
| 112 | + ) |
| 113 | + raise error |
| 114 | + |
| 115 | + |
| 116 | +def handle_local_manifest_command(args: list[str]) -> None: |
| 117 | + source = _get_local_yaml_source(args) |
| 118 | + launch( |
| 119 | + source=source, |
| 120 | + args=args, |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def handle_remote_manifest_command(args: list[str]) -> None: |
| 125 | + """Overrides the spec command to return the generalized spec for the declarative manifest source. |
| 126 | +
|
| 127 | + This is different from a typical low-code, but built and published separately source built as a ManifestDeclarativeSource, |
| 128 | + because that will have a spec method that returns the spec for that specific source. Other than spec, |
| 129 | + the generalized connector behaves the same as any other, since the manifest is provided in the config. |
| 130 | + """ |
| 131 | + if args[0] == "spec": |
| 132 | + json_spec = pkgutil.get_data( |
| 133 | + "airbyte_cdk.cli.source_declarative_manifest", |
| 134 | + "spec.json", |
| 135 | + ) |
| 136 | + if json_spec is None: |
| 137 | + raise FileNotFoundError( |
| 138 | + "Could not find `spec.json` file for source-declarative-manifest" |
| 139 | + ) |
| 140 | + |
| 141 | + spec_obj = json.loads(json_spec) |
| 142 | + spec = ConnectorSpecificationSerializer.load(spec_obj) |
| 143 | + |
| 144 | + message = AirbyteMessage(type=Type.SPEC, spec=spec) |
| 145 | + print(AirbyteEntrypoint.airbyte_message_to_string(message)) |
| 146 | + else: |
| 147 | + source = create_declarative_source(args) |
| 148 | + launch( |
| 149 | + source=source, |
| 150 | + args=args, |
| 151 | + ) |
| 152 | + |
| 153 | + |
| 154 | +def create_declarative_source(args: list[str]) -> ConcurrentDeclarativeSource: |
| 155 | + """Creates the source with the injected config. |
| 156 | +
|
| 157 | + This essentially does what other low-code sources do at build time, but at runtime, |
| 158 | + with a user-provided manifest in the config. This better reflects what happens in the |
| 159 | + connector builder. |
| 160 | + """ |
| 161 | + try: |
| 162 | + config, catalog, state = _parse_inputs_into_config_catalog_state(args) |
| 163 | + if "__injected_declarative_manifest" not in config: |
| 164 | + raise ValueError( |
| 165 | + f"Invalid config: `__injected_declarative_manifest` should be provided at the root of the config but config only has keys {list(config.keys())}" |
| 166 | + ) |
| 167 | + return ConcurrentDeclarativeSource( |
| 168 | + config=config, |
| 169 | + catalog=catalog, |
| 170 | + state=state, |
| 171 | + source_config=cast(dict[str, Any], config["__injected_declarative_manifest"]), |
| 172 | + ) |
| 173 | + except Exception as error: |
| 174 | + print( |
| 175 | + orjson.dumps( |
| 176 | + AirbyteMessageSerializer.dump( |
| 177 | + AirbyteMessage( |
| 178 | + type=Type.TRACE, |
| 179 | + trace=AirbyteTraceMessage( |
| 180 | + type=TraceType.ERROR, |
| 181 | + emitted_at=int(datetime.now().timestamp() * 1000), |
| 182 | + error=AirbyteErrorTraceMessage( |
| 183 | + message=f"Error starting the sync. This could be due to an invalid configuration or catalog. Please contact Support for assistance. Error: {error}", |
| 184 | + stack_trace=traceback.format_exc(), |
| 185 | + ), |
| 186 | + ), |
| 187 | + ) |
| 188 | + ) |
| 189 | + ).decode() |
| 190 | + ) |
| 191 | + raise error |
| 192 | + |
| 193 | + |
| 194 | +def _parse_inputs_into_config_catalog_state( |
| 195 | + args: list[str], |
| 196 | +) -> tuple[ |
| 197 | + Mapping[str, Any] | None, |
| 198 | + ConfiguredAirbyteCatalog | None, |
| 199 | + list[AirbyteStateMessage], |
| 200 | +]: |
| 201 | + parsed_args = AirbyteEntrypoint.parse_args(args) |
| 202 | + config = ( |
| 203 | + ConcurrentDeclarativeSource.read_config(parsed_args.config) |
| 204 | + if hasattr(parsed_args, "config") |
| 205 | + else None |
| 206 | + ) |
| 207 | + catalog = ( |
| 208 | + ConcurrentDeclarativeSource.read_catalog(parsed_args.catalog) |
| 209 | + if hasattr(parsed_args, "catalog") |
| 210 | + else None |
| 211 | + ) |
| 212 | + state = ( |
| 213 | + ConcurrentDeclarativeSource.read_state(parsed_args.state) |
| 214 | + if hasattr(parsed_args, "state") |
| 215 | + else [] |
| 216 | + ) |
| 217 | + |
| 218 | + return config, catalog, state |
| 219 | + |
| 220 | + |
| 221 | +def run() -> None: |
| 222 | + args: list[str] = sys.argv[1:] |
| 223 | + handle_command(args) |
0 commit comments