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
}
}
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);
});
}
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
}
}
}
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);
}
Aggregations