-
Notifications
You must be signed in to change notification settings - Fork 37
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 <widdis@gmail.com>
- Loading branch information
Showing
7 changed files
with
255 additions
and
61 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 & 0xffff0000) != 0) { | ||
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
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
Oops, something went wrong.