Skip to content

Commit

Permalink
add option to elevate to global context
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl DeBisschop committed Dec 24, 2019
1 parent 866dcd3 commit 489d89a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Map;

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.workflow.SharedOutputContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
Expand Down Expand Up @@ -64,22 +63,26 @@ public class JsonFindValueNodeStepPlugin implements NodeStepPlugin {
@PluginProperty(title = "Field Name", description = "Field name to lookup in JSON", required = true)
private String fieldName;

@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)", required = false)
private boolean elevateToGlobal;

@Override
public void executeNodeStep(final PluginStepContext context, final Map<String, Object> configuration,
final INodeEntry node) throws NodeStepException {
String path = configuration.getOrDefault("path", this.path).toString();
String group = configuration.getOrDefault("group", this.group).toString();
String name = configuration.getOrDefault("name", this.name).toString();
String fieldName = configuration.getOrDefault("fieldName", this.fieldName).toString();
boolean elevateToGlobal = configuration.getOrDefault("elevateToGlobal", this.elevateToGlobal).toString()
.equals("true");

try {
FileReader reader = new FileReader(path);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(reader);
String value = this.searchTree(rootNode, fieldName);
if (value != null) {
SharedOutputContext sharedOutputContext = context.getOutputContext();
sharedOutputContext.addOutput(group, name, value);
FileLookupUtils.addOutput(context, group, name, value, elevateToGlobal);
}
} catch (FileNotFoundException e) {
String msg = "Could not find file " + path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.List;
import java.util.Map;

import com.dtolabs.rundeck.core.execution.workflow.SharedOutputContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
Expand All @@ -34,14 +33,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Workflow Step Plug-in to find value of first matching field name in JSON file.
* Workflow Step Plug-in to find value of first matching field name in JSON
* file.
*
* Scans the specified file for the indicated field name and returns the value
* of the first matching value node. Search through non-value nodes is performed
* in a depth-first manner, so the match found is the first value match seen when
* when scanning down the file and earlier matches will mask matches that are
* less deep in the tree but later in the file. Breadth-first search could be
* implemented as an option but is not done here.
* in a depth-first manner, so the match found is the first value match seen
* when when scanning down the file and earlier matches will mask matches that
* are less deep in the tree but later in the file. Breadth-first search could
* be implemented as an option but is not done here.
*
* @author Karl DeBisschop <[email protected]>
* @since 2019-12-11
Expand All @@ -63,21 +63,25 @@ public class JsonFindValueStepPlugin implements StepPlugin {
@PluginProperty(title = "Field Name", description = "Field name to lookup in JSON", required = true)
private String fieldName;

@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)", required = false)
private boolean elevateToGlobal;

@Override
public void executeStep(PluginStepContext context, Map<String, Object> configuration) throws StepException {
String path = configuration.getOrDefault("path", this.path).toString();
String group = configuration.getOrDefault("group", this.group).toString();
String name = configuration.getOrDefault("name", this.name).toString();
String fieldName = configuration.getOrDefault("fieldName", this.fieldName).toString();
boolean elevateToGlobal = configuration.getOrDefault("elevateToGlobal", this.elevateToGlobal).toString()
.equals("true");

try {
FileReader reader = new FileReader(path);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(reader);
String value = this.searchTree(rootNode, fieldName);
if (value != null) {
SharedOutputContext sharedOutputContext = context.getOutputContext();
sharedOutputContext.addOutput(group, name, value);
FileLookupUtils.addOutput(context, group, name, value, elevateToGlobal);
}
} catch (FileNotFoundException e) {
String msg = "Could not find file " + path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.regex.Pattern;

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.workflow.SharedOutputContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
Expand Down Expand Up @@ -68,15 +67,19 @@ public class ScanFileNodeStepPlugin implements NodeStepPlugin {
@PluginProperty(title = "Pattern", description = "Regular expression to find, with one or two capture fields", required = true)
private String regex;

@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)", required = false)
private boolean elevateToGlobal;

@Override
public void executeNodeStep(PluginStepContext context, Map<String, Object> configuration, INodeEntry node)
throws NodeStepException {
String path = configuration.getOrDefault("path", this.path).toString();
String group = configuration.getOrDefault("group", this.group).toString();
String name = configuration.getOrDefault("name", this.name).toString();
String regex = configuration.getOrDefault("regex", this.regex).toString();
boolean elevateToGlobal = configuration.getOrDefault("elevateToGlobal", this.elevateToGlobal).toString()
.equals("true");

SharedOutputContext sharedOutputContext = context.getOutputContext();
Pattern pattern = Pattern.compile(regex);
MatchResult match;
Matcher matcher;
Expand All @@ -95,12 +98,13 @@ public void executeNodeStep(PluginStepContext context, Map<String, Object> confi
if (matcher.find()) {
match = matcher.toMatchResult();
if (match.groupCount() == 1) {
sharedOutputContext.addOutput(group, name, match.group(1));
FileLookupUtils.addOutput(context, group, name, match.group(1), elevateToGlobal);
return;
} else if (match.groupCount() == 2) {
// Take first value and do not overwrite, even though scanning proceeds
// through the rest of the file to find other matches to the pattern.
if (map.get(match.group(1)) == null) {
FileLookupUtils.addOutput(context, group, match.group(1), match.group(2), elevateToGlobal);
map.put(match.group(1), match.group(2));
}
}
Expand All @@ -116,8 +120,8 @@ public void executeNodeStep(PluginStepContext context, Map<String, Object> confi
throw new NodeStepException(msg, e, FileLookupFailureReason.FileNotReadable, nodeName);
}

for (Map.Entry<String, String> element : map.entrySet()) {
sharedOutputContext.addOutput(group, element.getKey(), element.getValue());
}
// for (Map.Entry<String, String> element : map.entrySet()) {
// FileLookupUtils.addOutput(context, group, element.getKey(), element.getValue(), elevateToGlobal);
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package com.bioraft.rundeck.filelookup;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.dtolabs.rundeck.core.execution.workflow.SharedOutputContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
Expand Down Expand Up @@ -67,14 +66,18 @@ public class ScanFileStepPlugin implements StepPlugin {
@PluginProperty(title = "Pattern", description = "Regular expression to find, with one or two capture fields", required = true)
private String regex;

@PluginProperty(title = "Make global?", description = "Elevate this variable to global scope (default: false)", required = false)
private boolean elevateToGlobal;

@Override
public void executeStep(PluginStepContext context, Map<String, Object> configuration) throws StepException {
String path = configuration.getOrDefault("path", this.path).toString();
String group = configuration.getOrDefault("group", this.group).toString();
String name = configuration.getOrDefault("name", this.name).toString();
String regex = configuration.getOrDefault("regex", this.regex).toString();
boolean elevateToGlobal = configuration.getOrDefault("elevateToGlobal", this.elevateToGlobal).toString()
.equals("true");

SharedOutputContext sharedOutputContext = context.getOutputContext();
Pattern pattern = Pattern.compile(regex);
MatchResult match;
Matcher matcher;
Expand All @@ -93,7 +96,7 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
if (matcher.find()) {
match = matcher.toMatchResult();
if (match.groupCount() == 1) {
sharedOutputContext.addOutput(group, name, match.group(1));
FileLookupUtils.addOutput(context, group, name, match.group(1), elevateToGlobal);
return;
} else if (match.groupCount() == 2) {
if (map.get(match.group(1)) == null) {
Expand All @@ -110,7 +113,7 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
throw new StepException(msg, e, FileLookupFailureReason.FileNotReadable);
}
for (Map.Entry<String, String> element : map.entrySet()) {
sharedOutputContext.addOutput(group, element.getKey(), element.getValue());
FileLookupUtils.addOutput(context, group, element.getKey(), element.getValue(), elevateToGlobal);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.matches;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -71,15 +71,15 @@ public class ScanFileNodeStepPluginTest {

@Captor
ArgumentCaptor<String> valueCaptor;

Map<String, Object> configuration;

@Before
public void setUp() {
this.plugin = new ScanFileNodeStepPlugin();
configuration = Stream.of(new String[][] { { "path", "testData/test.yaml" }, { "group", "example" },
{ "name", "key" }, { "regex", "single" }, })
.collect(Collectors.toMap(data -> data[0], data -> data[1]));
{ "name", "key" }, { "regex", "single" }, })
.collect(Collectors.toMap(data -> data[0], data -> data[1]));
}

@Test(expected = StepException.class)
Expand All @@ -95,7 +95,7 @@ public void canRunWithoutMatch() throws StepException {
when(context.getOutputContext()).thenReturn(sharedOutputContext);
this.plugin.executeNodeStep(context, configuration, entry);

verify(context).getOutputContext();
verify(context, never()).getOutputContext();
verify(sharedOutputContext, never()).addOutput(anyString(), anyString(), anyString());
}

Expand All @@ -122,8 +122,8 @@ public void canFindMultipleCapture() throws StepException {
when(context.getOutputContext()).thenReturn(sharedOutputContext);
this.plugin.executeNodeStep(context, configuration, entry);

verify(context).getOutputContext();
verify(sharedOutputContext, atLeastOnce()).addOutput(matches("^example$"), nameCaptor.capture(),
verify(context, times(2)).getOutputContext();
verify(sharedOutputContext, times(2)).addOutput(matches("^example$"), nameCaptor.capture(),
valueCaptor.capture());

List<String> names = nameCaptor.getAllValues();
Expand Down
Loading

0 comments on commit 489d89a

Please sign in to comment.