Search in sources :

Example 1 with Sequence

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));
    }
}
Also used : Sequence(io.jenkins.plugins.casc.model.Sequence) InvocationTargetException(java.lang.reflect.InvocationTargetException) Scalar(io.jenkins.plugins.casc.model.Scalar)

Example 2 with Sequence

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);
    }
}
Also used : ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) CNode(io.jenkins.plugins.casc.model.CNode) Node(org.yaml.snakeyaml.nodes.Node) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ArrayList(java.util.ArrayList) Mapping(io.jenkins.plugins.casc.model.Mapping) Sequence(io.jenkins.plugins.casc.model.Sequence) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) Scalar(io.jenkins.plugins.casc.model.Scalar) CNode(io.jenkins.plugins.casc.model.CNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) DumperOptions(org.yaml.snakeyaml.DumperOptions) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Map(java.util.Map) HashMap(java.util.HashMap) Restricted(org.kohsuke.accmod.Restricted) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull)

Example 3 with Sequence

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"));
}
Also used : ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Mapping(io.jenkins.plugins.casc.model.Mapping) Sequence(io.jenkins.plugins.casc.model.Sequence) Scalar(io.jenkins.plugins.casc.model.Scalar) Test(org.junit.Test)

Example 4 with Sequence

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));
    }
}
Also used : Sequence(io.jenkins.plugins.casc.model.Sequence) InvocationTargetException(java.lang.reflect.InvocationTargetException) Scalar(io.jenkins.plugins.casc.model.Scalar)

Example 5 with Sequence

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"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) ConfigurationAsCode(io.jenkins.plugins.casc.ConfigurationAsCode) Mapping(io.jenkins.plugins.casc.model.Mapping) Sequence(io.jenkins.plugins.casc.model.Sequence) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

Sequence (io.jenkins.plugins.casc.model.Sequence)5 Scalar (io.jenkins.plugins.casc.model.Scalar)4 Mapping (io.jenkins.plugins.casc.model.Mapping)3 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)2 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)2 CNode (io.jenkins.plugins.casc.model.CNode)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Test (org.junit.Test)2 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 ConfigurationAsCode (io.jenkins.plugins.casc.ConfigurationAsCode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Issue (org.jvnet.hudson.test.Issue)1 Restricted (org.kohsuke.accmod.Restricted)1 DumperOptions (org.yaml.snakeyaml.DumperOptions)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 Node (org.yaml.snakeyaml.nodes.Node)1 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)1 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)1