Skip to content

Commit

Permalink
changed resourcescreated to resourceCreated and other minor comment c…
Browse files Browse the repository at this point in the history
…hanges

Signed-off-by: Amit Galitzky <[email protected]>
  • Loading branch information
amitgalitz committed Nov 15, 2023
1 parent 2fa6fd1 commit a6b4dee
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ private CommonValue() {}
public static final String USER_OUTPUTS_FIELD = "user_outputs";
/** The template field name for template resources created */
public static final String RESOURCES_CREATED_FIELD = "resources_created";
/** The field name for the ResourcesCreated's resource ID */
/** The field name for the ResourceCreated's resource ID */
public static final String RESOURCE_ID_FIELD = "resource_id";
/** The field name for the ResourcesCreated's resource name */
/** The field name for the ResourceCreated's resource name */
public static final String WORKFLOW_STEP_NAME = "workflow_step_name";

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
/**
* This represents an object in the WorkflowState {@link WorkflowState}.
*/
public class ResourcesCreated implements ToXContentObject, Writeable {
// TODO: create an enum to add the resource name itself for each step example (create_connector_step -> connector)
public class ResourceCreated implements ToXContentObject, Writeable {

private String workflowStepName;
private String resourceId;
private final String workflowStepName;
private final String resourceId;

/**
* Create this resources created object with given resource name and ID.
* @param workflowStepName The workflow step name associating to the step where it was created
* @param resourceId The resources ID for relating to the created resource
*/
public ResourcesCreated(String workflowStepName, String resourceId) {
public ResourceCreated(String workflowStepName, String resourceId) {
this.workflowStepName = workflowStepName;
this.resourceId = resourceId;
}
Expand All @@ -44,7 +45,7 @@ public ResourcesCreated(String workflowStepName, String resourceId) {
* @param input the input stream to read from
* @throws IOException if failed to read input stream
*/
public ResourcesCreated(StreamInput input) throws IOException {
public ResourceCreated(StreamInput input) throws IOException {
this.workflowStepName = input.readString();
this.resourceId = input.readString();
}
Expand Down Expand Up @@ -82,13 +83,13 @@ public String workflowStepName() {
}

/**
* Parse raw JSON content into a resourcesCreated instance.
* Parse raw JSON content into a ResourceCreated instance.
*
* @param parser JSON based content parser
* @return the parsed ResourcesCreated instance
* @return the parsed ResourceCreated instance
* @throws IOException if content can't be parsed correctly
*/
public static ResourcesCreated parse(XContentParser parser) throws IOException {
public static ResourceCreated parse(XContentParser parser) throws IOException {
String workflowStepName = null;
String resourceId = null;

Expand All @@ -109,9 +110,9 @@ public static ResourcesCreated parse(XContentParser parser) throws IOException {
}
}
if (workflowStepName == null || resourceId == null) {
throw new IOException("A resourcesCreated object requires both a workflowStepName and resourceId.");
throw new IOException("A ResourceCreated object requires both a workflowStepName and resourceId.");
}
return new ResourcesCreated(workflowStepName, resourceId);
return new ResourceCreated(workflowStepName, resourceId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class WorkflowState implements ToXContentObject, Writeable {
private Instant provisionEndTime;
private User user;
private Map<String, Object> userOutputs;
private List<ResourcesCreated> resourcesCreated;
private List<ResourceCreated> resourcesCreated;

/**
* Instantiate the object representing the workflow state
Expand All @@ -78,7 +78,7 @@ public WorkflowState(
Instant provisionEndTime,
User user,
Map<String, Object> userOutputs,
List<ResourcesCreated> resourcesCreated
List<ResourceCreated> resourcesCreated
) {
this.workflowId = workflowId;
this.error = error;
Expand Down Expand Up @@ -108,7 +108,7 @@ public WorkflowState(StreamInput input) throws IOException {
// TODO: fix error: cannot access Response issue when integrating with access control
// this.user = input.readBoolean() ? new User(input) : null;
this.userOutputs = input.readBoolean() ? input.readMap() : null;
this.resourcesCreated = input.readList(ResourcesCreated::new);
this.resourcesCreated = input.readList(ResourceCreated::new);
}

/**
Expand All @@ -131,7 +131,7 @@ public static class Builder {
private Instant provisionEndTime = null;
private User user = null;
private Map<String, Object> userOutputs = null;
private List<ResourcesCreated> resourcesCreated = null;
private List<ResourceCreated> resourcesCreated = null;

/**
* Empty Constructor for the Builder object
Expand Down Expand Up @@ -223,7 +223,7 @@ public Builder userOutputs(Map<String, Object> userOutputs) {
* @param resourcesCreated resourcesCreated
* @return the Builder object
*/
public Builder resourcesCreated(List<ResourcesCreated> resourcesCreated) {
public Builder resourcesCreated(List<ResourceCreated> resourcesCreated) {
this.resourcesCreated = resourcesCreated;
return this;
}
Expand Down Expand Up @@ -320,7 +320,7 @@ public static WorkflowState parse(XContentParser parser) throws IOException {
Instant provisionEndTime = null;
User user = null;
Map<String, Object> userOutputs = new HashMap<>();
List<ResourcesCreated> resourcesCreated = new ArrayList<>();
List<ResourceCreated> resourcesCreated = new ArrayList<>();

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -368,7 +368,7 @@ public static WorkflowState parse(XContentParser parser) throws IOException {
try {
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
resourcesCreated.add(ResourcesCreated.parse(parser));
resourcesCreated.add(ResourceCreated.parse(parser));
}
} catch (Exception e) {
if (e instanceof ParsingException || e instanceof XContentParseException) {
Expand Down Expand Up @@ -461,7 +461,7 @@ public Map<String, Object> userOutputs() {
* A map of all the resources created
* @return the resources created
*/
public List<ResourcesCreated> resourcesCreated() {
public List<ResourceCreated> resourcesCreated() {
return resourcesCreated;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.opensearch.transport.TransportService;

import java.util.List;
import java.util.UUID;

import static org.opensearch.flowframework.common.CommonValue.PROVISIONING_PROGRESS_FIELD;
import static org.opensearch.flowframework.common.CommonValue.STATE_FIELD;
Expand Down Expand Up @@ -98,8 +97,6 @@ protected void doExecute(Task task, WorkflowRequest request, ActionListener<Work

if (request.isDryRun()) {
try {
// generating random workflowId only for validation purpose
String uniqueID = UUID.randomUUID().toString();
validateWorkflows(templateWithUser);
} catch (Exception e) {
if (e instanceof FlowFrameworkException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.flowframework.exception.FlowFrameworkException;
import org.opensearch.flowframework.indices.FlowFrameworkIndicesHandler;
import org.opensearch.flowframework.model.ResourcesCreated;
import org.opensearch.flowframework.model.ResourceCreated;
import org.opensearch.ml.client.MachineLearningNodeClient;
import org.opensearch.ml.common.connector.ConnectorAction;
import org.opensearch.ml.common.connector.ConnectorAction.ActionType;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void onResponse(MLCreateConnectorResponse mlCreateConnectorResponse) {
logger.info("Created connector successfully");
String workflowId = data.get(0).getWorkflowId();
String workflowStepName = getName();
ResourcesCreated newResource = new ResourcesCreated(workflowStepName, mlCreateConnectorResponse.getConnectorId());
ResourceCreated newResource = new ResourceCreated(workflowStepName, mlCreateConnectorResponse.getConnectorId());
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
newResource.toXContent(builder, ToXContentObject.EMPTY_PARAMS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@

import java.io.IOException;

public class ResourcesCreatedTests extends OpenSearchTestCase {
public class ResourceCreatedTests extends OpenSearchTestCase {

@Override
public void setUp() throws Exception {
super.setUp();
}

public void testParseFeature() throws IOException {
ResourcesCreated resourcesCreated = new ResourcesCreated("A", "B");
assertEquals(resourcesCreated.workflowStepName(), "A");
assertEquals(resourcesCreated.resourceId(), "B");
ResourceCreated ResourceCreated = new ResourceCreated("A", "B");
assertEquals(ResourceCreated.workflowStepName(), "A");
assertEquals(ResourceCreated.resourceId(), "B");

String expectedJson = "{\"workflow_step_name\":\"A\",\"resource_id\":\"B\"}";
String json = TemplateTestJsonUtil.parseToJson(resourcesCreated);
String json = TemplateTestJsonUtil.parseToJson(ResourceCreated);
assertEquals(expectedJson, json);

ResourcesCreated resourcesCreatedTwo = ResourcesCreated.parse(TemplateTestJsonUtil.jsonToParser(json));
assertEquals("A", resourcesCreatedTwo.workflowStepName());
assertEquals("B", resourcesCreatedTwo.resourceId());
ResourceCreated ResourceCreatedTwo = ResourceCreated.parse(TemplateTestJsonUtil.jsonToParser(json));
assertEquals("A", ResourceCreatedTwo.workflowStepName());
assertEquals("B", ResourceCreatedTwo.resourceId());
}

public void testExceptions() throws IOException {
String badJson = "{\"wrong\":\"A\",\"resource_id\":\"B\"}";
IOException e = assertThrows(IOException.class, () -> ResourcesCreated.parse(TemplateTestJsonUtil.jsonToParser(badJson)));
IOException e = assertThrows(IOException.class, () -> ResourceCreated.parse(TemplateTestJsonUtil.jsonToParser(badJson)));
assertEquals("Unable to parse field [wrong] in a resources_created object.", e.getMessage());

String missingJson = "{\"resource_id\":\"B\"}";
e = assertThrows(IOException.class, () -> ResourcesCreated.parse(TemplateTestJsonUtil.jsonToParser(missingJson)));
assertEquals("A resourcesCreated object requires both a workflowStepName and resourceId.", e.getMessage());
e = assertThrows(IOException.class, () -> ResourceCreated.parse(TemplateTestJsonUtil.jsonToParser(missingJson)));
assertEquals("A ResourceCreated object requires both a workflowStepName and resourceId.", e.getMessage());
}

}

0 comments on commit a6b4dee

Please sign in to comment.