-
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: Owais Kazi <[email protected]>
- Loading branch information
1 parent
a182b2d
commit e3efb04
Showing
7 changed files
with
244 additions
and
15 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/DeleteIndexStep.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.admin.indices.delete.DeleteIndexRequest; | ||
import org.opensearch.action.support.PlainActionFuture; | ||
import org.opensearch.action.support.master.AcknowledgedResponse; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.flowframework.util.ParseUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.flowframework.common.WorkflowResources.INDEX_NAME; | ||
|
||
/** | ||
* Step to delete an index | ||
*/ | ||
public class DeleteIndexStep implements WorkflowStep { | ||
private static final Logger logger = LogManager.getLogger(DeleteConnectorStep.class); | ||
|
||
private final Client client; | ||
|
||
/** The name of this step, used as a key in the template and the {@link WorkflowStepFactory} */ | ||
public static final String NAME = "delete_index"; | ||
|
||
/** | ||
* Instantiate this class | ||
* | ||
* @param client Client to create an index | ||
*/ | ||
public DeleteIndexStep(Client client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public PlainActionFuture<WorkflowData> execute( | ||
String currentNodeId, | ||
WorkflowData currentNodeInputs, | ||
Map<String, WorkflowData> outputs, | ||
Map<String, String> previousNodeInputs, | ||
Map<String, String> params | ||
) { | ||
|
||
PlainActionFuture<WorkflowData> deleteIndexFuture = PlainActionFuture.newFuture(); | ||
|
||
Set<String> requiredKeys = Set.of(INDEX_NAME); | ||
Set<String> optionalKeys = Collections.emptySet(); | ||
|
||
try { | ||
Map<String, Object> inputs = ParseUtils.getInputsFromPreviousSteps( | ||
requiredKeys, | ||
optionalKeys, | ||
currentNodeInputs, | ||
outputs, | ||
previousNodeInputs, | ||
params | ||
); | ||
String indexName = (String) inputs.get(INDEX_NAME); | ||
|
||
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName); | ||
client.admin().indices().delete(deleteIndexRequest, new ActionListener<AcknowledgedResponse>() { | ||
@Override | ||
public void onResponse(AcknowledgedResponse response) { | ||
logger.info("Deleted index: {}", indexName); | ||
deleteIndexFuture.onResponse( | ||
new WorkflowData( | ||
Map.ofEntries(Map.entry(INDEX_NAME, indexName)), | ||
currentNodeInputs.getWorkflowId(), | ||
currentNodeInputs.getNodeId() | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
String errorMessage = "Failed to delete the index " + indexName; | ||
logger.error(errorMessage, e); | ||
deleteIndexFuture.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(e))); | ||
} | ||
}); | ||
} catch (FlowFrameworkException e) { | ||
deleteIndexFuture.onFailure(e); | ||
} | ||
|
||
return deleteIndexFuture; | ||
} | ||
|
||
@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
118 changes: 118 additions & 0 deletions
118
src/test/java/org/opensearch/flowframework/workflow/DeleteIndexStepTests.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,118 @@ | ||
/* | ||
* 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.admin.indices.delete.DeleteIndexRequest; | ||
import org.opensearch.action.support.PlainActionFuture; | ||
import org.opensearch.action.support.master.AcknowledgedResponse; | ||
import org.opensearch.client.AdminClient; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.client.IndicesAdminClient; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX; | ||
import static org.opensearch.flowframework.common.WorkflowResources.INDEX_NAME; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class DeleteIndexStepTests extends OpenSearchTestCase { | ||
|
||
private WorkflowData inputData = WorkflowData.EMPTY; | ||
private Client client; | ||
private AdminClient adminClient; | ||
private DeleteIndexStep deleteIndexStep; | ||
private ThreadContext threadContext; | ||
private Metadata metadata; | ||
|
||
@Mock | ||
private IndicesAdminClient indicesAdminClient; | ||
@Mock | ||
private ThreadPool threadPool; | ||
@Mock | ||
IndexMetadata indexMetadata; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
MockitoAnnotations.openMocks(this); | ||
inputData = new WorkflowData(Map.ofEntries(Map.entry(INDEX_NAME, "demo")), "test-id", "test-node-id"); | ||
client = mock(Client.class); | ||
adminClient = mock(AdminClient.class); | ||
metadata = mock(Metadata.class); | ||
Settings settings = Settings.builder().build(); | ||
threadContext = new ThreadContext(settings); | ||
|
||
when(client.threadPool()).thenReturn(threadPool); | ||
when(threadPool.getThreadContext()).thenReturn(threadContext); | ||
when(client.admin()).thenReturn(adminClient); | ||
when(adminClient.indices()).thenReturn(indicesAdminClient); | ||
when(metadata.indices()).thenReturn(Map.of(GLOBAL_CONTEXT_INDEX, indexMetadata)); | ||
|
||
deleteIndexStep = new DeleteIndexStep(client); | ||
} | ||
|
||
public void testDeleteIndex() throws IOException, ExecutionException, InterruptedException { | ||
|
||
@SuppressWarnings({ "unchecked" }) | ||
ArgumentCaptor<ActionListener<AcknowledgedResponse>> actionListenerCaptor = ArgumentCaptor.forClass(ActionListener.class); | ||
PlainActionFuture<WorkflowData> future = deleteIndexStep.execute( | ||
inputData.getNodeId(), | ||
inputData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
assertFalse(future.isDone()); | ||
verify(indicesAdminClient, times(1)).delete(any(DeleteIndexRequest.class), actionListenerCaptor.capture()); | ||
actionListenerCaptor.getValue().onResponse(new AcknowledgedResponse(true)); | ||
|
||
assertTrue(future.isDone()); | ||
|
||
Map<String, Object> outputData = Map.of(INDEX_NAME, "demo"); | ||
assertEquals(outputData, future.get().getContent()); | ||
} | ||
|
||
public void testDeleteIndexStepFailure() throws ExecutionException, InterruptedException { | ||
@SuppressWarnings({ "unchecked" }) | ||
ArgumentCaptor<ActionListener<AcknowledgedResponse>> actionListenerCaptor = ArgumentCaptor.forClass(ActionListener.class); | ||
PlainActionFuture<WorkflowData> future = deleteIndexStep.execute( | ||
inputData.getNodeId(), | ||
inputData, | ||
Collections.emptyMap(), | ||
Collections.emptyMap(), | ||
Collections.emptyMap() | ||
); | ||
assertFalse(future.isDone()); | ||
verify(indicesAdminClient, times(1)).delete(any(DeleteIndexRequest.class), actionListenerCaptor.capture()); | ||
actionListenerCaptor.getValue().onFailure(new Exception("Failed to delete the index")); | ||
|
||
assertTrue(future.isDone()); | ||
ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get().getContent()); | ||
assertTrue(ex.getCause() instanceof Exception); | ||
assertEquals("Failed to delete the index demo", ex.getCause().getMessage()); | ||
} | ||
} |