Search in sources :

Example 6 with Mapping

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

the class DataBoundConfigurator method describe.

@CheckForNull
@Override
public CNode describe(T instance, ConfigurationContext context) throws Exception {
    // Here we assume a correctly designed DataBound Object will have required attributes set by DataBoundConstructor
    // and all others using DataBoundSetters. So constructor parameters for sure are part of the description, others
    // need to be compared with default values.
    // Build same object with only constructor parameters
    final Constructor constructor = getDataBoundConstructor();
    final Parameter[] parameters = constructor.getParameters();
    final String[] names = ClassDescriptor.loadParameterNames(constructor);
    final Attribute[] attributes = new Attribute[parameters.length];
    final Object[] args = new Object[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        final Parameter p = parameters[i];
        final Attribute a = createAttribute(names[i], TypePair.of(p));
        if (a != null) {
            Object value = a.getValue(instance);
            if (value != null) {
                Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
                if (converted instanceof Collection || p.getType().isArray() || !a.isMultiple()) {
                    args[i] = converted;
                } else if (Set.class.isAssignableFrom(p.getType())) {
                    args[i] = Collections.singleton(converted);
                } else {
                    args[i] = Collections.singletonList(converted);
                }
            }
            if (args[i] == null && p.getType().isPrimitive()) {
                args[i] = defaultPrimitiveValue(p.getType());
            }
            attributes[i] = a;
        }
    }
    T ref = (T) constructor.newInstance(args);
    // compare instance with this "default" object
    Mapping mapping = compare(instance, ref, context);
    // add constructor parameters
    for (int i = 0; i < parameters.length; i++) {
        if (args[i] == null)
            continue;
        mapping.put(names[i], attributes[i].describe(instance, context));
    }
    return mapping;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Attribute(io.jenkins.plugins.casc.Attribute) DescribableAttribute(io.jenkins.plugins.casc.impl.attributes.DescribableAttribute) DataBoundConstructor(org.kohsuke.stapler.DataBoundConstructor) Constructor(java.lang.reflect.Constructor) Mapping(io.jenkins.plugins.casc.model.Mapping) Parameter(java.lang.reflect.Parameter) Collection(java.util.Collection) CheckForNull(javax.annotation.CheckForNull)

Example 7 with Mapping

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

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

the class AdminWhitelistRuleConfiguratorTest method checkA2MAccessControl_disable.

@Test
@Issue("Issue #172")
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Agent2MasterSecurityKillSwitch_disabled.yml")
public void checkA2MAccessControl_disable() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    MasterKillSwitchConfiguration config = jenkins.getDescriptorByType(MasterKillSwitchConfiguration.class);
    Assert.assertFalse("Agent → Master Access Control should be disabled", config.getMasterToSlaveAccessControl());
    AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(AdminWhitelistRule.class);
    final CNode node = c.describe(rule, context);
    final Mapping agent = node.asMapping();
    assertEquals("false", agent.get("enabled").toString());
}
Also used : Jenkins(jenkins.model.Jenkins) CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) AdminWhitelistRule(jenkins.security.s2m.AdminWhitelistRule) Configurator(io.jenkins.plugins.casc.Configurator) MasterKillSwitchConfiguration(jenkins.security.s2m.MasterKillSwitchConfiguration) Mapping(io.jenkins.plugins.casc.model.Mapping) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 9 with Mapping

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

the class AdminWhitelistRuleConfiguratorTest method checkA2MAccessControl_enabled.

@Test
@Issue("Issue #172")
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Agent2MasterSecurityKillSwitch_enabled.yml")
public void checkA2MAccessControl_enabled() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    MasterKillSwitchConfiguration config = jenkins.getDescriptorByType(MasterKillSwitchConfiguration.class);
    Assert.assertTrue("Agent → Master Access Control should be enabled", config.getMasterToSlaveAccessControl());
    AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(AdminWhitelistRule.class);
    final CNode node = c.describe(rule, context);
    final Mapping agent = node.asMapping();
    assertEquals("true", agent.get("enabled").toString());
}
Also used : Jenkins(jenkins.model.Jenkins) CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) AdminWhitelistRule(jenkins.security.s2m.AdminWhitelistRule) Configurator(io.jenkins.plugins.casc.Configurator) MasterKillSwitchConfiguration(jenkins.security.s2m.MasterKillSwitchConfiguration) Mapping(io.jenkins.plugins.casc.model.Mapping) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 10 with Mapping

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

the class ProxyConfiguratorTest method shouldSetProxyWithAllFields.

@Test
@ConfiguredWithCode("Proxy.yml")
public void shouldSetProxyWithAllFields() throws Exception {
    ProxyConfiguration proxy = j.jenkins.proxy;
    assertEquals(proxy.name, "proxyhost");
    assertEquals(proxy.port, 80);
    assertEquals(proxy.getUserName(), "login");
    assertThat(proxy.getSecretPassword(), hasPlainText("password"));
    assertEquals(proxy.noProxyHost, "externalhost");
    assertEquals(proxy.getTestUrl(), "http://google.com");
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(ProxyConfiguration.class);
    final CNode node = c.describe(proxy, context);
    assertNotNull(node);
    Mapping mapping = node.asMapping();
    assertEquals(6, mapping.size());
    assertEquals("proxyhost", mapping.getScalarValue("name"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) ProxyConfiguration(hudson.ProxyConfiguration) Configurator(io.jenkins.plugins.casc.Configurator) Mapping(io.jenkins.plugins.casc.model.Mapping) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

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