Skip to content

Commit

Permalink
Use JSON format for switch cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl DeBisschop committed Jan 20, 2020
1 parent af7c207 commit 91bddc1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
15 changes: 9 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ apply plugin: 'idea'
sourceCompatibility = 1.8
defaultTasks 'clean','build'
ext.rundeckPluginVersion = '1.2'
ext.pluginClassNames='com.bioraft.rundeck.conditional.IfElseNodeStepPlugin,com.bioraft.rundeck.conditional.IfElseStepPlugin,com.bioraft.rundeck.conditional.SwitchCaseNodeStepPlugin,com.bioraft.rundeck.conditional.SwitchCaseStepPlugin'
ext.pluginClassNames='com.bioraft.rundeck.conditional.IfElseNodeStepPlugin,' +
'com.bioraft.rundeck.conditional.IfElseStepPlugin,' +
'com.bioraft.rundeck.conditional.SwitchCaseNodeStepPlugin,' +
'com.bioraft.rundeck.conditional.SwitchCaseStepPlugin'
ext.pluginName = 'RunDeck Conditional Logic Plugin'
ext.pluginDescription = 'Execute conditional logic in RunDeck workflows'

scmVersion {
ignoreUncommittedChanges = true
tag {
// Ignore tags that begin with <prefix><versionSeparator>, include all tags
// if prefix is empty.
prefix = ''
versionSeparator = ''

// Append .0 to satisfy SemVer if the tag version is only X.Y
def origDeserialize=deserialize
//apend .0 to satisfy semver if the tag version is only X.Y
deserialize = { config, position, tagName ->
def orig = origDeserialize(config, position, tagName)
String orig = origDeserialize(config, position, tagName)
if (orig.split('\\.').length < 3) {
orig += ".0"
}
Expand All @@ -32,9 +38,7 @@ scmVersion {
}
project.version = scmVersion.version


repositories {
mavenLocal()
mavenCentral()
}

Expand All @@ -60,7 +64,6 @@ dependencies {
)
}


// task to copy plugin libs to output/lib dir
task copyToLib(type: Copy) {
into "$buildDir/output/lib"
Expand Down
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon Jan 20 09:37:26 EST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
11 changes: 6 additions & 5 deletions src/main/java/com/bioraft/rundeck/conditional/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ 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(";")) {
for (String keyValue : cases.split(",")) {
String[] values = keyValue.split(":");
if (test.equals(values[0])) {
addOutput(elevate, group, name, values[1]);
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Matched " + values[0] + ".");
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;
}
}
Expand All @@ -102,5 +104,4 @@ private void addOutput(boolean elevate, String group, String name, String value)
ctx.getLogger().log(Constants.DEBUG_LEVEL, "Elevating to globsal ${export." + groupName + "}.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.descriptions.RenderingOption;
import com.dtolabs.rundeck.plugins.descriptions.RenderingOptions;
import com.dtolabs.rundeck.plugins.step.NodeStepPlugin;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;

import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.CODE_SYNTAX_MODE;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.DISPLAY_TYPE_KEY;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
* step variable.
Expand All @@ -46,7 +51,11 @@ public class SwitchCaseNodeStepPlugin implements NodeStepPlugin {
@PluginProperty(title = "Name", description = "Variable name (i.e., ${group.name}", required = true)
private String name;

@PluginProperty(title = "Cases", description = "Cases and results as colon-spliced pairs, seprated by semicolons", required = true)
@PluginProperty(title = "Cases", description = "Cases and results as colon-spliced pairs of quoted strings, separated by commas (i.e., members of a JSON object)", required = true)
@RenderingOptions({
@RenderingOption(key = DISPLAY_TYPE_KEY, value = "CODE"),
@RenderingOption(key = CODE_SYNTAX_MODE, value = "json"),
})
private String cases;

@PluginProperty(title = "Test Value", description = "Test value", required = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.descriptions.RenderingOption;
import com.dtolabs.rundeck.plugins.descriptions.RenderingOptions;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import com.dtolabs.rundeck.plugins.step.StepPlugin;

import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.CODE_SYNTAX_MODE;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.DISPLAY_TYPE_KEY;

/**
* Workflow Node Step Plug-in to choose one of several values to uplift into a
* step variable.
Expand All @@ -44,7 +49,11 @@ public class SwitchCaseStepPlugin implements StepPlugin {
@PluginProperty(title = "Name", description = "Variable name (i.e., ${group.name}", required = true)
private String name;

@PluginProperty(title = "Cases", description = "Cases and results as colon-spliced pairs, seprated by semicolons", required = true)
@PluginProperty(title = "Cases", description = "Cases and results as colon-spliced pairs of quoted strings, separated by commas (i.e., members of a JSON object)", required = true)
@RenderingOptions({
@RenderingOption(key = DISPLAY_TYPE_KEY, value = "CODE"),
@RenderingOption(key = CODE_SYNTAX_MODE, value = "json"),
})
private String cases;

@PluginProperty(title = "Test Value", description = "Test value", required = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void runTest(String expected, String testValue, Map<String, String> case
String group = "raft";
String name = "test";
StringBuffer caseString = new StringBuffer();
cases.forEach((k, v) -> caseString.append(k).append(":").append(v).append(";"));
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
Expand All @@ -126,7 +126,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(k).append(":").append(v).append(";"));
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
configuration.put("cases", caseString.toString());

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

Map<String, Object> configuration = new HashMap<>();
configuration.put("group", group);
Expand All @@ -121,7 +121,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(k).append(":").append(v).append(";"));
cases.forEach((k, v) -> caseString.append('"').append(k).append('"').append(":").append('"').append(v).append('"').append(","));
configuration.put("cases", caseString.toString());

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

0 comments on commit 91bddc1

Please sign in to comment.