-
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 Delete Connector Step (#211)
* Add Delete Connector Step Signed-off-by: Daniel Widdis <[email protected]> * Add eclipse core runtime version resolution Signed-off-by: Daniel Widdis <[email protected]> * Use JDK17 for spotless Signed-off-by: Daniel Widdis <[email protected]> * Add Delete Connector Step Signed-off-by: Daniel Widdis <[email protected]> * Add eclipse core runtime version resolution Signed-off-by: Daniel Widdis <[email protected]> * Use JDK17 for spotless Signed-off-by: Daniel Widdis <[email protected]> * Fetch connector ID from appropriate previous node output Signed-off-by: Daniel Widdis <[email protected]> * Fix tests Signed-off-by: Daniel Widdis <[email protected]> * Test that actual ID is properly passed Signed-off-by: Daniel Widdis <[email protected]> * Update to current setup-java version Signed-off-by: Daniel Widdis <[email protected]> * Remove unneeded argument captors Signed-off-by: Daniel Widdis <[email protected]> --------- Signed-off-by: Daniel Widdis <[email protected]>
- Loading branch information
Showing
7 changed files
with
241 additions
and
26 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
105 changes: 105 additions & 0 deletions
105
src/main/java/org/opensearch/flowframework/workflow/DeleteConnectorStep.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,105 @@ | ||
/* | ||
* 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.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.ExceptionsHelper; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.ml.client.MachineLearningNodeClient; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static org.opensearch.flowframework.common.CommonValue.CONNECTOR_ID; | ||
|
||
/** | ||
* Step to delete a connector for a remote model | ||
*/ | ||
public class DeleteConnectorStep implements WorkflowStep { | ||
|
||
private static final Logger logger = LogManager.getLogger(DeleteConnectorStep.class); | ||
|
||
private MachineLearningNodeClient mlClient; | ||
|
||
static final String NAME = "delete_connector"; | ||
|
||
/** | ||
* Instantiate this class | ||
* @param mlClient Machine Learning client to perform the deletion | ||
*/ | ||
public DeleteConnectorStep(MachineLearningNodeClient mlClient) { | ||
this.mlClient = mlClient; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<WorkflowData> execute( | ||
String currentNodeId, | ||
WorkflowData currentNodeInputs, | ||
Map<String, WorkflowData> outputs, | ||
Map<String, String> previousNodeInputs | ||
) throws IOException { | ||
CompletableFuture<WorkflowData> deleteConnectorFuture = new CompletableFuture<>(); | ||
|
||
ActionListener<DeleteResponse> actionListener = new ActionListener<>() { | ||
|
||
@Override | ||
public void onResponse(DeleteResponse deleteResponse) { | ||
deleteConnectorFuture.complete( | ||
new WorkflowData( | ||
Map.ofEntries(Map.entry("connector_id", deleteResponse.getId())), | ||
currentNodeInputs.getWorkflowId(), | ||
currentNodeInputs.getNodeId() | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
logger.error("Failed to delete connector"); | ||
deleteConnectorFuture.completeExceptionally(new FlowFrameworkException(e.getMessage(), ExceptionsHelper.status(e))); | ||
} | ||
}; | ||
|
||
String connectorId = null; | ||
|
||
// Previous Node inputs defines which step the connector ID came from | ||
Optional<String> previousNode = previousNodeInputs.entrySet() | ||
.stream() | ||
.filter(e -> CONNECTOR_ID.equals(e.getValue())) | ||
.map(Map.Entry::getKey) | ||
.findFirst(); | ||
if (previousNode.isPresent()) { | ||
WorkflowData previousNodeOutput = outputs.get(previousNode.get()); | ||
if (previousNodeOutput != null && previousNodeOutput.getContent().containsKey(CONNECTOR_ID)) { | ||
connectorId = previousNodeOutput.getContent().get(CONNECTOR_ID).toString(); | ||
} | ||
} | ||
|
||
if (connectorId != null) { | ||
mlClient.deleteConnector(connectorId, actionListener); | ||
} else { | ||
deleteConnectorFuture.completeExceptionally( | ||
new FlowFrameworkException("Required field " + CONNECTOR_ID + " is not provided", RestStatus.BAD_REQUEST) | ||
); | ||
} | ||
|
||
return deleteConnectorFuture; | ||
} | ||
|
||
@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
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
115 changes: 115 additions & 0 deletions
115
src/test/java/org/opensearch/flowframework/workflow/DeleteConnectorStepTests.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,115 @@ | ||
/* | ||
* 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.opensearch.action.delete.DeleteResponse; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.index.Index; | ||
import org.opensearch.core.index.shard.ShardId; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.flowframework.common.CommonValue; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.ml.client.MachineLearningNodeClient; | ||
import org.opensearch.ml.common.transport.connector.MLCreateConnectorResponse; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class DeleteConnectorStepTests extends OpenSearchTestCase { | ||
private WorkflowData inputData; | ||
|
||
@Mock | ||
MachineLearningNodeClient machineLearningNodeClient; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
MockitoAnnotations.openMocks(this); | ||
|
||
inputData = new WorkflowData(Map.of(CommonValue.CONNECTOR_ID, "test"), "test-id", "test-node-id"); | ||
} | ||
|
||
public void testDeleteConnector() throws IOException, ExecutionException, InterruptedException { | ||
|
||
String connectorId = randomAlphaOfLength(5); | ||
DeleteConnectorStep deleteConnectorStep = new DeleteConnectorStep(machineLearningNodeClient); | ||
|
||
doAnswer(invocation -> { | ||
String connectorIdArg = invocation.getArgument(0); | ||
ActionListener<DeleteResponse> actionListener = invocation.getArgument(1); | ||
ShardId shardId = new ShardId(new Index("indexName", "uuid"), 1); | ||
DeleteResponse output = new DeleteResponse(shardId, connectorIdArg, 1, 1, 1, true); | ||
actionListener.onResponse(output); | ||
return null; | ||
}).when(machineLearningNodeClient).deleteConnector(any(String.class), any()); | ||
|
||
CompletableFuture<WorkflowData> future = deleteConnectorStep.execute( | ||
inputData.getNodeId(), | ||
inputData, | ||
Map.of("step_1", new WorkflowData(Map.of("connector_id", connectorId), "workflowId", "nodeId")), | ||
Map.of("step_1", "connector_id") | ||
); | ||
verify(machineLearningNodeClient).deleteConnector(any(String.class), any()); | ||
|
||
assertTrue(future.isDone()); | ||
assertEquals(connectorId, future.get().getContent().get("connector_id")); | ||
} | ||
|
||
public void testNoConnectorIdInOutput() throws IOException { | ||
DeleteConnectorStep deleteConnectorStep = new DeleteConnectorStep(machineLearningNodeClient); | ||
|
||
CompletableFuture<WorkflowData> future = deleteConnectorStep.execute( | ||
inputData.getNodeId(), | ||
inputData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
|
||
assertTrue(future.isCompletedExceptionally()); | ||
ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get().getContent()); | ||
assertTrue(ex.getCause() instanceof FlowFrameworkException); | ||
assertEquals("Required field connector_id is not provided", ex.getCause().getMessage()); | ||
} | ||
|
||
public void testDeleteConnectorFailure() throws IOException { | ||
DeleteConnectorStep deleteConnectorStep = new DeleteConnectorStep(machineLearningNodeClient); | ||
|
||
doAnswer(invocation -> { | ||
ActionListener<MLCreateConnectorResponse> actionListener = invocation.getArgument(1); | ||
actionListener.onFailure(new FlowFrameworkException("Failed to delete connector", RestStatus.INTERNAL_SERVER_ERROR)); | ||
return null; | ||
}).when(machineLearningNodeClient).deleteConnector(any(String.class), any()); | ||
|
||
CompletableFuture<WorkflowData> future = deleteConnectorStep.execute( | ||
inputData.getNodeId(), | ||
inputData, | ||
Map.of("step_1", new WorkflowData(Map.of("connector_id", "test"), "workflowId", "nodeId")), | ||
Map.of("step_1", "connector_id") | ||
); | ||
|
||
verify(machineLearningNodeClient).deleteConnector(any(String.class), any()); | ||
|
||
assertTrue(future.isCompletedExceptionally()); | ||
ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get().getContent()); | ||
assertTrue(ex.getCause() instanceof FlowFrameworkException); | ||
assertEquals("Failed to delete connector", ex.getCause().getMessage()); | ||
} | ||
} |