Search in sources :

Example 1 with SpecGenerationRequest

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

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

the class ConnectionHandler method spec.

/**
 * Retrieve the spec for the connector, which can be used in a source/sink
 */
@POST
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}/specification")
public void spec(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, "Generating connector spec in system namespace is currently not supported");
            return;
        }
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, ConnectionId.getConnectionId(connection)), StandardPermission.USE);
        String specGenerationRequestString = StandardCharsets.UTF_8.decode(request.getContent()).toString();
        SpecGenerationRequest specRequest = GSON.fromJson(specGenerationRequestString, SpecGenerationRequest.class);
        if (specRequest == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "The request body is empty");
            return;
        }
        if (specRequest.getPath() == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Path is not provided in the sample request");
            return;
        }
        Connection conn = store.getConnection(new ConnectionId(namespaceSummary, connection));
        if (getContext().isRemoteTaskEnabled()) {
            executeRemotely(namespace, specGenerationRequestString, conn, RemoteConnectionSpecTask.class, responder);
        } else {
            specGenerationLocally(namespaceSummary.getName(), specRequest, conn, responder);
        }
        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_SPEC_COUNT, 1);
    });
}
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) SpecGenerationRequest(io.cdap.cdap.etl.proto.connection.SpecGenerationRequest) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 3 with SpecGenerationRequest

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

the class DataPipelineConnectionTest method getConnectionSpec.

private ConnectorDetail getConnectionSpec(String connection, String path, @Nullable String pluginName, @Nullable String pluginType) throws IOException {
    String url = URLEncoder.encode(String.format("v1/contexts/%s/connections/%s/specification", NamespaceId.DEFAULT.getNamespace(), connection), StandardCharsets.UTF_8.name());
    URL validatePipelineURL = serviceURI.resolve(url).toURL();
    HttpRequest.Builder request = HttpRequest.builder(HttpMethod.POST, validatePipelineURL).withBody(GSON.toJson(new SpecGenerationRequest(path, Collections.emptyMap(), pluginName, pluginType)));
    HttpResponse response = executeRequest(request);
    Assert.assertEquals("Wrong answer: " + response.getResponseBodyAsString(), expectedCode, response.getResponseCode());
    return expectedCode != HttpURLConnection.HTTP_OK ? null : GSON.fromJson(response.getResponseBodyAsString(), ConnectorDetail.class);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) ConnectorDetail(io.cdap.cdap.etl.proto.connection.ConnectorDetail) SpecGenerationRequest(io.cdap.cdap.etl.proto.connection.SpecGenerationRequest) URL(java.net.URL)

Aggregations

SpecGenerationRequest (io.cdap.cdap.etl.proto.connection.SpecGenerationRequest)3 Connection (io.cdap.cdap.etl.proto.connection.Connection)2 TransactionPolicy (io.cdap.cdap.api.annotation.TransactionPolicy)1 Metrics (io.cdap.cdap.api.metrics.Metrics)1 ServicePluginConfigurer (io.cdap.cdap.api.service.http.ServicePluginConfigurer)1 DefaultConnectorConfigurer (io.cdap.cdap.datapipeline.connection.DefaultConnectorConfigurer)1 Connector (io.cdap.cdap.etl.api.connector.Connector)1 ConnectorConfigurer (io.cdap.cdap.etl.api.connector.ConnectorConfigurer)1 ConnectorContext (io.cdap.cdap.etl.api.connector.ConnectorContext)1 ConnectorSpec (io.cdap.cdap.etl.api.connector.ConnectorSpec)1 ConnectorSpecRequest (io.cdap.cdap.etl.api.connector.ConnectorSpecRequest)1 ArtifactSelectorProvider (io.cdap.cdap.etl.common.ArtifactSelectorProvider)1 ConnectionId (io.cdap.cdap.etl.proto.connection.ConnectionId)1 ConnectorDetail (io.cdap.cdap.etl.proto.connection.ConnectorDetail)1 TrackedPluginSelector (io.cdap.cdap.etl.spec.TrackedPluginSelector)1 ConnectionEntityId (io.cdap.cdap.proto.id.ConnectionEntityId)1 HttpRequest (io.cdap.common.http.HttpRequest)1 HttpResponse (io.cdap.common.http.HttpResponse)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1