Skip to content

Commit

Permalink
Merge pull request #3 from kdebisschop/json-formatted-cases
Browse files Browse the repository at this point in the history
Use JSON format for switch cases
  • Loading branch information
kdebisschop authored Jan 22, 2020
2 parents 91bddc1 + 466332e commit 31170cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ext.pluginName = 'RunDeck Conditional Logic Plugin'
ext.pluginDescription = 'Execute conditional logic in RunDeck workflows'

scmVersion {
ignoreUncommittedChanges = true
ignoreUncommittedChanges = false
tag {
// Ignore tags that begin with <prefix><versionSeparator>, include all tags
// if prefix is empty.
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/com/bioraft/rundeck/conditional/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
import com.dtolabs.rundeck.core.Constants;
import com.dtolabs.rundeck.core.dispatcher.ContextView;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Iterator;
import java.util.Map;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
Expand Down Expand Up @@ -75,15 +81,22 @@ public void switchCase(String group, String name, String cases, String test, Str
* @return True if matched, false otherwise.
*/
public boolean switchCase(String group, String name, String cases, String test, boolean elevate) {
for (String keyValue : cases.split(",")) {
String[] values = keyValue.split(":");
String key = values[0].replaceAll("^\"|\"$", "");
String value = values[1].replaceAll("^\"|\"$", "");
if (test.equals(key)) {
addOutput(elevate, group, name, value);
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + key + ".");
return true;
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode map = objectMapper.readTree(ensureStringIsJsonObject(cases));
Iterator<Map.Entry<String, JsonNode>> iterator = map.fields();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue().asText();
if (test.equals(key)) {
addOutput(elevate, group, name, value);
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + key + ".");
return true;
}
}
} catch (JsonProcessingException e) {
return false;
}
return false;
}
Expand All @@ -104,4 +117,12 @@ private void addOutput(boolean elevate, String group, String name, String value)
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Elevating to globsal ${export." + groupName + "}.");
}
}

public static String ensureStringIsJsonObject(String string) {
if (string == null) {
return "";
}
String trimmed = string.replaceFirst("^\\s*\\{?", "{").replaceFirst("\\s*$", "");
return trimmed + (trimmed.endsWith("}") ? "" : "}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private void runTest(String expected, String testValue, Map<String, String> case
String name = "test";
StringBuffer caseString = new StringBuffer();
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
caseString.setLength(caseString.length() - 1);

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
Expand All @@ -127,6 +128,7 @@ public void runTestNoDefault(Map<String, Object> configuration)
Map<String, String> cases = ImmutableMap.<String, String>builder().put("k1", "v1").put("k2", "v2").build();
StringBuilder caseString = new StringBuilder();
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
caseString.setLength(caseString.length() - 1);
configuration.put("cases", caseString.toString());

configuration.put("group", group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void runTest(String expected, String testValue, Map<String, String> case
String name = "test";
StringBuffer caseString = new StringBuffer();
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
caseString.setLength(caseString.length() - 1);

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
Expand All @@ -122,6 +123,7 @@ public void runTestNoDefault(Map<String, Object> configuration) throws StepExcep
Map<String, String> cases = ImmutableMap.<String, String>builder().put("k1", "v1").put("k2", "v2").build();
StringBuilder caseString = new StringBuilder();
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
caseString.setLength(caseString.length() - 1);
configuration.put("cases", caseString.toString());

configuration.put("group", group);
Expand Down

0 comments on commit 31170cf

Please sign in to comment.