|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.integrations.destination.yellowbrick; |
| 6 | + |
| 7 | +import static io.airbyte.cdk.integrations.util.PostgresSslConnectionUtils.DISABLE; |
| 8 | +import static io.airbyte.cdk.integrations.util.PostgresSslConnectionUtils.PARAM_MODE; |
| 9 | +import static io.airbyte.cdk.integrations.util.PostgresSslConnectionUtils.PARAM_SSL; |
| 10 | +import static io.airbyte.cdk.integrations.util.PostgresSslConnectionUtils.PARAM_SSL_MODE; |
| 11 | +import static io.airbyte.cdk.integrations.util.PostgresSslConnectionUtils.obtainConnectionOptions; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.JsonNode; |
| 14 | +import com.google.common.collect.ImmutableMap; |
| 15 | +import io.airbyte.cdk.db.factory.DatabaseDriver; |
| 16 | +import io.airbyte.cdk.db.jdbc.JdbcDatabase; |
| 17 | +import io.airbyte.cdk.db.jdbc.JdbcUtils; |
| 18 | +import io.airbyte.cdk.integrations.base.*; |
| 19 | +import io.airbyte.cdk.integrations.base.ssh.SshWrappedDestination; |
| 20 | +import io.airbyte.cdk.integrations.destination.async.deser.StreamAwareDataTransformer; |
| 21 | +import io.airbyte.cdk.integrations.destination.jdbc.AbstractJdbcDestination; |
| 22 | +import io.airbyte.cdk.integrations.destination.jdbc.typing_deduping.JdbcDestinationHandler; |
| 23 | +import io.airbyte.cdk.integrations.destination.jdbc.typing_deduping.JdbcSqlGenerator; |
| 24 | +import io.airbyte.commons.json.Jsons; |
| 25 | +import io.airbyte.integrations.base.destination.typing_deduping.DestinationHandler; |
| 26 | +import io.airbyte.integrations.base.destination.typing_deduping.ParsedCatalog; |
| 27 | +import io.airbyte.integrations.base.destination.typing_deduping.SqlGenerator; |
| 28 | +import io.airbyte.integrations.base.destination.typing_deduping.migrators.Migration; |
| 29 | +import io.airbyte.integrations.destination.yellowbrick.typing_deduping.YellowbrickDataTransformer; |
| 30 | +import io.airbyte.integrations.destination.yellowbrick.typing_deduping.YellowbrickDestinationHandler; |
| 31 | +import io.airbyte.integrations.destination.yellowbrick.typing_deduping.YellowbrickSqlGenerator; |
| 32 | +import io.airbyte.integrations.destination.yellowbrick.typing_deduping.YellowbrickState; |
| 33 | +import java.io.UnsupportedEncodingException; |
| 34 | +import java.net.URLEncoder; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Optional; |
| 39 | +import org.slf4j.Logger; |
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +public class YellowbrickDestination extends AbstractJdbcDestination<YellowbrickState> implements Destination { |
| 43 | + |
| 44 | + private static final Logger LOGGER = LoggerFactory.getLogger(YellowbrickDestination.class); |
| 45 | + |
| 46 | + public static final String DRIVER_CLASS = DatabaseDriver.POSTGRESQL.getDriverClassName(); |
| 47 | + |
| 48 | + public static Destination sshWrappedDestination() { |
| 49 | + return new SshWrappedDestination(new YellowbrickDestination(), JdbcUtils.HOST_LIST_KEY, JdbcUtils.PORT_LIST_KEY); |
| 50 | + } |
| 51 | + |
| 52 | + public YellowbrickDestination() { |
| 53 | + super(DRIVER_CLASS, new YellowbrickSQLNameTransformer(), new YellowbrickSqlOperations()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected Map<String, String> getDefaultConnectionProperties(final JsonNode config) { |
| 58 | + final Map<String, String> additionalParameters = new HashMap<>(); |
| 59 | + if (!config.has(PARAM_SSL) || config.get(PARAM_SSL).asBoolean()) { |
| 60 | + if (config.has(PARAM_SSL_MODE)) { |
| 61 | + if (DISABLE.equals(config.get(PARAM_SSL_MODE).get(PARAM_MODE).asText())) { |
| 62 | + additionalParameters.put("sslmode", DISABLE); |
| 63 | + } else { |
| 64 | + additionalParameters.putAll(obtainConnectionOptions(config.get(PARAM_SSL_MODE))); |
| 65 | + } |
| 66 | + } else { |
| 67 | + additionalParameters.put(JdbcUtils.SSL_KEY, "true"); |
| 68 | + additionalParameters.put("sslmode", "require"); |
| 69 | + } |
| 70 | + } |
| 71 | + return additionalParameters; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public JsonNode toJdbcConfig(final JsonNode config) { |
| 76 | + final String schema = Optional.ofNullable(config.get(JdbcUtils.SCHEMA_KEY)).map(JsonNode::asText).orElse("public"); |
| 77 | + |
| 78 | + String encodedDatabase = config.get(JdbcUtils.DATABASE_KEY).asText(); |
| 79 | + if (encodedDatabase != null) { |
| 80 | + try { |
| 81 | + encodedDatabase = URLEncoder.encode(encodedDatabase, "UTF-8"); |
| 82 | + } catch (final UnsupportedEncodingException e) { |
| 83 | + // Should never happen |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + } |
| 87 | + final String jdbcUrl = String.format("jdbc:postgresql://%s:%s/%s?", |
| 88 | + config.get(JdbcUtils.HOST_KEY).asText(), |
| 89 | + config.get(JdbcUtils.PORT_KEY).asText(), |
| 90 | + encodedDatabase); |
| 91 | + |
| 92 | + final ImmutableMap.Builder<Object, Object> configBuilder = ImmutableMap.builder() |
| 93 | + .put(JdbcUtils.USERNAME_KEY, config.get(JdbcUtils.USERNAME_KEY).asText()) |
| 94 | + .put(JdbcUtils.JDBC_URL_KEY, jdbcUrl) |
| 95 | + .put(JdbcUtils.SCHEMA_KEY, schema); |
| 96 | + |
| 97 | + if (config.has(JdbcUtils.PASSWORD_KEY)) { |
| 98 | + configBuilder.put(JdbcUtils.PASSWORD_KEY, config.get(JdbcUtils.PASSWORD_KEY).asText()); |
| 99 | + } |
| 100 | + |
| 101 | + if (config.has(JdbcUtils.JDBC_URL_PARAMS_KEY)) { |
| 102 | + configBuilder.put(JdbcUtils.JDBC_URL_PARAMS_KEY, config.get(JdbcUtils.JDBC_URL_PARAMS_KEY).asText()); |
| 103 | + } |
| 104 | + |
| 105 | + return Jsons.jsonNode(configBuilder.build()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected JdbcDestinationHandler<YellowbrickState> getDestinationHandler(String databaseName, JdbcDatabase database, String rawTableSchema) { |
| 110 | + return new YellowbrickDestinationHandler(databaseName, database, rawTableSchema); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected JdbcSqlGenerator getSqlGenerator() { |
| 115 | + return new YellowbrickSqlGenerator(new YellowbrickSQLNameTransformer()); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected StreamAwareDataTransformer getDataTransformer(ParsedCatalog parsedCatalog, String defaultNamespace) { |
| 120 | + return new YellowbrickDataTransformer(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean isV2Destination() { |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected List<Migration<YellowbrickState>> getMigrations(JdbcDatabase database, |
| 130 | + String databaseName, |
| 131 | + SqlGenerator sqlGenerator, |
| 132 | + DestinationHandler<YellowbrickState> destinationHandler) { |
| 133 | + return List.of(); |
| 134 | + } |
| 135 | + |
| 136 | + public static void main(final String[] args) throws Exception { |
| 137 | + final Destination destination = YellowbrickDestination.sshWrappedDestination(); |
| 138 | + LOGGER.info("starting destination: {}", YellowbrickDestination.class); |
| 139 | + new IntegrationRunner(destination).run(args); |
| 140 | + LOGGER.info("completed destination: {}", YellowbrickDestination.class); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments