Search in sources :

Example 6 with ConfiguratorException

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

the class OverrideMergeStrategy method merge.

@Override
public void merge(Node root, Node node, String source) throws ConfiguratorException {
    if (root.getNodeId() != node.getNodeId()) {
        // means one of those yaml file doesn't conform to JCasC schema
        throw new ConfiguratorException(String.format("Found incompatible configuration elements %s %s", source, node.getStartMark()));
    }
    switch(root.getNodeId()) {
        case sequence:
            SequenceNode seq = (SequenceNode) root;
            SequenceNode seq2 = (SequenceNode) node;
            seq.getValue().addAll(seq2.getValue());
            return;
        case mapping:
            MappingNode map = (MappingNode) root;
            MappingNode map2 = (MappingNode) node;
            // merge common entries
            for (int i = 0; i < map2.getValue().size(); ) {
                NodeTuple t2 = map2.getValue().get(i);
                boolean found = false;
                for (NodeTuple tuple : map.getValue()) {
                    final Node key = tuple.getKeyNode();
                    final Node key2 = t2.getKeyNode();
                    if (key.getNodeId() == NodeId.scalar) {
                        // We dont support merge for more complex cases (yet)
                        if (((ScalarNode) key).getValue().equals(((ScalarNode) key2).getValue())) {
                            try {
                                merge(tuple.getValueNode(), t2.getValueNode(), source);
                            } catch (ConfiguratorConflictException e) {
                                map.getValue().set(map.getValue().indexOf(tuple), t2);
                            }
                            map2.getValue().remove(i);
                            found = true;
                            break;
                        }
                    } else {
                        throw new ConfiguratorException(String.format("Found non-mergeable configuration keys %s %s)", source, node.getEndMark()));
                    }
                }
                if (!found) {
                    ++i;
                }
            }
            // .. and add others
            map.getValue().addAll(map2.getValue());
            return;
        default:
            throw new ConfiguratorConflictException(String.format("Found conflicting configuration at %s %s", source, node.getStartMark()));
    }
}
Also used : ConfiguratorConflictException(io.jenkins.plugins.casc.ConfiguratorConflictException) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ConfiguratorException(io.jenkins.plugins.casc.ConfiguratorException) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode)

Example 7 with ConfiguratorException

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

the class YamlUtils method merge.

public static Node merge(List<YamlSource> sources, ConfigurationContext context) throws ConfiguratorException {
    Node root = null;
    MergeStrategy mergeStrategy = MergeStrategyFactory.getMergeStrategyOrDefault(context.getMergeStrategy());
    for (YamlSource<?> source : sources) {
        try (Reader reader = reader(source)) {
            final Node node = read(source, reader, context);
            if (root == null) {
                root = node;
            } else {
                if (node != null) {
                    mergeStrategy.merge(root, node, source.toString());
                }
            }
        } catch (IOException io) {
            throw new ConfiguratorException("Failed to read " + source, io);
        }
    }
    return root;
}
Also used : Node(org.yaml.snakeyaml.nodes.Node) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ConfiguratorException(io.jenkins.plugins.casc.ConfiguratorException)

Example 8 with ConfiguratorException

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

the class YamlUtils method read.

public static Node read(YamlSource source, Reader reader, ConfigurationContext context) throws IOException {
    LoaderOptions loaderOptions = new LoaderOptions();
    loaderOptions.setMaxAliasesForCollections(context.getYamlMaxAliasesForCollections());
    Composer composer = new Composer(new ParserImpl(new StreamReaderWithSource(source, reader)), new Resolver(), loaderOptions);
    try {
        return composer.getSingleNode();
    } catch (YAMLException e) {
        if (e.getMessage().startsWith("Number of aliases for non-scalar nodes exceeds the specified max")) {
            throw new ConfiguratorException(String.format("%s%nYou can increase the maximum by setting an environment variable or property%n  ENV: %s=\"100\"%n  PROPERTY: -D%s=\"100\"", e.getMessage(), ConfigurationContext.CASC_YAML_MAX_ALIASES_ENV, ConfigurationContext.CASC_YAML_MAX_ALIASES_PROPERTY));
        }
        throw e;
    }
}
Also used : Composer(org.yaml.snakeyaml.composer.Composer) LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Resolver(org.yaml.snakeyaml.resolver.Resolver) ParserImpl(org.yaml.snakeyaml.parser.ParserImpl) YAMLException(org.yaml.snakeyaml.error.YAMLException) ConfiguratorException(io.jenkins.plugins.casc.ConfiguratorException)

Example 9 with ConfiguratorException

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

the class DataBoundConfiguratorTest method shouldThrowConfiguratorException.

@Test
public void shouldThrowConfiguratorException() {
    Mapping config = new Mapping();
    config.put("foo", "foo");
    config.put("bar", "abcd");
    config.put("qix", "99");
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    try {
        registry.lookupOrFail(Foo.class).configure(config, new ConfigurationContext(registry));
        fail("above action is excepted to throw ConfiguratorException!");
    } catch (ConfiguratorException e) {
        assertThat(e.getMessage(), is("foo: Failed to construct instance of class io.jenkins.plugins.casc.impl.configurators.DataBoundConfiguratorTest$Foo.\n" + " Constructor: public io.jenkins.plugins.casc.impl.configurators.DataBoundConfiguratorTest$Foo(java.lang.String,boolean,int).\n" + " Arguments: [java.lang.String, java.lang.Boolean, java.lang.Integer].\n" + " Expected Parameters: foo java.lang.String, bar boolean, qix int"));
    }
}
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)

Aggregations

ConfiguratorException (io.jenkins.plugins.casc.ConfiguratorException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Node (org.yaml.snakeyaml.nodes.Node)3 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)2 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)2 Mapping (io.jenkins.plugins.casc.model.Mapping)2 Test (org.junit.Test)2 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)2 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)2 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)2 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Secret (hudson.util.Secret)1 BaseConfigurator (io.jenkins.plugins.casc.BaseConfigurator)1 Configurator (io.jenkins.plugins.casc.Configurator)1 ConfiguratorConflictException (io.jenkins.plugins.casc.ConfiguratorConflictException)1 CNode (io.jenkins.plugins.casc.model.CNode)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1