Search in sources :

Example 1 with PluginConfigurationException

use of alien4cloud.plugin.exception.PluginConfigurationException in project alien4cloud by alien4cloud.

the class PluginController method tryReusePreviousVersionConf.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void tryReusePreviousVersionConf(Plugin plugin) {
    // search for a previous version
    // TODO manage the case there are many previous versions
    QueryBuilder macthNameQuerybuilder = QueryBuilders.matchQuery("descriptor.id", plugin.getDescriptor().getId());
    QueryBuilder idQueryBuilder = QueryBuilders.idsQuery(MappingBuilder.indexTypeFromClass(Plugin.class)).ids(plugin.getId());
    QueryBuilder boolQuery = QueryBuilders.boolQuery().must(macthNameQuerybuilder).mustNot(idQueryBuilder);
    Plugin oldVersionPlugin = alienDAO.customFind(Plugin.class, boolQuery);
    if (oldVersionPlugin != null && oldVersionPlugin.isConfigurable()) {
        // get the configuration type
        Class<?> configType = pluginManager.getConfigurationType(plugin.getId());
        PluginConfiguration oldPluginConf = alienDAO.findById(PluginConfiguration.class, oldVersionPlugin.getId());
        // try to de-serialize it into the config of the new version
        if (oldPluginConf != null && oldPluginConf.getConfiguration() != null) {
            try {
                Object configObject = JsonUtil.readObject(JsonUtil.toString(oldPluginConf.getConfiguration()), configType);
                IPluginConfigurator configurator = pluginManager.getConfiguratorFor(plugin.getId());
                configurator.setConfiguration(configObject);
                PluginConfiguration pluginConf = new PluginConfiguration(plugin.getId(), configObject);
                alienDAO.save(pluginConf);
            } catch (IOException e) {
                log.warn("Plugin [" + plugin.getId() + "]: Failed to re-use the configuration of the previous version " + oldVersionPlugin.getId() + ". The configuration beans are not comptible", e);
            } catch (PluginConfigurationException e) {
                log.warn("Plugin [" + plugin.getId() + "]: Failed to re-use the configuration of the previous version " + oldVersionPlugin.getId() + ". Error while applying the configuration", e);
            }
        }
    }
}
Also used : IPluginConfigurator(alien4cloud.plugin.IPluginConfigurator) PluginConfiguration(alien4cloud.plugin.model.PluginConfiguration) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) PluginConfigurationException(alien4cloud.plugin.exception.PluginConfigurationException) Plugin(alien4cloud.plugin.Plugin)

Example 2 with PluginConfigurationException

use of alien4cloud.plugin.exception.PluginConfigurationException in project alien4cloud by alien4cloud.

the class PluginController method savePluginConfiguration.

@SuppressWarnings({ "unchecked", "rawtypes" })
@ApiOperation(value = "Save a configuration object for a plugin.", notes = "Save a configuration object for a plugin. Returns the newly saved configuration.  Role required [ ADMIN ]")
@RequestMapping(value = "/{pluginId:.+}/config", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public RestResponse<Object> savePluginConfiguration(@PathVariable String pluginId, @RequestBody Object configObjectRequest) throws PluginLoadingException {
    RestResponse<Object> response = RestResponseBuilder.<Object>builder().build();
    if (pluginManager.isPluginConfigurable(pluginId)) {
        Class<?> configType = pluginManager.getConfigurationType(pluginId);
        try {
            Object configObject = JsonUtil.readObject(JsonUtil.toString(configObjectRequest), configType);
            IPluginConfigurator configurator = pluginManager.getConfiguratorFor(pluginId);
            configurator.setConfiguration(configObject);
            PluginConfiguration pluginConf = new PluginConfiguration(pluginId, configObject);
            alienDAO.save(pluginConf);
            response.setData(configObject);
        } catch (IOException e) {
            response.setError(RestErrorBuilder.builder(RestErrorCode.INVALID_PLUGIN_CONFIGURATION).message("The posted configuration is not of type <" + configType.getName() + ">").build());
            log.error("The posted configuration is not of type <" + configType.getName() + ">", e);
        } catch (PluginConfigurationException e) {
            response.setError(RestErrorBuilder.builder(RestErrorCode.INVALID_PLUGIN_CONFIGURATION).message("The plugin configuration failed for plugin <" + pluginId + ">: configuration parameters are invalid.").build());
        }
    }
    return response;
}
Also used : IPluginConfigurator(alien4cloud.plugin.IPluginConfigurator) PluginConfiguration(alien4cloud.plugin.model.PluginConfiguration) IOException(java.io.IOException) PluginConfigurationException(alien4cloud.plugin.exception.PluginConfigurationException) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IPluginConfigurator (alien4cloud.plugin.IPluginConfigurator)2 PluginConfigurationException (alien4cloud.plugin.exception.PluginConfigurationException)2 PluginConfiguration (alien4cloud.plugin.model.PluginConfiguration)2 IOException (java.io.IOException)2 Audit (alien4cloud.audit.annotation.Audit)1 Plugin (alien4cloud.plugin.Plugin)1 ApiOperation (io.swagger.annotations.ApiOperation)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1