Search in sources :

Example 11 with Scalar

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

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

the class PrimitiveConfiguratorTest method _Integer.

@Test
public void _Integer() throws Exception {
    Configurator c = registry.lookupOrFail(Integer.class);
    final Object value = c.configure(new Scalar("123"), 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 13 with Scalar

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

the class PrimitiveConfiguratorTest method _string_env_default.

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

Example 14 with Scalar

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

the class PrimitiveConfiguratorTest method _int.

@Test
public void _int() throws Exception {
    Configurator c = registry.lookupOrFail(int.class);
    final Object value = c.configure(new Scalar("123"), 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 15 with Scalar

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

the class PrimitiveConfiguratorTest method _enum.

@Test
public void _enum() throws Exception {
    // Jenkins do register a StaplerConverter for it.
    Configurator<Node.Mode> c = registry.lookupOrFail(Node.Mode.class);
    final Node.Mode value = c.configure(new Scalar("NORMAL"), context);
    assertEquals(Node.Mode.NORMAL, value);
}
Also used : Node(hudson.model.Node) 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