-
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.
[Feature/agent_framework] Add Get Workflow API to retrieve a stored
template by workflow id (#273) * renaming status API implementation Signed-off-by: Joshua Palis <jpalis@amazon.com> * Adding GetWorkflow API Signed-off-by: Joshua Palis <jpalis@amazon.com> * addressing PR comments Signed-off-by: Joshua Palis <jpalis@amazon.com> * Adding todo reminder Signed-off-by: Joshua Palis <jpalis@amazon.com> --------- Signed-off-by: Joshua Palis <jpalis@amazon.com>
- Loading branch information
Showing
14 changed files
with
742 additions
and
186 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
109 changes: 109 additions & 0 deletions
109
src/main/java/org/opensearch/flowframework/rest/RestGetWorkflowStateAction.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,109 @@ | ||
/* | ||
* 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.rest; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.ExceptionsHelper; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.flowframework.common.FlowFrameworkFeatureEnabledSetting; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.flowframework.transport.GetWorkflowStateAction; | ||
import org.opensearch.flowframework.transport.GetWorkflowStateRequest; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_ID; | ||
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_URI; | ||
import static org.opensearch.flowframework.common.FlowFrameworkSettings.FLOW_FRAMEWORK_ENABLED; | ||
|
||
/** | ||
* Rest Action to facilitate requests to get a workflow status | ||
*/ | ||
public class RestGetWorkflowStateAction extends BaseRestHandler { | ||
|
||
private static final String GET_WORKFLOW_STATE_ACTION = "get_workflow_state"; | ||
private static final Logger logger = LogManager.getLogger(RestGetWorkflowStateAction.class); | ||
private FlowFrameworkFeatureEnabledSetting flowFrameworkFeatureEnabledSetting; | ||
|
||
/** | ||
* Instantiates a new RestGetWorkflowStateAction | ||
* @param flowFrameworkFeatureEnabledSetting Whether this API is enabled | ||
*/ | ||
public RestGetWorkflowStateAction(FlowFrameworkFeatureEnabledSetting flowFrameworkFeatureEnabledSetting) { | ||
this.flowFrameworkFeatureEnabledSetting = flowFrameworkFeatureEnabledSetting; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return GET_WORKFLOW_STATE_ACTION; | ||
} | ||
|
||
@Override | ||
protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
|
||
try { | ||
if (!flowFrameworkFeatureEnabledSetting.isFlowFrameworkEnabled()) { | ||
throw new FlowFrameworkException( | ||
"This API is disabled. To enable it, update the setting [" + FLOW_FRAMEWORK_ENABLED.getKey() + "] to true.", | ||
RestStatus.FORBIDDEN | ||
); | ||
} | ||
|
||
// Validate content | ||
if (request.hasContent()) { | ||
throw new FlowFrameworkException("No request body present", RestStatus.BAD_REQUEST); | ||
} | ||
// Validate params | ||
String workflowId = request.param(WORKFLOW_ID); | ||
if (workflowId == null) { | ||
throw new FlowFrameworkException("workflow_id cannot be null", RestStatus.BAD_REQUEST); | ||
} | ||
|
||
boolean all = request.paramAsBoolean("all", false); | ||
GetWorkflowStateRequest getWorkflowRequest = new GetWorkflowStateRequest(workflowId, all); | ||
return channel -> client.execute(GetWorkflowStateAction.INSTANCE, getWorkflowRequest, ActionListener.wrap(response -> { | ||
XContentBuilder builder = response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS); | ||
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder)); | ||
}, exception -> { | ||
try { | ||
FlowFrameworkException ex = new FlowFrameworkException(exception.getMessage(), ExceptionsHelper.status(exception)); | ||
XContentBuilder exceptionBuilder = ex.toXContent(channel.newErrorBuilder(), ToXContent.EMPTY_PARAMS); | ||
channel.sendResponse(new BytesRestResponse(ex.getRestStatus(), exceptionBuilder)); | ||
|
||
} catch (IOException e) { | ||
logger.error("Failed to send back provision workflow exception", e); | ||
channel.sendResponse(new BytesRestResponse(ExceptionsHelper.status(e), e.getMessage())); | ||
} | ||
})); | ||
|
||
} catch (FlowFrameworkException ex) { | ||
return channel -> channel.sendResponse( | ||
new BytesRestResponse(ex.getRestStatus(), ex.toXContent(channel.newErrorBuilder(), ToXContent.EMPTY_PARAMS)) | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return ImmutableList.of( | ||
new Route(RestRequest.Method.GET, String.format(Locale.ROOT, "%s/{%s}/%s", WORKFLOW_URI, WORKFLOW_ID, "_status")) | ||
); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/org/opensearch/flowframework/transport/GetWorkflowStateAction.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,29 @@ | ||
/* | ||
* 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.transport; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
import static org.opensearch.flowframework.common.CommonValue.TRANSPORT_ACTION_NAME_PREFIX; | ||
|
||
/** | ||
* External Action for public facing RestGetWorkflowStateAction | ||
*/ | ||
public class GetWorkflowStateAction extends ActionType<GetWorkflowStateResponse> { | ||
// TODO : If the template body is returned as part of the GetWorkflowStateAction, | ||
// it is necessary to ensure the user has permissions for workflow/get | ||
/** The name of this action */ | ||
public static final String NAME = TRANSPORT_ACTION_NAME_PREFIX + "workflow_state/get"; | ||
/** An instance of this action */ | ||
public static final GetWorkflowStateAction INSTANCE = new GetWorkflowStateAction(); | ||
|
||
private GetWorkflowStateAction() { | ||
super(NAME, GetWorkflowStateResponse::new); | ||
} | ||
} |
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.