use of io.jenkins.plugins.casc.model.Sequence in project configuration-as-code-plugin by jenkinsci.
the class Attribute method describe.
public CNode describe(Owner instance, ConfigurationContext context) throws ConfiguratorException {
final Configurator c = context.lookup(type);
if (c == null) {
return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": No configurator found for type " + type);
}
try {
Object o = getValue(instance);
if (o == null) {
return null;
}
// In Export we sensitive only those values which do not get rendered as secrets
boolean shouldBeMasked = isSecret(instance);
if (multiple) {
Sequence seq = new Sequence();
if (o.getClass().isArray())
o = Arrays.asList((Object[]) o);
if (o instanceof Iterable) {
for (Object value : (Iterable) o) {
seq.add(_describe(c, context, value, shouldBeMasked));
}
} else {
LOGGER.log(Level.FINE, o.getClass() + " is not iterable");
}
return seq;
}
return _describe(c, context, o, shouldBeMasked);
} catch (Exception | /* Jenkins.getDescriptorOrDie */
AssertionError e) {
// Don't fail the whole export, prefer logging this error
LOGGER.log(Level.WARNING, "Failed to export", e);
return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": " + printThrowable(e));
}
}
use of io.jenkins.plugins.casc.model.Sequence in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method toYaml.
@CheckForNull
// for testing only
@Restricted(NoExternalUse.class)
public Node toYaml(CNode config) throws ConfiguratorException {
if (config == null)
return null;
switch(config.getType()) {
case MAPPING:
final Mapping mapping = config.asMapping();
final List<NodeTuple> tuples = new ArrayList<>();
final List<Map.Entry<String, CNode>> entries = new ArrayList<>(mapping.entrySet());
entries.sort(Map.Entry.comparingByKey());
for (Map.Entry<String, CNode> entry : entries) {
final Node valueNode = toYaml(entry.getValue());
if (valueNode == null)
continue;
tuples.add(new NodeTuple(new ScalarNode(Tag.STR, entry.getKey(), null, null, PLAIN), valueNode));
}
if (tuples.isEmpty())
return null;
return new MappingNode(Tag.MAP, tuples, BLOCK);
case SEQUENCE:
final Sequence sequence = config.asSequence();
List<Node> nodes = new ArrayList<>();
for (CNode cNode : sequence) {
final Node valueNode = toYaml(cNode);
if (valueNode == null)
continue;
nodes.add(valueNode);
}
if (nodes.isEmpty())
return null;
return new SequenceNode(Tag.SEQ, nodes, BLOCK);
case SCALAR:
default:
final Scalar scalar = config.asScalar();
final String value = scalar.getValue();
if (value == null || value.length() == 0)
return null;
final DumperOptions.ScalarStyle style;
if (scalar.getFormat().equals(Format.MULTILINESTRING) && !scalar.isRaw()) {
style = LITERAL;
} else if (scalar.isRaw()) {
style = PLAIN;
} else {
style = DOUBLE_QUOTED;
}
return new ScalarNode(getTag(scalar.getFormat()), value, null, null, style);
}
}
use of io.jenkins.plugins.casc.model.Sequence in project configuration-as-code-plugin by jenkinsci.
the class DataBoundConfiguratorTest method configureWithSets.
@Test
public void configureWithSets() throws Exception {
Mapping config = new Mapping();
Sequence sequence = new Sequence();
sequence.add(new Scalar("bar"));
sequence.add(new Scalar("foo"));
config.put("strings", sequence);
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
final Bar configured = (Bar) registry.lookupOrFail(Bar.class).configure(config, new ConfigurationContext(registry));
Set<String> strings = configured.getStrings();
assertTrue(strings.contains("foo"));
assertTrue(strings.contains("bar"));
assertFalse(strings.contains("baz"));
}
use of io.jenkins.plugins.casc.model.Sequence in project configuration-as-code-plugin by jenkinsci.
the class Attribute method describeForSchema.
/**
* This function is for the JSONSchemaGeneration
* @param instance Owner Instance
* @param context Context to be passed
* @return CNode object describing the structure of the node
*/
public CNode describeForSchema(Owner instance, ConfigurationContext context) {
final Configurator c = context.lookup(type);
if (c == null) {
return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": No configurator found for type " + type);
}
try {
Object o = getType();
if (o == null) {
return null;
}
// In Export we sensitive only those values which do not get rendered as secrets
boolean shouldBeMasked = isSecret(instance);
if (multiple) {
Sequence seq = new Sequence();
if (o.getClass().isArray())
o = Collections.singletonList(o);
if (o instanceof Iterable) {
for (Object value : (Iterable) o) {
seq.add(_describe(c, context, value, shouldBeMasked));
}
}
return seq;
}
return _describe(c, context, o, shouldBeMasked);
} catch (Exception e) {
// Don't fail the whole export, prefer logging this error
LOGGER.log(Level.WARNING, "Failed to export", e);
return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": " + printThrowable(e));
}
}
use of io.jenkins.plugins.casc.model.Sequence in project configuration-as-code-plugin by jenkinsci.
the class DataBoundConfiguratorTest method export_mapping_should_not_be_null.
@Test
@Issue("PR #838, Issue #222")
public void export_mapping_should_not_be_null() throws Exception {
j.createFreeStyleProject("testJob1");
ConfigurationAsCode casc = ConfigurationAsCode.get();
casc.configure(this.getClass().getResource("DataBoundDescriptorNonNull.yml").toString());
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
final Mapping configNode = getJenkinsRoot(context);
final CNode viewsNode = configNode.get("views");
Mapping listView = viewsNode.asSequence().get(1).asMapping().get("list").asMapping();
Mapping otherListView = viewsNode.asSequence().get(2).asMapping().get("list").asMapping();
Sequence listViewColumns = listView.get("columns").asSequence();
Sequence otherListViewColumns = otherListView.get("columns").asSequence();
assertNotNull(listViewColumns);
assertEquals(6, listViewColumns.size());
assertNotNull(otherListViewColumns);
assertEquals(7, otherListViewColumns.size());
assertEquals("loggedInUsersCanDoAnything", configNode.getScalarValue("authorizationStrategy"));
assertEquals("plainText", configNode.getScalarValue("markupFormatter"));
}
Aggregations