Skip to content

Commit

Permalink
Addressed PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Owais Kazi <[email protected]>
  • Loading branch information
owaiskazi19 committed Sep 19, 2023
1 parent f759bea commit 4b6e3e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ opensearchplugin {
dependencyLicenses.enabled = false
// This requires an additional Jar not published as part of build-tools
loggerUsageCheck.enabled = false
thirdPartyAudit.enabled = false

// No need to validate pom, as we do not upload to maven/sonatype
validateNebulaPom.enabled = false
Expand Down Expand Up @@ -106,7 +107,7 @@ dependencies {
implementation "org.opensearch:opensearch:${opensearch_version}"
implementation 'org.junit.jupiter:junit-jupiter:5.10.0'
implementation "com.google.code.gson:gson:2.10.1"
compileOnly "com.google.guava:guava:32.1.2-jre"
implementation "com.google.guava:guava:32.1.2-jre"
api group: 'org.opensearch', name:'opensearch-ml-client', version: "${opensearch_build}"

configurations.all {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import com.google.common.io.Resources;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.ActionListener;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.admin.indices.create.CreateIndexResponse;
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.action.ActionListener;
import org.opensearch.flowframework.workflow.WorkflowData;
import org.opensearch.flowframework.workflow.WorkflowStep;

Expand All @@ -26,12 +27,19 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
* Step to create an index
*/
public class CreateIndexStep implements WorkflowStep {

private static final Logger logger = LogManager.getLogger(CreateIndexStep.class);

Check warning on line 35 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L35

Added line #L35 was not covered by tests
private Client client;
private final String CREATE_INDEX_STEP = "create_index_step";
private final String NAME = "create_index_step";

Check warning on line 37 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L37

Added line #L37 was not covered by tests

/**
* Instantiate this class
* @param client Client to create an index
*/
public CreateIndexStep(Client client) {
this.client = client;
}

Check warning on line 45 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L43-L45

Added lines #L43 - L45 were not covered by tests
Expand All @@ -43,7 +51,7 @@ public CompletableFuture<WorkflowData> execute(List<WorkflowData> data) {

@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
logger.info("created index:{}");
logger.info("created index:{}", createIndexResponse.index());
future.complete(new WorkflowData() {

Check warning on line 55 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L54-L55

Added lines #L54 - L55 were not covered by tests
@Override
public Map<String, Object> getContent() {
Expand All @@ -54,41 +62,54 @@ public Map<String, Object> getContent() {

@Override
public void onFailure(Exception e) {
logger.error("Index creation failed", e);
logger.error("Failed to create an index", e);
future.completeExceptionally(e);
}

Check warning on line 67 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L65-L67

Added lines #L65 - L67 were not covered by tests
};

String index = null;
String type = null;
Settings settings = null;

Check warning on line 72 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L70-L72

Added lines #L70 - L72 were not covered by tests

for (WorkflowData workflowData : data) {
// Fetch index from content i.e. request body of execute API
Map<String, Object> content = workflowData.getContent();
index = (String) content.get("index");
type = (String) content.get("type");
settings = (Settings) content.get("settings");

Check warning on line 79 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L76-L79

Added lines #L76 - L79 were not covered by tests
if (index != null && type != null) {
break;

Check warning on line 81 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L81

Added line #L81 was not covered by tests
}
}

Check warning on line 83 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L83

Added line #L83 was not covered by tests

// TODO:
// 1. Map index type -> fileName
// 2. Create settings based on the index settings received from content
CreateIndexRequest request = new CreateIndexRequest(index).mapping(getIndexMappings(fileName), XContentType.JSON)
.settings(settings);
client.admin().indices().create(request, actionListener);
// 1. Create settings based on the index settings received from content

try {
CreateIndexRequest request = new CreateIndexRequest(index).mapping(getIndexMappings(type), XContentType.JSON)
.settings(settings);
client.admin().indices().create(request, actionListener);
} catch (Exception e) {
logger.error("Failed to find the right mapping for the index", e);
}

Check warning on line 94 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L89-L94

Added lines #L89 - L94 were not covered by tests

return future;

Check warning on line 96 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L96

Added line #L96 was not covered by tests
}

@Override
public String getName() {
return CREATE_INDEX_STEP;
return NAME;

Check warning on line 101 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L101

Added line #L101 was not covered by tests
}

/**
* Get index mapping json content.
*
* @param mapping type of the index to fetch the specific mapping file
* @return index mapping
* @throws IOException IOException if mapping file can't be read correctly
*/
public static String getIndexMappings(String mappingFileName) throws IOException {
URL url = CreateIndexStep.class.getClassLoader().getResource(mappingFileName);
public static String getIndexMappings(String mapping) throws IOException {
URL url = CreateIndexStep.class.getClassLoader().getResource(mapping);
return Resources.toString(url, Charsets.UTF_8);

Check warning on line 113 in src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java#L112-L113

Added lines #L112 - L113 were not covered by tests
}
}
File renamed without changes.

0 comments on commit 4b6e3e3

Please sign in to comment.