Search in sources :

Example 1 with ConnectionConflictException

use of io.cdap.cdap.etl.proto.connection.ConnectionConflictException in project cdap by caskdata.

the class ConnectionStoreTest method testConflict.

@Test
public void testConflict() throws Exception {
    // put a connection in the store
    NamespaceSummary namespace = new NamespaceSummary("default", "", 0L);
    ConnectionId connectionId = new ConnectionId(namespace, "my_conn");
    Connection connection = new Connection("my_conn", "GCS", "GCS connection", false, false, 0L, 0L, new PluginInfo("GCS", "connector", "Google Cloud Platform", ImmutableMap.of("project", "abc"), new ArtifactSelectorConfig("SYSTEM", "google-cloud", "1.0.0")));
    connectionStore.saveConnection(connectionId, connection, false);
    // use a different name but evaluate to same id, with overwrite to false, it should fail to update
    try {
        connectionId = new ConnectionId(namespace, "my conn");
        connection = new Connection("my conn", "GCS", "GCS connection", false, false, 0L, 0L, new PluginInfo("GCS", "connector", "Google Cloud Platform", ImmutableMap.of("project", "abc"), new ArtifactSelectorConfig("SYSTEM", "google-cloud", "1.0.0")));
        connectionStore.saveConnection(connectionId, connection, false);
        Assert.fail();
    } catch (ConnectionConflictException e) {
    // expected
    }
    // update the same name should also fail
    try {
        connectionId = new ConnectionId(namespace, "my_conn");
        connection = new Connection("my conn", "GCS", "GCS connection", false, false, 0L, 0L, new PluginInfo("GCS", "connector", "Google Cloud Platform", ImmutableMap.of("project", "abc"), new ArtifactSelectorConfig("SYSTEM", "google-cloud", "1.0.0")));
        connectionStore.saveConnection(connectionId, connection, false);
        Assert.fail();
    } catch (ConnectionConflictException e) {
    // expected
    }
    // check pre configured connection cannot get updated
    connectionId = new ConnectionId(namespace, "default conn");
    connection = new Connection("default conn", "GCS", "GCS connection", true, false, 0L, 0L, new PluginInfo("GCS", "connector", "Google Cloud Platform", ImmutableMap.of("project", "abc"), new ArtifactSelectorConfig("SYSTEM", "google-cloud", "1.0.0")));
    connectionStore.saveConnection(connectionId, connection, false);
    try {
        connection = new Connection("default conn", "BigQuery", "", false, false, 0L, 0L, new PluginInfo("BigQuery", "connector", "", Collections.emptyMap(), new ArtifactSelectorConfig()));
        connectionStore.saveConnection(connectionId, connection, true);
        Assert.fail();
    } catch (ConnectionConflictException e) {
    // expected
    }
    // and pre-configured cannot be deleted
    try {
        connectionStore.deleteConnection(connectionId);
        Assert.fail();
    } catch (ConnectionConflictException e) {
    // expected
    }
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) ArtifactSelectorConfig(io.cdap.cdap.etl.proto.ArtifactSelectorConfig) Connection(io.cdap.cdap.etl.proto.connection.Connection) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) PluginInfo(io.cdap.cdap.etl.proto.connection.PluginInfo) Test(org.junit.Test)

Example 2 with ConnectionConflictException

use of io.cdap.cdap.etl.proto.connection.ConnectionConflictException in project cdap by caskdata.

the class ConnectionStore method saveConnection.

/**
 * Save the connection in the store.
 *
 * @param connectionId the connection id
 * @param connection the connection information
 * @param overWrite flag indicating whether the store should overwrite an existing connection with same connection id
 *                  but different connection name, i.e, a b and a.b both convert to id a_b
 */
public void saveConnection(ConnectionId connectionId, Connection connection, boolean overWrite) {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        Connection oldConnection = getConnectionInternal(table, connectionId, false);
        Connection newConnection = connection;
        if (oldConnection != null) {
            if (oldConnection.isPreConfigured()) {
                throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s and is pre-configured. " + "Preconfigured connections cannot be updated or overwritten.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
            }
            if (!oldConnection.getName().equals(newConnection.getName()) && !overWrite) {
                throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s. Please choose a different connection name.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
            }
            newConnection = new Connection(connection.getName(), oldConnection.getConnectionId(), connection.getConnectionType(), connection.getDescription(), connection.isPreConfigured(), connection.isDefault(), oldConnection.getCreatedTimeMillis(), connection.getUpdatedTimeMillis(), connection.getPlugin());
        }
        Collection<Field<?>> fields = getConnectionKeys(connectionId);
        fields.add(Fields.longField(CREATED_COL, newConnection.getCreatedTimeMillis()));
        fields.add(Fields.longField(UPDATED_COL, newConnection.getUpdatedTimeMillis()));
        fields.add(Fields.stringField(CONNECTION_DATA_FIELD, GSON.toJson(newConnection)));
        table.upsert(fields);
    });
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Connection(io.cdap.cdap.etl.proto.connection.Connection)

Example 3 with ConnectionConflictException

use of io.cdap.cdap.etl.proto.connection.ConnectionConflictException in project cdap by caskdata.

the class StudioService method createPreconfiguredConnections.

private void createPreconfiguredConnections(SystemServiceContext context) throws IOException {
    ConnectionStore connectionStore = new ConnectionStore(context);
    for (PreconfiguredConnectionCreationRequest creationRequest : connectionConfig.getConnections()) {
        if (creationRequest.getName() == null || creationRequest.getNamespace() == null) {
            continue;
        }
        NamespaceSummary namespaceSummary = context.getAdmin().getNamespaceSummary(creationRequest.getNamespace());
        if (namespaceSummary == null) {
            LOG.warn("Namespace {} does not exist, skipping creating connection {}", creationRequest.getNamespace(), creationRequest.getName());
        }
        ConnectionId connectionId = new ConnectionId(namespaceSummary, creationRequest.getName());
        long now = System.currentTimeMillis();
        Connection connectionInfo = new Connection(creationRequest.getName(), connectionId.getConnectionId(), creationRequest.getPlugin().getName(), creationRequest.getDescription(), true, creationRequest.getName().equals(connectionConfig.getDefaultConnection()) ? true : false, now, now, creationRequest.getPlugin());
        try {
            connectionStore.saveConnection(connectionId, connectionInfo, false);
        } catch (ConnectionConflictException e) {
        // expected if the connection is already created
        }
    }
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Connection(io.cdap.cdap.etl.proto.connection.Connection) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) ConnectionStore(io.cdap.cdap.datapipeline.connection.ConnectionStore) PreconfiguredConnectionCreationRequest(io.cdap.cdap.etl.proto.connection.PreconfiguredConnectionCreationRequest)

Example 4 with ConnectionConflictException

use of io.cdap.cdap.etl.proto.connection.ConnectionConflictException in project cdap by caskdata.

the class ConnectionStore method deleteConnection.

/**
 * Delete the given connection
 *
 * @param connectionId the connection id to delete
 * @throws ConnectionNotFoundException if the connection is not found
 */
public void deleteConnection(ConnectionId connectionId) throws ConnectionNotFoundException {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        Connection oldConnection = getConnectionInternal(table, connectionId, true);
        if (oldConnection.isPreConfigured()) {
            throw new ConnectionConflictException(String.format("Connection %s in namespace %s is pre-configured and it cannot be deleted.", connectionId.getConnection(), connectionId.getNamespace()));
        }
        table.delete(getConnectionKeys(connectionId));
    }, ConnectionNotFoundException.class);
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Connection(io.cdap.cdap.etl.proto.connection.Connection)

Aggregations

Connection (io.cdap.cdap.etl.proto.connection.Connection)4 ConnectionConflictException (io.cdap.cdap.etl.proto.connection.ConnectionConflictException)4 NamespaceSummary (io.cdap.cdap.api.NamespaceSummary)2 ConnectionId (io.cdap.cdap.etl.proto.connection.ConnectionId)2 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)2 ConnectionStore (io.cdap.cdap.datapipeline.connection.ConnectionStore)1 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)1 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)1 PreconfiguredConnectionCreationRequest (io.cdap.cdap.etl.proto.connection.PreconfiguredConnectionCreationRequest)1 Field (io.cdap.cdap.spi.data.table.field.Field)1 Test (org.junit.Test)1