Search in sources :

Example 1 with Connection

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

the class RemoteConnectionSpecTask 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);
    ConnectorConfigurer connectorConfigurer = new DefaultConnectorConfigurer(pluginConfigurer);
    ConnectorContext connectorContext = ConnectionUtils.getConnectorContext(pluginConfigurer);
    SpecGenerationRequest specRequest = GSON.fromJson(request.getRequest(), SpecGenerationRequest.class);
    try (Connector connector = getConnector(systemAppContext, pluginConfigurer, connection.getPlugin(), namespace, pluginSelector)) {
        connector.configure(connectorConfigurer);
        ConnectorSpecRequest connectorSpecRequest = ConnectorSpecRequest.builder().setPath(specRequest.getPath()).setConnection(connection.getName()).setProperties(specRequest.getProperties()).build();
        ConnectorSpec spec = connector.generateSpec(connectorContext, connectorSpecRequest);
        ConnectorSpec newSpec = ConnectionUtils.filterSpecWithPluginNameAndType(spec, specRequest.getPluginName(), specRequest.getPluginType());
        return GSON.toJson(ConnectionUtils.getConnectorDetail(pluginSelector.getSelectedArtifact(), newSpec));
    }
}
Also used : Connector(io.cdap.cdap.etl.api.connector.Connector) ArtifactSelectorProvider(io.cdap.cdap.etl.common.ArtifactSelectorProvider) TrackedPluginSelector(io.cdap.cdap.etl.spec.TrackedPluginSelector) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectorContext(io.cdap.cdap.etl.api.connector.ConnectorContext) SpecGenerationRequest(io.cdap.cdap.etl.proto.connection.SpecGenerationRequest) ConnectorSpecRequest(io.cdap.cdap.etl.api.connector.ConnectorSpecRequest) ConnectorSpec(io.cdap.cdap.etl.api.connector.ConnectorSpec) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) DefaultConnectorConfigurer(io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer) ConnectorConfigurer(io.cdap.cdap.etl.api.connector.ConnectorConfigurer) ServicePluginConfigurer(io.cdap.cdap.api.service.http.ServicePluginConfigurer)

Example 2 with Connection

use of io.cdap.cdap.etl.proto.connection.Connection 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 3 with Connection

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

the class ConnectionHandler method deleteConnection.

/**
 * Delete a connection in the given namespace
 */
@DELETE
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}")
public void deleteConnection(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Deleting connection in system namespace is currently not supported");
            return;
        }
        ConnectionId connectionId = new ConnectionId(namespaceSummary, connection);
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, connectionId.getConnectionId()), StandardPermission.DELETE);
        Connection oldConnection = store.getConnection(connectionId);
        store.deleteConnection(connectionId);
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, oldConnection.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_DELETED_COUNT, 1);
        responder.sendStatus(HttpURLConnection.HTTP_OK);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 4 with Connection

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

the class ConnectionHandler method getConnection.

/**
 * Returns the specific connection information in the given namespace
 */
@GET
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}")
public void getConnection(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Getting connection in system namespace is currently not supported");
            return;
        }
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, ConnectionId.getConnectionId(connection)), StandardPermission.GET);
        Connection conn = store.getConnection(new ConnectionId(namespaceSummary, connection));
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, conn.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_GET_COUNT, 1);
        responder.sendJson(conn);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 5 with Connection

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

the class ConnectionHandler method createConnection.

/**
 * Creates a connection in the given namespace
 */
@PUT
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}")
public void createConnection(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Creating connection in system namespace is currently not supported");
            return;
        }
        ConnectionId connectionId = new ConnectionId(namespaceSummary, connection);
        checkPutConnectionPermissions(connectionId);
        ConnectionCreationRequest creationRequest = GSON.fromJson(StandardCharsets.UTF_8.decode(request.getContent()).toString(), ConnectionCreationRequest.class);
        String connType = creationRequest.getPlugin().getName();
        if (disabledTypes.contains(connType)) {
            throw new ConnectionBadRequestException(String.format("Connection type %s is disabled, connection cannot be created", connType));
        }
        long now = System.currentTimeMillis();
        Connection connectionInfo = new Connection(connection, connectionId.getConnectionId(), connType, creationRequest.getDescription(), false, false, now, now, creationRequest.getPlugin());
        store.saveConnection(connectionId, connectionInfo, creationRequest.overWrite());
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, connectionInfo.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_COUNT, 1);
        responder.sendStatus(HttpURLConnection.HTTP_OK);
    });
}
Also used : ConnectionBadRequestException(io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException) ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectionCreationRequest(io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest) Path(javax.ws.rs.Path) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy) PUT(javax.ws.rs.PUT)

Aggregations

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