use of io.cdap.cdap.etl.proto.connection.Connection in project cdap by cdapio.
the class ConnectionStore method listConnections.
/**
* Get all the connections in the given namespace
*
* @param namespace the namespace to look up
* @return the list of connections in this namespace
*/
public List<Connection> listConnections(NamespaceSummary namespace) {
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
Range range = Range.singleton(getNamespaceKeys(namespace));
List<Connection> connections = new ArrayList<>();
try (CloseableIterator<StructuredRow> rowIter = table.scan(range, Integer.MAX_VALUE)) {
rowIter.forEachRemaining(structuredRow -> connections.add(GSON.fromJson(structuredRow.getString(CONNECTION_DATA_FIELD), Connection.class)));
}
return connections;
});
}
use of io.cdap.cdap.etl.proto.connection.Connection in project cdap by cdapio.
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);
}
use of io.cdap.cdap.etl.proto.connection.Connection in project cdap by cdapio.
the class RemoteConnectionSampleTask method execute.
@Override
public String execute(SystemAppTaskContext systemAppContext, RemoteConnectionRequest request) throws Exception {
String namespace = request.getNamespace();
Connection connection = request.getConnection();
// Plugin selector and configurer
TrackedPluginSelector pluginSelector = new TrackedPluginSelector(new ArtifactSelectorProvider().getPluginSelector(connection.getPlugin().getArtifact()));
ServicePluginConfigurer pluginConfigurer = systemAppContext.createServicePluginConfigurer(namespace);
ConnectorContext connectorContext = ConnectionUtils.getConnectorContext(pluginConfigurer);
SampleRequest sampleRequest = GSON.fromJson(request.getRequest(), SampleRequest.class);
try (Connector connector = getConnector(systemAppContext, pluginConfigurer, connection.getPlugin(), namespace, pluginSelector)) {
connector.configure(new DefaultConnectorConfigurer(pluginConfigurer));
ConnectorSpecRequest specRequest = ConnectorSpecRequest.builder().setPath(sampleRequest.getPath()).setConnection(connection.getName()).setProperties(sampleRequest.getProperties()).build();
ConnectorSpec spec = connector.generateSpec(connectorContext, specRequest);
ConnectorDetail detail = ConnectionUtils.getConnectorDetail(pluginSelector.getSelectedArtifact(), spec);
SampleResponse sampleResponse = ConnectionUtils.getSampleResponse(connector, connectorContext, sampleRequest, detail, pluginConfigurer);
return GSON.toJson(sampleResponse);
}
}
use of io.cdap.cdap.etl.proto.connection.Connection in project cdap by cdapio.
the class ConnectionStoreTest method testNotFound.
@Test
public void testNotFound() throws Exception {
ConnectionId connectionId = new ConnectionId(new NamespaceSummary("default", "", 0L), "nonexisting");
// get non-existing connection
try {
connectionStore.getConnection(connectionId);
Assert.fail();
} catch (ConnectionNotFoundException e) {
// expected
}
try {
connectionStore.deleteConnection(connectionId);
Assert.fail();
} catch (ConnectionNotFoundException e) {
// expected
}
// put a connection in the store
NamespaceSummary oldNamespace = new NamespaceSummary("default", "", 0L);
ConnectionId id = new ConnectionId(oldNamespace, "myconn");
Connection connection = new Connection("myconn", "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(id, connection, false);
// get the connection with a namespace with a higher generation id should fail
try {
connectionStore.getConnection(new ConnectionId(new NamespaceSummary("default", "", 1L), "myconn"));
Assert.fail();
} catch (ConnectionNotFoundException e) {
// expected
}
}
use of io.cdap.cdap.etl.proto.connection.Connection in project cdap by caskdata.
the class ConnectionMacroEvaluator method evaluateMacroMap.
/**
* Evaluates the connection macro function by calling the Connection service to retrieve the connection information.
*
* @param args should contains exactly one arguments. The argument should contain the connection name
* @return the json representation of the properties of the connection
*/
@Override
Map<String, String> evaluateMacroMap(String macroFunction, String... args) throws InvalidMacroException, IOException, RetryableException {
if (args.length != 1) {
throw new InvalidMacroException("Macro '" + FUNCTION_NAME + "' should have exactly 1 arguments");
}
// only encode the connection name here since / will get encoded to %2f and some router cannot recognize it
// we don't need to worry about space getting converted to plus here since connection lookup is based on id,
// space and plus both get converted to _ in the id
String connName = URLEncoder.encode(args[0], StandardCharsets.UTF_8.name());
HttpURLConnection urlConn = serviceDiscoverer.openConnection(NamespaceId.SYSTEM.getNamespace(), Constants.PIPELINEID, Constants.STUDIO_SERVICE_NAME, String.format("v1/contexts/%s/connections/%s", namespace, connName));
Connection connection = gson.fromJson(validateAndRetrieveContent(SERVICE_NAME, urlConn), Connection.class);
return connection.getPlugin().getProperties();
}
Aggregations