use of alien4cloud.plugin.IPluginConfigurator 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);
}
}
}
}
use of alien4cloud.plugin.IPluginConfigurator 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;
}
Aggregations