Search in sources :

Example 1 with PluginConfiguration

use of alien4cloud.plugin.model.PluginConfiguration 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 PluginConfiguration

use of alien4cloud.plugin.model.PluginConfiguration in project alien4cloud by alien4cloud.

the class ApplicationBootstrap method migration.

/**
 * Performs data migration for 1.3.1 version where plugin ids are not generated using the version anymore.
 */
private void migration() {
    log.debug("Initializing plugin id migrations");
    int count = 0;
    // This code updates the ids of plugin configurations and plugins in elasticsearch to remove the version reference.
    GetMultipleDataResult<Plugin> pluginResult = alienDAO.buildQuery(Plugin.class).prepareSearch().search(0, Integer.MAX_VALUE);
    for (Plugin plugin : pluginResult.getData()) {
        if (plugin.getEsId().contains(":")) {
            PluginConfiguration pluginConfiguration = alienDAO.findById(PluginConfiguration.class, plugin.getEsId());
            if (pluginConfiguration != null) {
                pluginConfiguration.setPluginId(plugin.getId());
                alienDAO.save(pluginConfiguration);
                alienDAO.delete(PluginConfiguration.class, plugin.getEsId());
            }
            alienDAO.save(plugin);
            alienDAO.delete(Plugin.class, plugin.getEsId());
            count++;
        }
    }
    if (count > 0) {
        log.info("{} plugins migrated", count);
    }
    count = 0;
    // This code updates the plugin id in the orchestrators.
    GetMultipleDataResult<Orchestrator> orchestratorResult = alienDAO.buildQuery(Orchestrator.class).prepareSearch().search(0, Integer.MAX_VALUE);
    for (Orchestrator orchestrator : orchestratorResult.getData()) {
        if (orchestrator.getPluginId().contains(":")) {
            orchestrator.setPluginId(orchestrator.getPluginId().split(":")[0]);
            alienDAO.save(orchestrator);
            count++;
        }
    }
    if (count > 0) {
        log.info("Orchestrator migrated: {}.", count);
    }
    log.debug("plugin id migration done.");
}
Also used : PluginConfiguration(alien4cloud.plugin.model.PluginConfiguration) Orchestrator(alien4cloud.model.orchestrators.Orchestrator) Plugin(alien4cloud.plugin.Plugin)

Example 3 with PluginConfiguration

use of alien4cloud.plugin.model.PluginConfiguration in project alien4cloud by alien4cloud.

the class PluginController method getPluginConfiguration.

@ApiOperation(value = "Get a plugin configuration object.", notes = "Retrieve a plugin configuration object.  Role required [ ADMIN ]")
@RequestMapping(value = "/{pluginId:.+}/config", method = RequestMethod.GET)
@PreAuthorize("hasAuthority('ADMIN')")
public RestResponse<Object> getPluginConfiguration(@PathVariable String pluginId) throws PluginLoadingException {
    RestResponse<Object> response = RestResponseBuilder.<Object>builder().build();
    // check if a config object already exist in the repository
    PluginConfiguration pluginConf = alienDAO.findById(PluginConfiguration.class, pluginId);
    if (pluginConf != null && pluginConf.getConfiguration() != null) {
        response.setData(pluginConf.getConfiguration());
        return response;
    }
    if (pluginManager.isPluginConfigurable(pluginId)) {
        Object configObject = pluginManager.getConfiguratorFor(pluginId).getDefaultConfiguration();
        response.setData(configObject);
    }
    return response;
}
Also used : PluginConfiguration(alien4cloud.plugin.model.PluginConfiguration) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with PluginConfiguration

use of alien4cloud.plugin.model.PluginConfiguration 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

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