Search in sources :

Example 1 with SchemaValidator

use of com.microsoft.azure.maven.prompt.SchemaValidator in project azure-maven-plugins by microsoft.

the class ConfigurationPrompter method initialize.

public void initialize() throws IOException, InvalidConfigurationException {
    prompt = new DefaultPrompter();
    validator = new SchemaValidator();
    templates = new HashMap<>();
    commonVariables = new HashMap<>();
    final Yaml yaml = new Yaml();
    final Set<String> resourceNames = new HashSet<>();
    try (final InputStream inputStream = this.getClass().getResourceAsStream("/MessageTemplates.yaml")) {
        final Iterable<Object> rules = yaml.loadAll(inputStream);
        for (final Object rule : rules) {
            final Map<String, Object> map = (Map<String, Object>) rule;
            templates.put((String) map.get("id"), map);
            if (map.containsKey("resource")) {
                resourceNames.add((String) map.get("resource"));
            }
        }
    }
    for (final String resourceName : resourceNames) {
        final ObjectNode resourceSchema = (ObjectNode) JsonLoader.fromResource("/schema/" + resourceName + ".json");
        if (!resourceSchema.has("properties")) {
            throw new InvalidConfigurationException(String.format("Bad schema for %s: missing properties field.", resourceName));
        }
        final ObjectNode propertiesNode = (ObjectNode) resourceSchema.get("properties");
        IteratorUtils.forEach(propertiesNode.fields(), prop -> {
            try {
                this.validator.collectSingleProperty(resourceName, prop.getKey(), prop.getValue());
            } catch (JsonProcessingException e) {
                throw Lombok.sneakyThrow(e);
            }
        });
    }
}
Also used : DefaultPrompter(com.microsoft.azure.maven.prompt.DefaultPrompter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) SchemaValidator(com.microsoft.azure.maven.prompt.SchemaValidator) Yaml(org.yaml.snakeyaml.Yaml) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashSet(java.util.HashSet)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DefaultPrompter (com.microsoft.azure.maven.prompt.DefaultPrompter)1 SchemaValidator (com.microsoft.azure.maven.prompt.SchemaValidator)1 InvalidConfigurationException (com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Yaml (org.yaml.snakeyaml.Yaml)1