-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathConnectionsHandler.java
608 lines (505 loc) · 27.9 KB
/
ConnectionsHandler.java
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.server.handlers;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Lists;
import io.airbyte.analytics.TrackingClient;
import io.airbyte.api.model.generated.AirbyteCatalog;
import io.airbyte.api.model.generated.AirbyteStreamConfiguration;
import io.airbyte.api.model.generated.CatalogDiff;
import io.airbyte.api.model.generated.ConnectionCreate;
import io.airbyte.api.model.generated.ConnectionRead;
import io.airbyte.api.model.generated.ConnectionReadList;
import io.airbyte.api.model.generated.ConnectionSearch;
import io.airbyte.api.model.generated.ConnectionUpdate;
import io.airbyte.api.model.generated.DestinationRead;
import io.airbyte.api.model.generated.DestinationSearch;
import io.airbyte.api.model.generated.SourceRead;
import io.airbyte.api.model.generated.SourceSearch;
import io.airbyte.api.model.generated.StreamDescriptor;
import io.airbyte.api.model.generated.WorkspaceIdRequestBody;
import io.airbyte.commons.enums.Enums;
import io.airbyte.commons.json.Jsons;
import io.airbyte.config.ActorCatalog;
import io.airbyte.config.BasicSchedule;
import io.airbyte.config.DestinationConnection;
import io.airbyte.config.FieldSelectionData;
import io.airbyte.config.Geography;
import io.airbyte.config.JobSyncConfig.NamespaceDefinitionType;
import io.airbyte.config.Schedule;
import io.airbyte.config.ScheduleData;
import io.airbyte.config.SourceConnection;
import io.airbyte.config.StandardDestinationDefinition;
import io.airbyte.config.StandardSourceDefinition;
import io.airbyte.config.StandardSync;
import io.airbyte.config.StandardSync.ScheduleType;
import io.airbyte.config.StandardWorkspace;
import io.airbyte.config.helpers.ScheduleHelpers;
import io.airbyte.config.persistence.ConfigNotFoundException;
import io.airbyte.config.persistence.ConfigRepository;
import io.airbyte.persistence.job.WorkspaceHelper;
import io.airbyte.protocol.models.CatalogHelpers;
import io.airbyte.protocol.models.ConfiguredAirbyteCatalog;
import io.airbyte.server.converters.ApiPojoConverters;
import io.airbyte.server.converters.CatalogDiffConverters;
import io.airbyte.server.handlers.helpers.CatalogConverter;
import io.airbyte.server.handlers.helpers.ConnectionMatcher;
import io.airbyte.server.handlers.helpers.ConnectionScheduleHelper;
import io.airbyte.server.handlers.helpers.DestinationMatcher;
import io.airbyte.server.handlers.helpers.SourceMatcher;
import io.airbyte.server.scheduler.EventRunner;
import io.airbyte.validation.json.JsonValidationException;
import io.airbyte.workers.helper.ConnectionHelper;
import jakarta.inject.Singleton;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class ConnectionsHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionsHandler.class);
private final ConfigRepository configRepository;
private final Supplier<UUID> uuidGenerator;
private final WorkspaceHelper workspaceHelper;
private final TrackingClient trackingClient;
private final EventRunner eventRunner;
private final ConnectionHelper connectionHelper;
@VisibleForTesting
ConnectionsHandler(final ConfigRepository configRepository,
final Supplier<UUID> uuidGenerator,
final WorkspaceHelper workspaceHelper,
final TrackingClient trackingClient,
final EventRunner eventRunner,
final ConnectionHelper connectionHelper) {
this.configRepository = configRepository;
this.uuidGenerator = uuidGenerator;
this.workspaceHelper = workspaceHelper;
this.trackingClient = trackingClient;
this.eventRunner = eventRunner;
this.connectionHelper = connectionHelper;
}
@Deprecated(forRemoval = true)
public ConnectionsHandler(final ConfigRepository configRepository,
final WorkspaceHelper workspaceHelper,
final TrackingClient trackingClient,
final EventRunner eventRunner,
final ConnectionHelper connectionHelper) {
this(configRepository,
UUID::randomUUID,
workspaceHelper,
trackingClient,
eventRunner,
connectionHelper);
}
public ConnectionRead createConnection(final ConnectionCreate connectionCreate)
throws JsonValidationException, IOException, ConfigNotFoundException {
// Validate source and destination
final SourceConnection sourceConnection = configRepository.getSourceConnection(connectionCreate.getSourceId());
final DestinationConnection destinationConnection = configRepository.getDestinationConnection(connectionCreate.getDestinationId());
// Set this as default name if connectionCreate doesn't have it
final String defaultName = sourceConnection.getName() + " <> " + destinationConnection.getName();
final List<UUID> operationIds = connectionCreate.getOperationIds() != null ? connectionCreate.getOperationIds() : Collections.emptyList();
ConnectionHelper.validateWorkspace(workspaceHelper,
connectionCreate.getSourceId(),
connectionCreate.getDestinationId(),
operationIds);
final UUID connectionId = uuidGenerator.get();
// If not specified, default the NamespaceDefinition to 'source'
final NamespaceDefinitionType namespaceDefinitionType =
connectionCreate.getNamespaceDefinition() == null
? NamespaceDefinitionType.SOURCE
: Enums.convertTo(connectionCreate.getNamespaceDefinition(), NamespaceDefinitionType.class);
// persist sync
final StandardSync standardSync = new StandardSync()
.withConnectionId(connectionId)
.withName(connectionCreate.getName() != null ? connectionCreate.getName() : defaultName)
.withNamespaceDefinition(namespaceDefinitionType)
.withNamespaceFormat(connectionCreate.getNamespaceFormat())
.withPrefix(connectionCreate.getPrefix())
.withSourceId(connectionCreate.getSourceId())
.withDestinationId(connectionCreate.getDestinationId())
.withOperationIds(operationIds)
.withStatus(ApiPojoConverters.toPersistenceStatus(connectionCreate.getStatus()))
.withSourceCatalogId(connectionCreate.getSourceCatalogId())
.withGeography(getGeographyFromConnectionCreateOrWorkspace(connectionCreate))
.withBreakingChange(false)
.withNonBreakingChangesPreference(
ApiPojoConverters.toPersistenceNonBreakingChangesPreference(connectionCreate.getNonBreakingChangesPreference()));
if (connectionCreate.getResourceRequirements() != null) {
standardSync.withResourceRequirements(ApiPojoConverters.resourceRequirementsToInternal(connectionCreate.getResourceRequirements()));
}
// TODO Undesirable behavior: sending a null configured catalog should not be valid?
if (connectionCreate.getSyncCatalog() != null) {
standardSync.withCatalog(CatalogConverter.toConfiguredProtocol(connectionCreate.getSyncCatalog()));
standardSync.withFieldSelectionData(CatalogConverter.getFieldSelectionData(connectionCreate.getSyncCatalog()));
} else {
standardSync.withCatalog(new ConfiguredAirbyteCatalog().withStreams(Collections.emptyList()));
standardSync.withFieldSelectionData(new FieldSelectionData());
}
if (connectionCreate.getSchedule() != null && connectionCreate.getScheduleType() != null) {
throw new JsonValidationException("supply old or new schedule schema but not both");
}
if (connectionCreate.getScheduleType() != null) {
ConnectionScheduleHelper.populateSyncFromScheduleTypeAndData(standardSync, connectionCreate.getScheduleType(),
connectionCreate.getScheduleData());
} else {
populateSyncFromLegacySchedule(standardSync, connectionCreate);
}
configRepository.writeStandardSync(standardSync);
trackNewConnection(standardSync);
try {
LOGGER.info("Starting a connection manager workflow");
eventRunner.createConnectionManagerWorkflow(connectionId);
} catch (final Exception e) {
LOGGER.error("Start of the connection manager workflow failed", e);
configRepository.deleteStandardSync(standardSync.getConnectionId());
throw e;
}
return buildConnectionRead(connectionId);
}
private Geography getGeographyFromConnectionCreateOrWorkspace(final ConnectionCreate connectionCreate)
throws JsonValidationException, ConfigNotFoundException, IOException {
if (connectionCreate.getGeography() != null) {
return ApiPojoConverters.toPersistenceGeography(connectionCreate.getGeography());
}
// connectionCreate didn't specify a geography, so use the workspace default geography if one exists
final UUID workspaceId = workspaceHelper.getWorkspaceForSourceId(connectionCreate.getSourceId());
final StandardWorkspace workspace = configRepository.getStandardWorkspaceNoSecrets(workspaceId, true);
if (workspace.getDefaultGeography() != null) {
return workspace.getDefaultGeography();
}
// if the workspace doesn't have a default geography, default to 'auto'
return Geography.AUTO;
}
private void populateSyncFromLegacySchedule(final StandardSync standardSync, final ConnectionCreate connectionCreate) {
if (connectionCreate.getSchedule() != null) {
final Schedule schedule = new Schedule()
.withTimeUnit(ApiPojoConverters.toPersistenceTimeUnit(connectionCreate.getSchedule().getTimeUnit()))
.withUnits(connectionCreate.getSchedule().getUnits());
// Populate the legacy field.
// TODO(https://github.com/airbytehq/airbyte/issues/11432): remove.
standardSync
.withManual(false)
.withSchedule(schedule);
// Also write into the new field. This one will be consumed if populated.
standardSync
.withScheduleType(ScheduleType.BASIC_SCHEDULE);
standardSync.withScheduleData(new ScheduleData().withBasicSchedule(
new BasicSchedule().withTimeUnit(ApiPojoConverters.toBasicScheduleTimeUnit(connectionCreate.getSchedule().getTimeUnit()))
.withUnits(connectionCreate.getSchedule().getUnits())));
} else {
standardSync.withManual(true);
standardSync.withScheduleType(ScheduleType.MANUAL);
}
}
private void trackNewConnection(final StandardSync standardSync) {
try {
final UUID workspaceId = workspaceHelper.getWorkspaceForConnectionIdIgnoreExceptions(standardSync.getConnectionId());
final Builder<String, Object> metadataBuilder = generateMetadata(standardSync);
trackingClient.track(workspaceId, "New Connection - Backend", metadataBuilder.build());
} catch (final Exception e) {
LOGGER.error("failed while reporting usage.", e);
}
}
private Builder<String, Object> generateMetadata(final StandardSync standardSync) {
final Builder<String, Object> metadata = ImmutableMap.builder();
final UUID connectionId = standardSync.getConnectionId();
final StandardSourceDefinition sourceDefinition = configRepository
.getSourceDefinitionFromConnection(connectionId);
final StandardDestinationDefinition destinationDefinition = configRepository
.getDestinationDefinitionFromConnection(connectionId);
metadata.put("connector_source", sourceDefinition.getName());
metadata.put("connector_source_definition_id", sourceDefinition.getSourceDefinitionId());
metadata.put("connector_destination", destinationDefinition.getName());
metadata.put("connector_destination_definition_id", destinationDefinition.getDestinationDefinitionId());
final String frequencyString;
if (standardSync.getScheduleType() != null) {
frequencyString = getFrequencyStringFromScheduleType(standardSync.getScheduleType(), standardSync.getScheduleData());
} else if (standardSync.getManual()) {
frequencyString = "manual";
} else {
final long intervalInMinutes = TimeUnit.SECONDS.toMinutes(ScheduleHelpers.getIntervalInSecond(standardSync.getSchedule()));
frequencyString = intervalInMinutes + " min";
}
metadata.put("frequency", frequencyString);
return metadata;
}
public ConnectionRead updateConnection(final ConnectionUpdate connectionPatch)
throws ConfigNotFoundException, IOException, JsonValidationException {
final UUID connectionId = connectionPatch.getConnectionId();
LOGGER.debug("Starting updateConnection for connectionId {}...", connectionId);
LOGGER.debug("incoming connectionPatch: {}", connectionPatch);
final StandardSync sync = configRepository.getStandardSync(connectionId);
LOGGER.debug("initial StandardSync: {}", sync);
validateConnectionPatch(workspaceHelper, sync, connectionPatch);
final ConnectionRead initialConnectionRead = ApiPojoConverters.internalToConnectionRead(sync);
LOGGER.debug("initial ConnectionRead: {}", initialConnectionRead);
applyPatchToStandardSync(sync, connectionPatch);
LOGGER.debug("patched StandardSync before persisting: {}", sync);
configRepository.writeStandardSync(sync);
eventRunner.update(connectionId);
final ConnectionRead updatedRead = buildConnectionRead(connectionId);
LOGGER.debug("final connectionRead: {}", updatedRead);
return updatedRead;
}
/**
* Modifies the given StandardSync by applying changes from a partially-filled ConnectionUpdate
* patch. Any fields that are null in the patch will be left unchanged.
*/
private static void applyPatchToStandardSync(final StandardSync sync, final ConnectionUpdate patch) throws JsonValidationException {
// update the sync's schedule using the patch's scheduleType and scheduleData. validations occur in
// the helper to ensure both fields
// make sense together.
if (patch.getScheduleType() != null) {
ConnectionScheduleHelper.populateSyncFromScheduleTypeAndData(sync, patch.getScheduleType(), patch.getScheduleData());
}
// the rest of the fields are straightforward to patch. If present in the patch, set the field to
// the value
// in the patch. Otherwise, leave the field unchanged.
if (patch.getSyncCatalog() != null) {
sync.setCatalog(CatalogConverter.toConfiguredProtocol(patch.getSyncCatalog()));
sync.withFieldSelectionData(CatalogConverter.getFieldSelectionData(patch.getSyncCatalog()));
}
if (patch.getName() != null) {
sync.setName(patch.getName());
}
if (patch.getNamespaceDefinition() != null) {
sync.setNamespaceDefinition(Enums.convertTo(patch.getNamespaceDefinition(), NamespaceDefinitionType.class));
}
if (patch.getNamespaceFormat() != null) {
sync.setNamespaceFormat(patch.getNamespaceFormat());
}
if (patch.getPrefix() != null) {
sync.setPrefix(patch.getPrefix());
}
if (patch.getOperationIds() != null) {
sync.setOperationIds(patch.getOperationIds());
}
if (patch.getStatus() != null) {
sync.setStatus(ApiPojoConverters.toPersistenceStatus(patch.getStatus()));
}
if (patch.getSourceCatalogId() != null) {
sync.setSourceCatalogId(patch.getSourceCatalogId());
}
if (patch.getResourceRequirements() != null) {
sync.setResourceRequirements(ApiPojoConverters.resourceRequirementsToInternal(patch.getResourceRequirements()));
}
if (patch.getGeography() != null) {
sync.setGeography(ApiPojoConverters.toPersistenceGeography(patch.getGeography()));
}
if (patch.getBreakingChange() != null) {
sync.setBreakingChange(patch.getBreakingChange());
}
if (patch.getNotifySchemaChanges() != null) {
sync.setNotifySchemaChanges(patch.getNotifySchemaChanges());
}
if (patch.getNonBreakingChangesPreference() != null) {
sync.setNonBreakingChangesPreference(ApiPojoConverters.toPersistenceNonBreakingChangesPreference(patch.getNonBreakingChangesPreference()));
}
}
private void validateConnectionPatch(final WorkspaceHelper workspaceHelper, final StandardSync persistedSync, final ConnectionUpdate patch) {
// sanity check that we're updating the right connection
Preconditions.checkArgument(persistedSync.getConnectionId().equals(patch.getConnectionId()));
// make sure all operationIds belong to the same workspace as the connection
ConnectionHelper.validateWorkspace(
workspaceHelper, persistedSync.getSourceId(), persistedSync.getDestinationId(), patch.getOperationIds());
// make sure the incoming schedule update is sensible. Note that schedule details are further
// validated in ConnectionScheduleHelper, this just
// sanity checks that fields are populated when they should be.
Preconditions.checkArgument(
patch.getSchedule() == null,
"ConnectionUpdate should only make changes to the schedule by setting scheduleType and scheduleData. 'schedule' is no longer supported.");
if (patch.getScheduleType() == null) {
Preconditions.checkArgument(
patch.getScheduleData() == null,
"ConnectionUpdate should not include any scheduleData without also specifying a valid scheduleType.");
} else {
switch (patch.getScheduleType()) {
case MANUAL -> Preconditions.checkArgument(
patch.getScheduleData() == null,
"ConnectionUpdate should not include any scheduleData when setting the Connection scheduleType to MANUAL.");
case BASIC -> Preconditions.checkArgument(
patch.getScheduleData() != null,
"ConnectionUpdate should include scheduleData when setting the Connection scheduleType to BASIC.");
case CRON -> Preconditions.checkArgument(
patch.getScheduleData() != null,
"ConnectionUpdate should include scheduleData when setting the Connection scheduleType to CRON.");
// shouldn't be possible to reach this case
default -> throw new RuntimeException("Unrecognized scheduleType!");
}
}
}
public ConnectionReadList listConnectionsForWorkspace(final WorkspaceIdRequestBody workspaceIdRequestBody)
throws JsonValidationException, IOException, ConfigNotFoundException {
return listConnectionsForWorkspace(workspaceIdRequestBody, false);
}
public ConnectionReadList listAllConnectionsForWorkspace(final WorkspaceIdRequestBody workspaceIdRequestBody)
throws JsonValidationException, IOException, ConfigNotFoundException {
return listConnectionsForWorkspace(workspaceIdRequestBody, true);
}
public ConnectionReadList listConnectionsForWorkspace(final WorkspaceIdRequestBody workspaceIdRequestBody, final boolean includeDeleted)
throws JsonValidationException, IOException, ConfigNotFoundException {
final List<ConnectionRead> connectionReads = Lists.newArrayList();
for (final StandardSync standardSync : configRepository.listWorkspaceStandardSyncs(workspaceIdRequestBody.getWorkspaceId(), includeDeleted)) {
connectionReads.add(ApiPojoConverters.internalToConnectionRead(standardSync));
}
return new ConnectionReadList().connections(connectionReads);
}
public ConnectionReadList listConnectionsForSource(final UUID sourceId, final boolean includeDeleted) throws IOException {
final List<ConnectionRead> connectionReads = Lists.newArrayList();
for (final StandardSync standardSync : configRepository.listConnectionsBySource(sourceId, includeDeleted)) {
connectionReads.add(ApiPojoConverters.internalToConnectionRead(standardSync));
}
return new ConnectionReadList().connections(connectionReads);
}
public ConnectionReadList listConnections() throws JsonValidationException, ConfigNotFoundException, IOException {
final List<ConnectionRead> connectionReads = Lists.newArrayList();
for (final StandardSync standardSync : configRepository.listStandardSyncs()) {
if (standardSync.getStatus() == StandardSync.Status.DEPRECATED) {
continue;
}
connectionReads.add(ApiPojoConverters.internalToConnectionRead(standardSync));
}
return new ConnectionReadList().connections(connectionReads);
}
public ConnectionRead getConnection(final UUID connectionId)
throws JsonValidationException, IOException, ConfigNotFoundException {
return buildConnectionRead(connectionId);
}
public CatalogDiff getDiff(final AirbyteCatalog oldCatalog, final AirbyteCatalog newCatalog, final ConfiguredAirbyteCatalog configuredCatalog)
throws JsonValidationException {
return new CatalogDiff().transforms(CatalogHelpers.getCatalogDiff(
CatalogHelpers.configuredCatalogToCatalog(CatalogConverter.toProtocolKeepAllStreams(oldCatalog)),
CatalogHelpers.configuredCatalogToCatalog(CatalogConverter.toProtocolKeepAllStreams(newCatalog)), configuredCatalog)
.stream()
.map(CatalogDiffConverters::streamTransformToApi)
.toList());
}
/**
* Returns the list of the streamDescriptor that have their config updated.
*
* @param oldCatalog the old catalog
* @param newCatalog the new catalog
* @return the list of StreamDescriptor that have their configuration changed
*/
public Set<StreamDescriptor> getConfigurationDiff(final AirbyteCatalog oldCatalog, final AirbyteCatalog newCatalog) {
final Map<StreamDescriptor, AirbyteStreamConfiguration> oldStreams = catalogToPerStreamConfiguration(oldCatalog);
final Map<StreamDescriptor, AirbyteStreamConfiguration> newStreams = catalogToPerStreamConfiguration(newCatalog);
final Set<StreamDescriptor> streamWithDifferentConf = new HashSet<>();
newStreams.forEach(((streamDescriptor, airbyteStreamConfiguration) -> {
final AirbyteStreamConfiguration oldConfig = oldStreams.get(streamDescriptor);
if (oldConfig != null && haveConfigChange(oldConfig, airbyteStreamConfiguration)) {
streamWithDifferentConf.add(streamDescriptor);
}
}));
return streamWithDifferentConf;
}
private boolean haveConfigChange(final AirbyteStreamConfiguration oldConfig, final AirbyteStreamConfiguration newConfig) {
final List<String> oldCursors = oldConfig.getCursorField();
final List<String> newCursors = newConfig.getCursorField();
final boolean hasCursorChanged = !(oldCursors.equals(newCursors));
final boolean hasSyncModeChanged = !oldConfig.getSyncMode().equals(newConfig.getSyncMode());
final boolean hasDestinationSyncModeChanged = !oldConfig.getDestinationSyncMode().equals(newConfig.getDestinationSyncMode());
final Set<List<String>> convertedOldPrimaryKey = new HashSet<>(oldConfig.getPrimaryKey());
final Set<List<String>> convertedNewPrimaryKey = new HashSet<>(newConfig.getPrimaryKey());
final boolean hasPrimaryKeyChanged = !(convertedOldPrimaryKey.equals(convertedNewPrimaryKey));
return hasCursorChanged || hasSyncModeChanged || hasDestinationSyncModeChanged || hasPrimaryKeyChanged;
}
private Map<StreamDescriptor, AirbyteStreamConfiguration> catalogToPerStreamConfiguration(final AirbyteCatalog catalog) {
return catalog.getStreams().stream().collect(Collectors.toMap(stream -> new StreamDescriptor()
.name(stream.getStream().getName())
.namespace(stream.getStream().getNamespace()),
stream -> stream.getConfig()));
}
public Optional<AirbyteCatalog> getConnectionAirbyteCatalog(final UUID connectionId)
throws JsonValidationException, ConfigNotFoundException, IOException {
final StandardSync connection = configRepository.getStandardSync(connectionId);
if (connection.getSourceCatalogId() == null) {
return Optional.empty();
}
final ActorCatalog catalog = configRepository.getActorCatalogById(connection.getSourceCatalogId());
return Optional.of(CatalogConverter.toApi(Jsons.object(catalog.getCatalog(),
io.airbyte.protocol.models.AirbyteCatalog.class)));
}
public ConnectionReadList searchConnections(final ConnectionSearch connectionSearch)
throws JsonValidationException, IOException, ConfigNotFoundException {
final List<ConnectionRead> reads = Lists.newArrayList();
for (final StandardSync standardSync : configRepository.listStandardSyncs()) {
if (standardSync.getStatus() != StandardSync.Status.DEPRECATED) {
final ConnectionRead connectionRead = ApiPojoConverters.internalToConnectionRead(standardSync);
if (matchSearch(connectionSearch, connectionRead)) {
reads.add(connectionRead);
}
}
}
return new ConnectionReadList().connections(reads);
}
public boolean matchSearch(final ConnectionSearch connectionSearch, final ConnectionRead connectionRead)
throws JsonValidationException, ConfigNotFoundException, IOException {
final SourceConnection sourceConnection = configRepository.getSourceConnection(connectionRead.getSourceId());
final StandardSourceDefinition sourceDefinition =
configRepository.getStandardSourceDefinition(sourceConnection.getSourceDefinitionId());
final SourceRead sourceRead = SourceHandler.toSourceRead(sourceConnection, sourceDefinition);
final DestinationConnection destinationConnection = configRepository.getDestinationConnection(connectionRead.getDestinationId());
final StandardDestinationDefinition destinationDefinition =
configRepository.getStandardDestinationDefinition(destinationConnection.getDestinationDefinitionId());
final DestinationRead destinationRead = DestinationHandler.toDestinationRead(destinationConnection, destinationDefinition);
final ConnectionMatcher connectionMatcher = new ConnectionMatcher(connectionSearch);
final ConnectionRead connectionReadFromSearch = connectionMatcher.match(connectionRead);
return (connectionReadFromSearch == null || connectionReadFromSearch.equals(connectionRead)) &&
matchSearch(connectionSearch.getSource(), sourceRead) &&
matchSearch(connectionSearch.getDestination(), destinationRead);
}
// todo (cgardens) - make this static. requires removing one bad dependency in SourceHandlerTest
public boolean matchSearch(final SourceSearch sourceSearch, final SourceRead sourceRead) {
final SourceMatcher sourceMatcher = new SourceMatcher(sourceSearch);
final SourceRead sourceReadFromSearch = sourceMatcher.match(sourceRead);
return (sourceReadFromSearch == null || sourceReadFromSearch.equals(sourceRead));
}
// todo (cgardens) - make this static. requires removing one bad dependency in
// DestinationHandlerTest
public boolean matchSearch(final DestinationSearch destinationSearch, final DestinationRead destinationRead) {
final DestinationMatcher destinationMatcher = new DestinationMatcher(destinationSearch);
final DestinationRead destinationReadFromSearch = destinationMatcher.match(destinationRead);
return (destinationReadFromSearch == null || destinationReadFromSearch.equals(destinationRead));
}
public void deleteConnection(final UUID connectionId) throws JsonValidationException, ConfigNotFoundException, IOException {
connectionHelper.deleteConnection(connectionId);
eventRunner.forceDeleteConnection(connectionId);
}
private ConnectionRead buildConnectionRead(final UUID connectionId)
throws ConfigNotFoundException, IOException, JsonValidationException {
final StandardSync standardSync = configRepository.getStandardSync(connectionId);
return ApiPojoConverters.internalToConnectionRead(standardSync);
}
private static String getFrequencyStringFromScheduleType(final ScheduleType scheduleType, final ScheduleData scheduleData) {
switch (scheduleType) {
case MANUAL -> {
return "manual";
}
case BASIC_SCHEDULE -> {
return TimeUnit.SECONDS.toMinutes(ScheduleHelpers.getIntervalInSecond(scheduleData.getBasicSchedule())) + " min";
}
case CRON -> {
// TODO(https://github.com/airbytehq/airbyte/issues/2170): consider something more detailed.
return "cron";
}
default -> {
throw new RuntimeException("Unexpected schedule type");
}
}
}
}