Skip to content

Commit

Permalink
Merge branch 'main' into whitesource-remediate/actions-github-script-7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
owaiskazi19 authored Nov 13, 2023
2 parents 56c9af1 + cdb8c68 commit 0fb1cea
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 44 deletions.
167 changes: 159 additions & 8 deletions src/main/java/org/opensearch/flowframework/model/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.opensearch.flowframework.common.CommonValue.DESCRIPTION_FIELD;
import static org.opensearch.flowframework.common.CommonValue.NAME_FIELD;
import static org.opensearch.flowframework.common.CommonValue.TEMPLATE_FIELD;
import static org.opensearch.flowframework.common.CommonValue.UI_METADATA_FIELD;
import static org.opensearch.flowframework.common.CommonValue.USER_FIELD;
import static org.opensearch.flowframework.common.CommonValue.USE_CASE_FIELD;
import static org.opensearch.flowframework.common.CommonValue.VERSION_FIELD;
Expand All @@ -40,13 +41,14 @@
*/
public class Template implements ToXContentObject {

private final String name;
private final String description;
private final String useCase; // probably an ENUM actually
private final Version templateVersion;
private final List<Version> compatibilityVersion;
private final Map<String, Workflow> workflows;
private final User user;
private String name;
private String description;
private String useCase; // probably an ENUM actually
private Version templateVersion;
private List<Version> compatibilityVersion;
private Map<String, Workflow> workflows;
private Map<String, Object> uiMetadata;
private User user;

/**
* Instantiate the object representing a use case template
Expand All @@ -57,6 +59,7 @@ public class Template implements ToXContentObject {
* @param templateVersion The version of this template
* @param compatibilityVersion OpenSearch version compatibility of this template
* @param workflows Workflow graph definitions corresponding to the defined operations.
* @param uiMetadata The UI metadata related to the given workflow
* @param user The user extracted from the thread context from the request
*/
public Template(
Expand All @@ -66,6 +69,7 @@ public Template(
Version templateVersion,
List<Version> compatibilityVersion,
Map<String, Workflow> workflows,
Map<String, Object> uiMetadata,
User user
) {
this.name = name;
Expand All @@ -74,9 +78,129 @@ public Template(
this.templateVersion = templateVersion;
this.compatibilityVersion = List.copyOf(compatibilityVersion);
this.workflows = Map.copyOf(workflows);
this.uiMetadata = uiMetadata;
this.user = user;
}

private Template() {}

/**
* Class for constructing a Builder for Template
*/
public static class Builder {
private String name = null;
private String description = "";
private String useCase = "";
private Version templateVersion = null;
private List<Version> compatibilityVersion = new ArrayList<>();
private Map<String, Workflow> workflows = new HashMap<>();
private Map<String, Object> uiMetadata = null;
private User user = null;

/**
* Empty Constructor for the Builder object
*/
public Builder() {}

/**
* Builder method for adding template name
* @param name template name
* @return the Builder object
*/
public Builder name(String name) {
this.name = name;
return this;
}

/**
* Builder method for adding template description
* @param description template description
* @return the Builder object
*/
public Builder description(String description) {
this.description = description;
return this;
}

/**
* Builder method for adding template useCase
* @param useCase template useCase
* @return the Builder object
*/
public Builder useCase(String useCase) {
this.useCase = useCase;
return this;
}

/**
* Builder method for adding templateVersion
* @param templateVersion templateVersion
* @return the Builder object
*/
public Builder templateVersion(Version templateVersion) {
this.templateVersion = templateVersion;
return this;
}

/**
* Builder method for adding compatibilityVersion
* @param compatibilityVersion compatibilityVersion
* @return the Builder object
*/
public Builder compatibilityVersion(List<Version> compatibilityVersion) {
this.compatibilityVersion = compatibilityVersion;
return this;
}

/**
* Builder method for adding workflows
* @param workflows workflows
* @return the Builder object
*/
public Builder workflows(Map<String, Workflow> workflows) {
this.workflows = workflows;
return this;
}

/**
* Builder method for adding uiMetadata
* @param uiMetadata uiMetadata
* @return the Builder object
*/
public Builder uiMetadata(Map<String, Object> uiMetadata) {
this.uiMetadata = uiMetadata;
return this;
}

/**
* Builder method for adding user
* @param user user
* @return the Builder object
*/
public Builder user(User user) {
this.user = user;
return this;
}

/**
* Allows building a template
* @return Template Object containing all needed fields
*/
public Template build() {
Template template = new Template();
template.name = this.name;
template.description = this.description;
template.useCase = this.useCase;
template.templateVersion = this.templateVersion;
template.compatibilityVersion = this.compatibilityVersion;
template.workflows = this.workflows;
template.uiMetadata = this.uiMetadata;
template.user = this.user;
return template;
}

}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
XContentBuilder xContentBuilder = builder.startObject();
Expand All @@ -103,6 +227,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
for (Entry<String, Workflow> e : workflows.entrySet()) {
xContentBuilder.field(e.getKey(), e.getValue(), params);
}

if (uiMetadata != null && !uiMetadata.isEmpty()) {
xContentBuilder.field(UI_METADATA_FIELD, uiMetadata);
}

xContentBuilder.endObject();
if (user != null) {
xContentBuilder.field(USER_FIELD, user);
Expand All @@ -125,6 +254,7 @@ public static Template parse(XContentParser parser) throws IOException {
Version templateVersion = null;
List<Version> compatibilityVersion = new ArrayList<>();
Map<String, Workflow> workflows = new HashMap<>();
Map<String, Object> uiMetadata = null;
User user = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
Expand Down Expand Up @@ -169,6 +299,9 @@ public static Template parse(XContentParser parser) throws IOException {
workflows.put(workflowFieldName, Workflow.parse(parser));
}
break;
case UI_METADATA_FIELD:
uiMetadata = parser.map();
break;
case USER_FIELD:
user = User.parse(parser);
break;
Expand All @@ -180,7 +313,15 @@ public static Template parse(XContentParser parser) throws IOException {
throw new IOException("A template object requires a name.");
}

return new Template(name, description, useCase, templateVersion, compatibilityVersion, workflows, user);
return new Builder().name(name)
.description(description)
.useCase(useCase)
.templateVersion(templateVersion)
.compatibilityVersion(compatibilityVersion)
.workflows(workflows)
.uiMetadata(uiMetadata)
.user(user)
.build();
}

/**
Expand Down Expand Up @@ -276,6 +417,14 @@ public Map<String, Workflow> workflows() {
return workflows;
}

/**
* A map corresponding to the UI metadata
* @return the userOutputs
*/
public Map<String, Object> getUiMetadata() {
return uiMetadata;
}

/**
* User that created and owns this template
* @return the user
Expand All @@ -298,6 +447,8 @@ public String toString() {
+ compatibilityVersion
+ ", workflows="
+ workflows
+ ", uiMedata="
+ uiMetadata
+ "]";
}
}
50 changes: 17 additions & 33 deletions src/main/java/org/opensearch/flowframework/model/WorkflowState.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static org.opensearch.flowframework.common.CommonValue.PROVISION_START_TIME_FIELD;
import static org.opensearch.flowframework.common.CommonValue.RESOURCES_CREATED_FIELD;
import static org.opensearch.flowframework.common.CommonValue.STATE_FIELD;
import static org.opensearch.flowframework.common.CommonValue.UI_METADATA_FIELD;
import static org.opensearch.flowframework.common.CommonValue.USER_FIELD;
import static org.opensearch.flowframework.common.CommonValue.USER_OUTPUTS_FIELD;
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_ID_FIELD;
Expand All @@ -45,7 +44,6 @@ public class WorkflowState implements ToXContentObject {
private Instant provisionStartTime;
private Instant provisionEndTime;
private User user;
private Map<String, Object> uiMetadata;
private Map<String, Object> userOutputs;
private Map<String, Object> resourcesCreated;

Expand All @@ -59,7 +57,6 @@ public class WorkflowState implements ToXContentObject {
* @param provisionStartTime Indicates the start time of the whole provisioning flow
* @param provisionEndTime Indicates the end time of the whole provisioning flow
* @param user The user extracted from the thread context from the request
* @param uiMetadata The UI metadata related to the given workflow
* @param userOutputs A map of essential API responses for backend to use and lookup.
* @param resourcesCreated A map of all the resources created.
*/
Expand All @@ -71,7 +68,6 @@ public WorkflowState(
Instant provisionStartTime,
Instant provisionEndTime,
User user,
Map<String, Object> uiMetadata,
Map<String, Object> userOutputs,
Map<String, Object> resourcesCreated
) {
Expand All @@ -82,7 +78,6 @@ public WorkflowState(
this.provisionStartTime = provisionStartTime;
this.provisionEndTime = provisionEndTime;
this.user = user;
this.uiMetadata = uiMetadata;
this.userOutputs = Map.copyOf(userOutputs);
this.resourcesCreated = Map.copyOf(resourcesCreated);
}
Expand All @@ -108,7 +103,6 @@ public static class Builder {
private Instant provisionStartTime = null;
private Instant provisionEndTime = null;
private User user = null;
private Map<String, Object> uiMetadata = null;
private Map<String, Object> userOutputs = null;
private Map<String, Object> resourcesCreated = null;

Expand Down Expand Up @@ -187,16 +181,6 @@ public Builder user(User user) {
return this;
}

/**
* Builder method for adding uiMetadata
* @param uiMetadata uiMetadata
* @return the Builder object
*/
public Builder uiMetadata(Map<String, Object> uiMetadata) {
this.uiMetadata = uiMetadata;
return this;
}

/**
* Builder method for adding userOutputs
* @param userOutputs userOutputs
Expand Down Expand Up @@ -230,7 +214,6 @@ public WorkflowState build() {
workflowState.provisionStartTime = this.provisionStartTime;
workflowState.provisionEndTime = this.provisionEndTime;
workflowState.user = this.user;
workflowState.uiMetadata = this.uiMetadata;
workflowState.userOutputs = this.userOutputs;
workflowState.resourcesCreated = this.resourcesCreated;
return workflowState;
Expand Down Expand Up @@ -261,9 +244,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (user != null) {
xContentBuilder.field(USER_FIELD, user);
}
if (uiMetadata != null && !uiMetadata.isEmpty()) {
xContentBuilder.field(UI_METADATA_FIELD, uiMetadata);
}
if (userOutputs != null && !userOutputs.isEmpty()) {
xContentBuilder.field(USER_OUTPUTS_FIELD, userOutputs);
}
Expand All @@ -288,7 +268,6 @@ public static WorkflowState parse(XContentParser parser) throws IOException {
Instant provisionStartTime = null;
Instant provisionEndTime = null;
User user = null;
Map<String, Object> uiMetadata = null;
Map<String, Object> userOutputs = new HashMap<>();
Map<String, Object> resourcesCreated = new HashMap<>();

Expand Down Expand Up @@ -318,9 +297,6 @@ public static WorkflowState parse(XContentParser parser) throws IOException {
case USER_FIELD:
user = User.parse(parser);
break;
case UI_METADATA_FIELD:
uiMetadata = parser.map();
break;
case USER_OUTPUTS_FIELD:
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -367,7 +343,6 @@ public static WorkflowState parse(XContentParser parser) throws IOException {
.provisionStartTime(provisionStartTime)
.provisionEndTime(provisionEndTime)
.user(user)
.uiMetadata(uiMetadata)
.userOutputs(userOutputs)
.resourcesCreated(resourcesCreated)
.build();
Expand Down Expand Up @@ -429,14 +404,6 @@ public User getUser() {
return user;
}

/**
* A map corresponding to the UI metadata
* @return the userOutputs
*/
public Map<String, Object> getUiMetadata() {
return uiMetadata;
}

/**
* A map of essential API responses
* @return the userOutputs
Expand All @@ -452,4 +419,21 @@ public Map<String, Object> userOutputs() {
public Map<String, Object> resourcesCreated() {
return resourcesCreated;
}

@Override
public String toString() {
return "WorkflowState [workflowId="
+ workflowId
+ ", error="
+ error
+ ", state="
+ state
+ ", provisioningProgress="
+ provisioningProgress
+ ", userOutputs="
+ userOutputs
+ ", resourcesCreated="
+ resourcesCreated
+ "]";
}
}
Loading

0 comments on commit 0fb1cea

Please sign in to comment.