Search in sources :

Example 16 with Mapping

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

the class DataBoundConfiguratorTest method packageParametersAreNonnullByDefault.

@Test
public void packageParametersAreNonnullByDefault() {
    Mapping config = new Mapping();
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    String expectedMessage = "string is required to configure class io.jenkins.plugins.casc.impl.configurators.nonnull.nonnullparampackage.PackageParametersAreNonnullByDefault";
    ConfiguratorException exception = assertThrows(ConfiguratorException.class, () -> registry.lookupOrFail(PackageParametersAreNonnullByDefault.class).configure(config, new ConfigurationContext(registry)));
    assertThat(exception.getMessage(), is(expectedMessage));
}
Also used : ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Mapping(io.jenkins.plugins.casc.model.Mapping) ConfiguratorException(io.jenkins.plugins.casc.ConfiguratorException) Test(org.junit.Test)

Example 17 with Mapping

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

the class DataBoundConfiguratorTest method exportYaml.

@Test
public void exportYaml() throws Exception {
    Foo foo = new Foo("foo", true, 42);
    foo.setZot("zot");
    foo.setDbl(12.34);
    // whole numbers are exported as "<number>.0"
    foo.setFlt(1f);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final Configurator c = registry.lookupOrFail(Foo.class);
    final ConfigurationContext context = new ConfigurationContext(registry);
    final CNode node = c.describe(foo, context);
    assertNotNull(node);
    assertTrue(node instanceof Mapping);
    Mapping map = (Mapping) node;
    assertEquals(map.get("foo").toString(), "foo");
    assertEquals(map.get("bar").toString(), "true");
    assertEquals(map.get("qix").toString(), "42");
    assertEquals(map.get("zot").toString(), "zot");
    assertEquals(map.get("dbl").toString(), "12.34");
    assertEquals(map.get("flt").toString(), "1.0");
    assertEquals(Util.toYamlString(map.get("foo")).trim(), "\"foo\"");
    assertEquals(Util.toYamlString(map.get("bar")).trim(), "true");
    assertEquals(Util.toYamlString(map.get("qix")).trim(), "42");
    assertEquals(Util.toYamlString(map.get("zot")).trim(), "\"zot\"");
    assertEquals(Util.toYamlString(map.get("dbl")).trim(), "\"12.34\"");
    assertEquals(Util.toYamlString(map.get("flt")).trim(), "\"1.0\"");
    assertFalse(map.containsKey("other"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Configurator(io.jenkins.plugins.casc.Configurator) Mapping(io.jenkins.plugins.casc.model.Mapping) Test(org.junit.Test)

Example 18 with Mapping

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

the class CredentialsTest method testExportSSHCredentials.

@ConfiguredWithCode("GlobalCredentials.yml")
@Test
public void testExportSSHCredentials() 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 sshCredential = mapping.get("system").asMapping().get("domainCredentials").asSequence().get(0).asMapping().get("credentials").asSequence().get(1).asMapping().get("basicSSHUserPrivateKey").asMapping();
    assertThat(sshCredential.getScalarValue("scope"), is("SYSTEM"));
    assertThat(sshCredential.getScalarValue("id"), is("agent-private-key"));
    assertThat(sshCredential.getScalarValue("username"), is("agentuser"));
    String passphrase = sshCredential.getScalarValue("passphrase");
    assertThat(passphrase, not("password"));
    assertThat(requireNonNull(Secret.decrypt(passphrase), "Failed to decrypt the password from " + passphrase).getPlainText(), is("password"));
    String sshKeyExported = sshCredential.get("privateKeySource").asMapping().get("directEntry").asMapping().get("privateKey").asScalar().getValue();
    assertThat(sshKeyExported, not("sp0ds9d+skkfjf"));
    assertThat(requireNonNull(Secret.decrypt(sshKeyExported)).getPlainText(), is("sp0ds9d+skkfjf"));
}
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 19 with Mapping

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

the class GlobalConfigurationCategoryConfigurator method describe.

@CheckForNull
@Override
public CNode describe(GlobalConfigurationCategory instance, ConfigurationContext context) {
    final Mapping mapping = new Mapping();
    Jenkins.get().getExtensionList(Descriptor.class).stream().filter(this::filterDescriptors).forEach(d -> describe(d, mapping, context));
    return mapping;
}
Also used : Descriptor(hudson.model.Descriptor) Mapping(io.jenkins.plugins.casc.model.Mapping) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull)

Example 20 with Mapping

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

the class YamlUtils method loadFrom.

/**
 * Load configuration-as-code model from a snakeyaml Node
 */
private static Mapping loadFrom(Node node) {
    final ModelConstructor constructor = new ModelConstructor();
    constructor.setComposer(new Composer(null, null) {

        @Override
        public Node getSingleNode() {
            return node;
        }
    });
    return (Mapping) constructor.getSingleData(Mapping.class);
}
Also used : Composer(org.yaml.snakeyaml.composer.Composer) Node(org.yaml.snakeyaml.nodes.Node) 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