-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmanifest_declarative_source.py
355 lines (307 loc) · 14.7 KB
/
manifest_declarative_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import json
import logging
import pkgutil
from copy import deepcopy
from importlib import metadata
from typing import Any, Dict, Iterator, List, Mapping, Optional
from packaging.version import Version, InvalidVersion
import yaml
from airbyte_cdk.models import (
AirbyteConnectionStatus,
AirbyteMessage,
AirbyteStateMessage,
ConfiguredAirbyteCatalog,
ConnectorSpecification,
)
from airbyte_cdk.sources.declarative.checks.connection_checker import ConnectionChecker
from airbyte_cdk.sources.declarative.declarative_source import DeclarativeSource
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
CheckStream as CheckStreamModel,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
DeclarativeStream as DeclarativeStreamModel,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import Spec as SpecModel
from airbyte_cdk.sources.declarative.parsers.manifest_component_transformer import (
ManifestComponentTransformer,
)
from airbyte_cdk.sources.declarative.resolvers import COMPONENTS_RESOLVER_TYPE_MAPPING
from airbyte_cdk.sources.declarative.parsers.manifest_reference_resolver import (
ManifestReferenceResolver,
)
from airbyte_cdk.sources.declarative.parsers.model_to_component_factory import (
ModelToComponentFactory,
)
from airbyte_cdk.sources.message import MessageRepository
from airbyte_cdk.sources.streams.core import Stream
from airbyte_cdk.sources.types import ConnectionDefinition
from airbyte_cdk.sources.utils.slice_logger import (
AlwaysLogSliceLogger,
DebugSliceLogger,
SliceLogger,
)
from jsonschema.exceptions import ValidationError
from jsonschema.validators import validate
class ManifestDeclarativeSource(DeclarativeSource):
"""Declarative source defined by a manifest of low-code components that define source connector behavior"""
def __init__(
self,
source_config: ConnectionDefinition,
debug: bool = False,
emit_connector_builder_messages: bool = False,
component_factory: Optional[ModelToComponentFactory] = None,
):
"""
:param source_config(Mapping[str, Any]): The manifest of low-code components that describe the source connector
:param debug(bool): True if debug mode is enabled
:param component_factory(ModelToComponentFactory): optional factory if ModelToComponentFactory's default behaviour needs to be tweaked
"""
self.logger = logging.getLogger(f"airbyte.{self.name}")
# For ease of use we don't require the type to be specified at the top level manifest, but it should be included during processing
manifest = dict(source_config)
if "type" not in manifest:
manifest["type"] = "DeclarativeSource"
resolved_source_config = ManifestReferenceResolver().preprocess_manifest(manifest)
propagated_source_config = ManifestComponentTransformer().propagate_types_and_parameters(
"", resolved_source_config, {}
)
self._source_config = propagated_source_config
self._debug = debug
self._emit_connector_builder_messages = emit_connector_builder_messages
self._constructor = (
component_factory
if component_factory
else ModelToComponentFactory(emit_connector_builder_messages)
)
self._message_repository = self._constructor.get_message_repository()
self._slice_logger: SliceLogger = (
AlwaysLogSliceLogger() if emit_connector_builder_messages else DebugSliceLogger()
)
self._validate_source()
@property
def resolved_manifest(self) -> Mapping[str, Any]:
return self._source_config
@property
def message_repository(self) -> MessageRepository:
return self._message_repository
@property
def connection_checker(self) -> ConnectionChecker:
check = self._source_config["check"]
if "type" not in check:
check["type"] = "CheckStream"
check_stream = self._constructor.create_component(
CheckStreamModel,
check,
dict(),
emit_connector_builder_messages=self._emit_connector_builder_messages,
)
if isinstance(check_stream, ConnectionChecker):
return check_stream
else:
raise ValueError(
f"Expected to generate a ConnectionChecker component, but received {check_stream.__class__}"
)
def streams(self, config: Mapping[str, Any]) -> List[Stream]:
self._emit_manifest_debug_message(
extra_args={"source_name": self.name, "parsed_config": json.dumps(self._source_config)}
)
stream_configs = self._stream_configs(self._source_config) + self._dynamic_stream_configs(
self._source_config, config
)
source_streams = [
self._constructor.create_component(
DeclarativeStreamModel,
stream_config,
config,
emit_connector_builder_messages=self._emit_connector_builder_messages,
)
for stream_config in self._initialize_cache_for_parent_streams(deepcopy(stream_configs))
]
return source_streams
@staticmethod
def _initialize_cache_for_parent_streams(
stream_configs: List[Dict[str, Any]],
) -> List[Dict[str, Any]]:
parent_streams = set()
def update_with_cache_parent_configs(parent_configs: list[dict[str, Any]]) -> None:
for parent_config in parent_configs:
parent_streams.add(parent_config["stream"]["name"])
parent_config["stream"]["retriever"]["requester"]["use_cache"] = True
for stream_config in stream_configs:
if stream_config.get("incremental_sync", {}).get("parent_stream"):
parent_streams.add(stream_config["incremental_sync"]["parent_stream"]["name"])
stream_config["incremental_sync"]["parent_stream"]["retriever"]["requester"][
"use_cache"
] = True
elif stream_config.get("retriever", {}).get("partition_router", {}):
partition_router = stream_config["retriever"]["partition_router"]
if isinstance(partition_router, dict) and partition_router.get(
"parent_stream_configs"
):
update_with_cache_parent_configs(partition_router["parent_stream_configs"])
elif isinstance(partition_router, list):
for router in partition_router:
if router.get("parent_stream_configs"):
update_with_cache_parent_configs(router["parent_stream_configs"])
for stream_config in stream_configs:
if stream_config["name"] in parent_streams:
stream_config["retriever"]["requester"]["use_cache"] = True
return stream_configs
def spec(self, logger: logging.Logger) -> ConnectorSpecification:
"""
Returns the connector specification (spec) as defined in the Airbyte Protocol. The spec is an object describing the possible
configurations (e.g: username and password) which can be configured when running this connector. For low-code connectors, this
will first attempt to load the spec from the manifest's spec block, otherwise it will load it from "spec.yaml" or "spec.json"
in the project root.
"""
self._configure_logger_level(logger)
self._emit_manifest_debug_message(
extra_args={"source_name": self.name, "parsed_config": json.dumps(self._source_config)}
)
spec = self._source_config.get("spec")
if spec:
if "type" not in spec:
spec["type"] = "Spec"
spec_component = self._constructor.create_component(SpecModel, spec, dict())
return spec_component.generate_spec()
else:
return super().spec(logger)
def check(self, logger: logging.Logger, config: Mapping[str, Any]) -> AirbyteConnectionStatus:
self._configure_logger_level(logger)
return super().check(logger, config)
def read(
self,
logger: logging.Logger,
config: Mapping[str, Any],
catalog: ConfiguredAirbyteCatalog,
state: Optional[List[AirbyteStateMessage]] = None,
) -> Iterator[AirbyteMessage]:
self._configure_logger_level(logger)
yield from super().read(logger, config, catalog, state)
def _configure_logger_level(self, logger: logging.Logger) -> None:
"""
Set the log level to logging.DEBUG if debug mode is enabled
"""
if self._debug:
logger.setLevel(logging.DEBUG)
def _validate_source(self) -> None:
"""
Validates the connector manifest against the declarative component schema
"""
try:
raw_component_schema = pkgutil.get_data(
"airbyte_cdk", "sources/declarative/declarative_component_schema.yaml"
)
if raw_component_schema is not None:
declarative_component_schema = yaml.load(
raw_component_schema, Loader=yaml.SafeLoader
)
else:
raise RuntimeError(
"Failed to read manifest component json schema required for validation"
)
except FileNotFoundError as e:
raise FileNotFoundError(
f"Failed to read manifest component json schema required for validation: {e}"
)
streams = self._source_config.get("streams")
dynamic_streams = self._source_config.get("dynamic_streams")
if not (streams or dynamic_streams):
raise ValidationError(
f"A valid manifest should have at least one stream defined. Got {streams}"
)
try:
validate(self._source_config, declarative_component_schema)
except ValidationError as e:
raise ValidationError(
"Validation against json schema defined in declarative_component_schema.yaml schema failed"
) from e
cdk_version_str = metadata.version("airbyte_cdk")
cdk_version = self._parse_version(cdk_version_str, "airbyte-cdk")
manifest_version_str = self._source_config.get("version")
if manifest_version_str is None:
raise RuntimeError(
"Manifest version is not defined in the manifest. This is unexpected since it should be a required field. Please contact support."
)
manifest_version = self._parse_version(manifest_version_str, "manifest")
if (cdk_version.major, cdk_version.minor, cdk_version.micro) == (0, 0, 0):
# Skipping version compatibility check on unreleased dev branch
pass
elif (cdk_version.major, cdk_version.minor) < (
manifest_version.major,
manifest_version.minor,
):
raise ValidationError(
f"The manifest version {manifest_version!s} is greater than the airbyte-cdk package version ({cdk_version!s}). Your "
f"manifest may contain features that are not in the current CDK version."
)
elif (manifest_version.major, manifest_version.minor) < (0, 29):
raise ValidationError(
f"The low-code framework was promoted to Beta in airbyte-cdk version 0.29.0 and contains many breaking changes to the "
f"language. The manifest version {manifest_version!s} is incompatible with the airbyte-cdk package version "
f"{cdk_version!s} which contains these breaking changes."
)
@staticmethod
def _parse_version(
version: str,
version_type: str,
) -> Version:
"""Takes a semantic version represented as a string and splits it into a tuple.
The fourth part (prerelease) is not returned in the tuple.
Returns:
Version: the parsed version object
"""
try:
parsed_version = Version(version)
except InvalidVersion as ex:
raise ValidationError(
f"The {version_type} version '{version}' is not a valid version format."
) from ex
else:
# No exception
return parsed_version
def _stream_configs(self, manifest: Mapping[str, Any]) -> List[Dict[str, Any]]:
# This has a warning flag for static, but after we finish part 4 we'll replace manifest with self._source_config
stream_configs: List[Dict[str, Any]] = manifest.get("streams", [])
for s in stream_configs:
if "type" not in s:
s["type"] = "DeclarativeStream"
return stream_configs
def _dynamic_stream_configs(
self, manifest: Mapping[str, Any], config: Mapping[str, Any]
) -> List[Dict[str, Any]]:
dynamic_stream_definitions: List[Dict[str, Any]] = manifest.get("dynamic_streams", [])
dynamic_stream_configs: List[Dict[str, Any]] = []
for dynamic_definition in dynamic_stream_definitions:
components_resolver_config = dynamic_definition["components_resolver"]
if not components_resolver_config:
raise ValueError(
f"Missing 'components_resolver' in dynamic definition: {dynamic_definition}"
)
resolver_type = components_resolver_config.get("type")
if not resolver_type:
raise ValueError(
f"Missing 'type' in components resolver configuration: {components_resolver_config}"
)
if resolver_type not in COMPONENTS_RESOLVER_TYPE_MAPPING:
raise ValueError(
f"Invalid components resolver type '{resolver_type}'. "
f"Expected one of {list(COMPONENTS_RESOLVER_TYPE_MAPPING.keys())}."
)
# Create a resolver for dynamic components based on type
components_resolver = self._constructor.create_component(
COMPONENTS_RESOLVER_TYPE_MAPPING[resolver_type], components_resolver_config, config
)
stream_template_config = dynamic_definition["stream_template"]
for dynamic_stream in components_resolver.resolve_components(
stream_template_config=stream_template_config
):
if "type" not in dynamic_stream:
dynamic_stream["type"] = "DeclarativeStream"
dynamic_stream_configs.append(dynamic_stream)
return dynamic_stream_configs
def _emit_manifest_debug_message(self, extra_args: dict[str, Any]) -> None:
self.logger.debug("declarative source created from manifest", extra=extra_args)