forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a new API to get the current transformed pipelines as a JSON (o…
…pensearch-project#4980) * Adding a new API to get the current transformed pipelines as a JSON Signed-off-by: Souvik Bose <[email protected]> * Rename the api and address comments Signed-off-by: Souvik Bose <[email protected]> --------- Signed-off-by: Souvik Bose <[email protected]> Co-authored-by: Souvik Bose <[email protected]>
- Loading branch information
Showing
8 changed files
with
214 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...er-core/src/main/java/org/opensearch/dataprepper/pipeline/server/GetPipelinesHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.server; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import org.opensearch.dataprepper.model.configuration.PipelineModel; | ||
import org.opensearch.dataprepper.pipeline.PipelinesProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.HttpMethod; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class GetPipelinesHandler implements HttpHandler { | ||
|
||
private final PipelinesProvider pipelinesProvider; | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
private final Logger LOG = LoggerFactory.getLogger(GetPipelinesHandler.class); | ||
|
||
public GetPipelinesHandler(final PipelinesProvider pipelinesProvider) { | ||
this.pipelinesProvider = pipelinesProvider; | ||
} | ||
|
||
@Override | ||
public void handle(final HttpExchange exchange) throws IOException { | ||
String requestMethod = exchange.getRequestMethod(); | ||
if (!requestMethod.equals(HttpMethod.GET)) { | ||
exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_METHOD, 0); | ||
exchange.getResponseBody().close(); | ||
return; | ||
} | ||
|
||
try { | ||
List<PipelineModel> pipelineModels = new ArrayList<>(pipelinesProvider.getPipelinesDataFlowModel().getPipelines().values()); | ||
|
||
final byte[] response = OBJECT_MAPPER.writeValueAsString(pipelineModels).getBytes(); | ||
|
||
exchange.getResponseHeaders().add("Content-Type", "text/plain; charset=UTF-8"); | ||
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); | ||
exchange.getResponseBody().write(response); | ||
} catch (final Exception e) { | ||
LOG.error("Caught exception listing pipelines", e); | ||
exchange.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, 0); | ||
} finally { | ||
exchange.getResponseBody().close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...ore/src/test/java/org/opensearch/dataprepper/pipeline/server/GetPipelinesHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.server; | ||
|
||
import com.sun.net.httpserver.Headers; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.model.configuration.DataPrepperVersion; | ||
import org.opensearch.dataprepper.model.configuration.PipelineModel; | ||
import org.opensearch.dataprepper.model.configuration.PipelinesDataFlowModel; | ||
import org.opensearch.dataprepper.model.configuration.PluginModel; | ||
import org.opensearch.dataprepper.model.configuration.SinkModel; | ||
import org.opensearch.dataprepper.pipeline.PipelinesProvider; | ||
|
||
import javax.ws.rs.HttpMethod; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class GetPipelinesHandlerTest { | ||
@Mock | ||
private PipelinesProvider pipelinesProvider; | ||
@Mock | ||
private HttpExchange httpExchange; | ||
|
||
@Mock | ||
private OutputStream outputStream; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
when(httpExchange.getResponseBody()) | ||
.thenReturn(outputStream); | ||
} | ||
|
||
private GetPipelinesHandler createObjectUnderTest() { | ||
return new GetPipelinesHandler(pipelinesProvider); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { HttpMethod.GET }) | ||
public void testGivenPipelinesThenResponseWritten(String httpMethod) throws IOException { | ||
final String pipelineName = "test-pipeline"; | ||
final Headers headers = mock(Headers.class); | ||
doNothing().when(headers).add(anyString(), anyString()); | ||
final DataPrepperVersion version = DataPrepperVersion.parse("2.0"); | ||
final PluginModel source = new PluginModel("testSource", (Map<String, Object>) null); | ||
final List<PluginModel> processors = Collections.singletonList(new PluginModel("testProcessor", (Map<String, Object>) null)); | ||
final List<SinkModel> sinks = Collections.singletonList(new SinkModel("testSink", Collections.emptyList(), null, Collections.emptyList(), Collections.emptyList(), null)); | ||
final PipelineModel pipelineModel = new PipelineModel(source, null, processors, null, sinks, 8, 50); | ||
|
||
final PipelinesDataFlowModel pipelinesDataFlowModel = new PipelinesDataFlowModel(version, Collections.singletonMap(pipelineName, pipelineModel)); | ||
|
||
when(pipelinesProvider.getPipelinesDataFlowModel()) | ||
.thenReturn(pipelinesDataFlowModel); | ||
when(httpExchange.getResponseHeaders()) | ||
.thenReturn(headers); | ||
when(httpExchange.getRequestMethod()) | ||
.thenReturn(httpMethod); | ||
|
||
final GetPipelinesHandler handler = createObjectUnderTest(); | ||
|
||
handler.handle(httpExchange); | ||
|
||
verify(headers) | ||
.add(eq("Content-Type"), eq("text/plain; charset=UTF-8")); | ||
verify(httpExchange) | ||
.sendResponseHeaders(eq(HttpURLConnection.HTTP_OK), anyLong()); | ||
verify(outputStream) | ||
.write(any(byte[].class)); | ||
verify(outputStream) | ||
.close(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { HttpMethod.DELETE, HttpMethod.PATCH, HttpMethod.PUT, HttpMethod.POST }) | ||
public void testGivenProhibitedHttpMethodThenErrorResponseWritten(String httpMethod) throws IOException { | ||
final GetPipelinesHandler handler = createObjectUnderTest(); | ||
|
||
when(httpExchange.getRequestMethod()) | ||
.thenReturn(httpMethod); | ||
|
||
handler.handle(httpExchange); | ||
|
||
verify(httpExchange) | ||
.sendResponseHeaders(eq(HttpURLConnection.HTTP_BAD_METHOD), eq(0L)); | ||
verify(outputStream) | ||
.close(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { HttpMethod.GET }) | ||
public void testGivenExceptionThrownThenErrorResponseWritten(String httpMethod) throws IOException { | ||
when(httpExchange.getRequestMethod()) | ||
.thenReturn(httpMethod); | ||
|
||
pipelinesProvider = null; | ||
final GetPipelinesHandler handler = createObjectUnderTest(); | ||
handler.handle(httpExchange); | ||
|
||
verify(httpExchange) | ||
.sendResponseHeaders(eq(HttpURLConnection.HTTP_INTERNAL_ERROR), eq(0L)); | ||
verify(outputStream) | ||
.close(); | ||
} | ||
} |