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