Search in sources :

Example 1 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class ConfigMojo method doExecute.

@Override
protected void doExecute() throws AzureExecutionException {
    if (!settings.isInteractiveMode()) {
        throw new UnsupportedOperationException("The goal 'config' must be run at interactive mode.");
    }
    if (!MavenConfigUtils.isPomPackaging(this.project) && !MavenConfigUtils.isJarPackaging(this.project)) {
        throw new UnsupportedOperationException(String.format("The project (%s) with packaging %s is not supported for azure spring cloud service.", this.project.getName(), this.project.getPackaging()));
    }
    if (isProjectConfigured(this.project)) {
        getLog().warn(String.format("Project (%s) is already configured and won't be affected by this command.", this.project.getName()));
        return;
    }
    appSettings = new AppRawConfig();
    deploymentSettings = new AppDeploymentRawConfig();
    parentMode = MavenConfigUtils.isPomPackaging(this.project);
    if (parentMode && advancedOptions) {
        throw new UnsupportedOperationException("The \"advancedOptions\" mode is not supported at parent folder.");
    }
    final ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
    try {
        this.wrapper = new ConfigurationPrompter(expressionEvaluator, getLog());
        this.wrapper.initialize();
        this.wrapper.putCommonVariable("project", this.project);
        selectProjects();
        if (targetProjects == null || targetProjects.isEmpty()) {
            // no need to proceed when there are no projects need to be configured
            return;
        }
        // select subscription in spring cloud -> config is different from other goals since it is prompted after select project.
        // set up account and select subscription here
        getAzureAccount();
        promptAndSelectSubscription();
        selectAppCluster();
        configCommon();
        confirmAndSave();
    } catch (IOException | InvalidConfigurationException | UnsupportedOperationException | MavenDecryptException | LoginFailureException e) {
        throw new AzureExecutionException(e.getMessage());
    } finally {
        if (this.wrapper != null) {
            try {
                this.wrapper.close();
            } catch (IOException e) {
            // ignore at final step
            }
        }
    }
}
Also used : ConfigurationPrompter(com.microsoft.azure.maven.springcloud.config.ConfigurationPrompter) PluginParameterExpressionEvaluator(org.apache.maven.plugin.PluginParameterExpressionEvaluator) LoginFailureException(com.microsoft.azure.toolkit.lib.auth.exception.LoginFailureException) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) AppDeploymentRawConfig(com.microsoft.azure.maven.springcloud.config.AppDeploymentRawConfig) MavenDecryptException(com.microsoft.azure.maven.exception.MavenDecryptException) AppRawConfig(com.microsoft.azure.maven.springcloud.config.AppRawConfig) IOException(java.io.IOException) PluginParameterExpressionEvaluator(org.apache.maven.plugin.PluginParameterExpressionEvaluator) ExpressionEvaluator(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException)

Example 2 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class ConfigMojo method configureAppName.

private void configureAppName() throws IOException, InvalidConfigurationException {
    if (StringUtils.isNotBlank(appName) && this.parentMode) {
        throw new UnsupportedOperationException("Cannot specify appName in parent mode.");
    }
    if (this.parentMode) {
        appNameByProject = new HashMap<>();
        for (final MavenProject proj : targetProjects) {
            this.wrapper.putCommonVariable("project", proj);
            this.appNameByProject.put(proj, this.wrapper.handle("configure-app-name", this.parentMode, this.appName));
        }
        // reset back of variable project
        this.wrapper.putCommonVariable("project", this.project);
        // handle duplicate app name
        final String duplicateAppNames = this.appNameByProject.values().stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().filter(t -> t.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.joining(","));
        if (StringUtils.isNotBlank(duplicateAppNames)) {
            throw new InvalidConfigurationException(String.format("Cannot apply default appName due to duplicate: %s", duplicateAppNames));
        }
    } else {
        this.appSettings.setAppName(this.wrapper.handle("configure-app-name", false, this.appName));
    }
}
Also used : MavenProject(org.apache.maven.project.MavenProject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) InvalidConfigurationException(com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException)

Example 3 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException 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)

Example 4 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class AzureConfigurationExceptionTest method testMessage.

@Test
public void testMessage() {
    final InvalidConfigurationException ex = new InvalidConfigurationException("error");
    assertEquals("error", ex.getMessage());
}
Also used : InvalidConfigurationException(com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 5 with InvalidConfigurationException

use of com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException in project azure-maven-plugins by microsoft.

the class AzureConfigurationExceptionTest method testMessageCause.

@Test
public void testMessageCause() {
    final RuntimeException cause = new RuntimeException("cause");
    final InvalidConfigurationException ex = new InvalidConfigurationException("error", cause);
    assertEquals("error", ex.getMessage());
    assertEquals(cause, ex.getCause());
}
Also used : InvalidConfigurationException(com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException) Test(org.junit.Test)

Aggregations

InvalidConfigurationException (com.microsoft.azure.toolkit.lib.common.exception.InvalidConfigurationException)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 MavenDecryptException (com.microsoft.azure.maven.exception.MavenDecryptException)1 DefaultPrompter (com.microsoft.azure.maven.prompt.DefaultPrompter)1 SchemaValidator (com.microsoft.azure.maven.prompt.SchemaValidator)1 AppDeploymentRawConfig (com.microsoft.azure.maven.springcloud.config.AppDeploymentRawConfig)1 AppRawConfig (com.microsoft.azure.maven.springcloud.config.AppRawConfig)1 ConfigurationPrompter (com.microsoft.azure.maven.springcloud.config.ConfigurationPrompter)1 LoginFailureException (com.microsoft.azure.toolkit.lib.auth.exception.LoginFailureException)1 AzureExecutionException (com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 PluginParameterExpressionEvaluator (org.apache.maven.plugin.PluginParameterExpressionEvaluator)1 MavenProject (org.apache.maven.project.MavenProject)1