Search in sources :

Example 1 with Mapping

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

the class CredentialsTest method testExportFileCredentials.

@Test
@ConfiguredWithCode("GlobalCredentials.yml")
public void testExportFileCredentials() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
    CNode node = root.describe(root.getTargetComponent(context), context);
    assertNotNull(node);
    final Mapping mapping = node.asMapping();
    Mapping fileCredential = mapping.get("system").asMapping().get("domainCredentials").asSequence().get(0).asMapping().get("credentials").asSequence().get(2).asMapping().get("file").asMapping();
    assertThat(fileCredential.getScalarValue("scope"), is("GLOBAL"));
    assertThat(fileCredential.getScalarValue("id"), is("secret-file"));
    assertThat(fileCredential.getScalarValue("fileName"), is("mysecretfile.txt"));
    assertThat(fileCredential.getScalarValue("secretBytes"), not("WJjZAo="));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) CredentialsRootConfigurator(com.cloudbees.plugins.credentials.casc.CredentialsRootConfigurator) Mapping(io.jenkins.plugins.casc.model.Mapping) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 2 with Mapping

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

the class BaseConfigurator method configure.

@NonNull
@Override
public T configure(CNode c, ConfigurationContext context) throws ConfiguratorException {
    final Mapping mapping = (c != null ? c.asMapping() : Mapping.EMPTY);
    final T instance = instance(mapping, context);
    if (instance instanceof Saveable) {
        try (BulkChange bc = new BulkChange((Saveable) instance)) {
            configure(mapping, instance, false, context);
            bc.commit();
        } catch (IOException e) {
            throw new ConfiguratorException("Failed to save " + instance, e);
        }
    } else {
        configure(mapping, instance, false, context);
    }
    return instance;
}
Also used : Saveable(hudson.model.Saveable) BulkChange(hudson.BulkChange) Mapping(io.jenkins.plugins.casc.model.Mapping) IOException(java.io.IOException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with Mapping

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

the class BaseConfigurator method check.

@Override
public T check(CNode c, ConfigurationContext context) throws ConfiguratorException {
    final Mapping mapping = (c != null ? c.asMapping() : Mapping.EMPTY);
    final T instance = instance(mapping, context);
    configure(mapping, instance, true, context);
    return instance;
}
Also used : Mapping(io.jenkins.plugins.casc.model.Mapping)

Example 4 with Mapping

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

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

the class ConfigurationAsCode method configureWith.

private void configureWith(Mapping entries, ConfigurationContext context) throws ConfiguratorException {
    // Initialize secret sources
    SecretSource.all().forEach(SecretSource::init);
    // Check input before actually applying changes, so we don't let controller in a
    // weird state after some ConfiguratorException has been thrown
    final Mapping clone = entries.clone();
    checkWith(clone, context);
    final ObsoleteConfigurationMonitor monitor = ObsoleteConfigurationMonitor.get();
    monitor.reset();
    context.clearListeners();
    context.addListener(monitor::record);
    try (ACLContext acl = ACL.as2(ACL.SYSTEM2)) {
        invokeWith(entries, (configurator, config) -> configurator.configure(config, context));
    }
}
Also used : ACLContext(hudson.security.ACLContext) Mapping(io.jenkins.plugins.casc.model.Mapping)

Aggregations

Mapping (io.jenkins.plugins.casc.model.Mapping)29 Test (org.junit.Test)21 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)19 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)19 CNode (io.jenkins.plugins.casc.model.CNode)12 Configurator (io.jenkins.plugins.casc.Configurator)8 ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)7 Issue (org.jvnet.hudson.test.Issue)5 Scalar (io.jenkins.plugins.casc.model.Scalar)3 Sequence (io.jenkins.plugins.casc.model.Sequence)3 CredentialsRootConfigurator (com.cloudbees.plugins.credentials.casc.CredentialsRootConfigurator)2 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)2 ProxyConfiguration (hudson.ProxyConfiguration)2 ConfiguratorException (io.jenkins.plugins.casc.ConfiguratorException)2 HashSet (java.util.HashSet)2 Jenkins (jenkins.model.Jenkins)2 AdminWhitelistRule (jenkins.security.s2m.AdminWhitelistRule)2 MasterKillSwitchConfiguration (jenkins.security.s2m.MasterKillSwitchConfiguration)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 BulkChange (hudson.BulkChange)1