Search in sources :

Example 1 with Scalar

use of io.jenkins.plugins.casc.model.Scalar 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 Scalar

use of io.jenkins.plugins.casc.model.Scalar 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 Scalar

use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.

the class ModelConstructor method constructMapping2ndStep.

/**
 * Enforce Map keys are only Scalars and can be used as {@link String} keys in {@link Mapping}
 */
@Override
protected void constructMapping2ndStep(MappingNode node, final Map mapping) {
    ((Mapping) mapping).setSource(getSource(node));
    super.constructMapping2ndStep(node, new AbstractMapDecorator(mapping) {

        @Override
        public Object put(Object key, Object value) {
            if (!(key instanceof Scalar))
                throw new IllegalStateException("We only support scalar map keys");
            Object scalar = ObjectUtils.clone(value);
            if (scalar instanceof Number)
                scalar = new Scalar(scalar.toString());
            else if (scalar instanceof Boolean)
                scalar = new Scalar(scalar.toString());
            return mapping.put(key.toString(), scalar);
        }
    });
}
Also used : AbstractMapDecorator(org.apache.commons.collections.map.AbstractMapDecorator) Mapping(io.jenkins.plugins.casc.model.Mapping) Scalar(io.jenkins.plugins.casc.model.Scalar)

Example 4 with Scalar

use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.

the class PrimitiveConfiguratorTest method _Integer_env.

@Test
public void _Integer_env() throws Exception {
    environment.set("ENV_FOR_TEST", "123");
    Configurator c = registry.lookupOrFail(Integer.class);
    final Object value = c.configure(new Scalar("${ENV_FOR_TEST}"), context);
    assertEquals(123, (int) value);
}
Also used : Configurator(io.jenkins.plugins.casc.Configurator) Scalar(io.jenkins.plugins.casc.model.Scalar) Test(org.junit.Test)

Example 5 with Scalar

use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.

the class PrimitiveConfiguratorTest method _string_env.

@Test
public void _string_env() throws Exception {
    environment.set("ENV_FOR_TEST", "abc");
    Configurator c = registry.lookupOrFail(String.class);
    final Object value = c.configure(new Scalar("${ENV_FOR_TEST}"), context);
    assertEquals("abc", value);
}
Also used : Configurator(io.jenkins.plugins.casc.Configurator) Scalar(io.jenkins.plugins.casc.model.Scalar) Test(org.junit.Test)

Aggregations

Scalar (io.jenkins.plugins.casc.model.Scalar)15 Test (org.junit.Test)11 Configurator (io.jenkins.plugins.casc.Configurator)8 Sequence (io.jenkins.plugins.casc.model.Sequence)4 Mapping (io.jenkins.plugins.casc.model.Mapping)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 Node (hudson.model.Node)1 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)1 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)1 CNode (io.jenkins.plugins.casc.model.CNode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 AbstractMapDecorator (org.apache.commons.collections.map.AbstractMapDecorator)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