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="));
}
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;
}
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;
}
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);
}
}
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));
}
}
Aggregations