-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daniel Widdis <[email protected]>
- Loading branch information
Showing
6 changed files
with
246 additions
and
2 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
103 changes: 103 additions & 0 deletions
103
src/main/java/org/opensearch/flowframework/workflow/HttpHostStep.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,103 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.workflow; | ||
|
||
import org.apache.hc.core5.http.HttpHost; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.action.support.PlainActionFuture; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.flowframework.util.ParseUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.flowframework.common.CommonValue.HOSTNAME_FIELD; | ||
import static org.opensearch.flowframework.common.CommonValue.HTTP_HOST_FIELD; | ||
import static org.opensearch.flowframework.common.CommonValue.PORT_FIELD; | ||
import static org.opensearch.flowframework.common.CommonValue.SCHEME_FIELD; | ||
|
||
/** | ||
* Step to register parameters for an HTTP Connection to a Host | ||
*/ | ||
public class HttpHostStep implements WorkflowStep { | ||
|
||
private static final Logger logger = LogManager.getLogger(HttpHostStep.class); | ||
PlainActionFuture<WorkflowData> hostFuture = PlainActionFuture.newFuture(); | ||
static final String NAME = "http_host"; | ||
|
||
@Override | ||
public PlainActionFuture<WorkflowData> execute( | ||
String currentNodeId, | ||
WorkflowData currentNodeInputs, | ||
Map<String, WorkflowData> outputs, | ||
Map<String, String> previousNodeInputs | ||
) { | ||
Set<String> requiredKeys = Set.of(SCHEME_FIELD, HOSTNAME_FIELD, PORT_FIELD); | ||
Set<String> optionalKeys = Collections.emptySet(); | ||
|
||
try { | ||
Map<String, Object> inputs = ParseUtils.getInputsFromPreviousSteps( | ||
requiredKeys, | ||
optionalKeys, | ||
currentNodeInputs, | ||
outputs, | ||
previousNodeInputs | ||
); | ||
|
||
String scheme = validScheme(inputs.get(SCHEME_FIELD)); | ||
String hostname = inputs.get(HOSTNAME_FIELD).toString(); | ||
int port = validPort(inputs.get(PORT_FIELD)); | ||
|
||
HttpHost httpHost = new HttpHost(scheme, hostname, port); | ||
|
||
hostFuture.onResponse( | ||
new WorkflowData( | ||
Map.ofEntries(Map.entry(HTTP_HOST_FIELD, httpHost)), | ||
currentNodeInputs.getWorkflowId(), | ||
currentNodeInputs.getNodeId() | ||
) | ||
); | ||
|
||
logger.info("Http Host registered successfully {}", httpHost); | ||
|
||
} catch (FlowFrameworkException e) { | ||
hostFuture.onFailure(e); | ||
} | ||
return hostFuture; | ||
} | ||
|
||
private String validScheme(Object o) { | ||
String scheme = o.toString().toLowerCase(Locale.ROOT); | ||
if ("http".equals(scheme) || "https".equals(scheme)) { | ||
return scheme; | ||
} | ||
throw new FlowFrameworkException("http_host scheme must be http or https", RestStatus.BAD_REQUEST); | ||
} | ||
|
||
private int validPort(Object o) { | ||
try { | ||
int port = Integer.parseInt(o.toString()); | ||
if (port < 0 || port > 65535) { | ||
throw new FlowFrameworkException("http_host port number must be between 0 and 65535", RestStatus.BAD_REQUEST); | ||
} | ||
return port; | ||
} catch (NumberFormatException e) { | ||
throw new FlowFrameworkException("http_host port must be a number between 0 and 65535", RestStatus.BAD_REQUEST); | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
src/test/java/org/opensearch/flowframework/workflow/HttpHostStepTests.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,117 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.workflow; | ||
|
||
import org.apache.hc.core5.http.HttpHost; | ||
import org.opensearch.action.support.PlainActionFuture; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.opensearch.flowframework.common.CommonValue.HOSTNAME_FIELD; | ||
import static org.opensearch.flowframework.common.CommonValue.HTTP_HOST_FIELD; | ||
import static org.opensearch.flowframework.common.CommonValue.PORT_FIELD; | ||
import static org.opensearch.flowframework.common.CommonValue.SCHEME_FIELD; | ||
|
||
public class HttpHostStepTests extends OpenSearchTestCase { | ||
|
||
public void testHttpHost() throws InterruptedException, ExecutionException { | ||
HttpHostStep httpHostStep = new HttpHostStep(); | ||
assertEquals(HttpHostStep.NAME, httpHostStep.getName()); | ||
|
||
WorkflowData inputData = new WorkflowData( | ||
Map.ofEntries(Map.entry(SCHEME_FIELD, "http"), Map.entry(HOSTNAME_FIELD, "localhost"), Map.entry(PORT_FIELD, 1234)), | ||
"test-id", | ||
"test-node-id" | ||
); | ||
|
||
PlainActionFuture<WorkflowData> future = httpHostStep.execute( | ||
inputData.getNodeId(), | ||
inputData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
|
||
assertTrue(future.isDone()); | ||
assertEquals(HttpHost.class, future.get().getContent().get(HTTP_HOST_FIELD).getClass()); | ||
HttpHost host = (HttpHost) future.get().getContent().get(HTTP_HOST_FIELD); | ||
assertEquals("http", host.getSchemeName()); | ||
assertEquals("localhost", host.getHostName()); | ||
assertEquals(1234, host.getPort()); | ||
} | ||
|
||
public void testBadScheme() { | ||
HttpHostStep httpHostStep = new HttpHostStep(); | ||
|
||
WorkflowData badSchemeData = new WorkflowData( | ||
Map.ofEntries(Map.entry(SCHEME_FIELD, "ftp"), Map.entry(HOSTNAME_FIELD, "localhost"), Map.entry(PORT_FIELD, 1234)), | ||
"test-id", | ||
"test-node-id" | ||
); | ||
|
||
PlainActionFuture<WorkflowData> future = httpHostStep.execute( | ||
badSchemeData.getNodeId(), | ||
badSchemeData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
|
||
assertTrue(future.isDone()); | ||
ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get()); | ||
assertEquals(FlowFrameworkException.class, ex.getCause().getClass()); | ||
assertEquals("http_host scheme must be http or https", ex.getCause().getMessage()); | ||
} | ||
|
||
public void testBadPort() { | ||
HttpHostStep httpHostStep = new HttpHostStep(); | ||
|
||
WorkflowData badPortData = new WorkflowData( | ||
Map.ofEntries(Map.entry(SCHEME_FIELD, "https"), Map.entry(HOSTNAME_FIELD, "localhost"), Map.entry(PORT_FIELD, 123456)), | ||
"test-id", | ||
"test-node-id" | ||
); | ||
|
||
PlainActionFuture<WorkflowData> future = httpHostStep.execute( | ||
badPortData.getNodeId(), | ||
badPortData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
|
||
assertTrue(future.isDone()); | ||
ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get()); | ||
assertEquals(FlowFrameworkException.class, ex.getCause().getClass()); | ||
assertEquals("http_host port number must be between 0 and 65535", ex.getCause().getMessage()); | ||
} | ||
|
||
public void testNoParsePort() { | ||
HttpHostStep httpHostStep = new HttpHostStep(); | ||
|
||
WorkflowData noParsePortData = new WorkflowData( | ||
Map.ofEntries(Map.entry(SCHEME_FIELD, "https"), Map.entry(HOSTNAME_FIELD, "localhost"), Map.entry(PORT_FIELD, "doesn't parse")), | ||
"test-id", | ||
"test-node-id" | ||
); | ||
|
||
PlainActionFuture<WorkflowData> future = httpHostStep.execute( | ||
noParsePortData.getNodeId(), | ||
noParsePortData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
|
||
assertTrue(future.isDone()); | ||
ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get()); | ||
assertEquals(FlowFrameworkException.class, ex.getCause().getClass()); | ||
assertEquals("http_host port must be a number between 0 and 65535", ex.getCause().getMessage()); | ||
} | ||
} |