Search in sources :

Example 16 with Connection

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;
    });
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Connection(io.cdap.cdap.etl.proto.connection.Connection) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Range(io.cdap.cdap.spi.data.table.field.Range)

Example 17 with Connection

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

Example 18 with Connection

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);
    }
}
Also used : Connector(io.cdap.cdap.etl.api.connector.Connector) ArtifactSelectorProvider(io.cdap.cdap.etl.common.ArtifactSelectorProvider) SampleResponse(io.cdap.cdap.etl.proto.connection.SampleResponse) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectorDetail(io.cdap.cdap.etl.proto.connection.ConnectorDetail) ConnectorSpec(io.cdap.cdap.etl.api.connector.ConnectorSpec) SampleRequest(io.cdap.cdap.etl.api.connector.SampleRequest) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) TrackedPluginSelector(io.cdap.cdap.etl.spec.TrackedPluginSelector) ConnectorContext(io.cdap.cdap.etl.api.connector.ConnectorContext) ConnectorSpecRequest(io.cdap.cdap.etl.api.connector.ConnectorSpecRequest) ServicePluginConfigurer(io.cdap.cdap.api.service.http.ServicePluginConfigurer)

Example 19 with Connection

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
    }
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) ConnectionNotFoundException(io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException) 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 20 with Connection

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();
}
Also used : InvalidMacroException(io.cdap.cdap.api.macro.InvalidMacroException) HttpURLConnection(java.net.HttpURLConnection) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection)

Aggregations

Connection (io.cdap.cdap.etl.proto.connection.Connection)34 ConnectionId (io.cdap.cdap.etl.proto.connection.ConnectionId)20 HttpURLConnection (java.net.HttpURLConnection)14 TransactionPolicy (io.cdap.cdap.api.annotation.TransactionPolicy)12 Metrics (io.cdap.cdap.api.metrics.Metrics)12 Path (javax.ws.rs.Path)12 ConnectionEntityId (io.cdap.cdap.proto.id.ConnectionEntityId)10 NamespaceSummary (io.cdap.cdap.api.NamespaceSummary)8 ConnectionConflictException (io.cdap.cdap.etl.proto.connection.ConnectionConflictException)8 ServicePluginConfigurer (io.cdap.cdap.api.service.http.ServicePluginConfigurer)6 DefaultConnectorConfigurer (io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer)6 Connector (io.cdap.cdap.etl.api.connector.Connector)6 ConnectorContext (io.cdap.cdap.etl.api.connector.ConnectorContext)6 ArtifactSelectorProvider (io.cdap.cdap.etl.common.ArtifactSelectorProvider)6 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)6 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)6 TrackedPluginSelector (io.cdap.cdap.etl.spec.TrackedPluginSelector)6 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)6 POST (javax.ws.rs.POST)6 Test (org.junit.Test)6