Search in sources :

Example 1 with InvalidPluginProperty

use of io.cdap.cdap.api.plugin.InvalidPluginProperty in project cdap by caskdata.

the class PipelineSpecGenerator method getPlugin.

/**
 * Adds a Plugin usage to the Application and create a new instance.
 *
 * @param stageName stage name
 * @param etlPlugin plugin
 * @param pluginSelector plugin selector
 * @param type type of the plugin
 * @param pluginName plugin name
 * @param collector failure collector
 * @throws ValidationException if error while creating new plugin instance
 * @return new instance of the plugin
 */
private Object getPlugin(String stageName, ETLPlugin etlPlugin, TrackedPluginSelector pluginSelector, String type, String pluginName, FailureCollector collector) {
    Object plugin = null;
    Map<String, String> pluginProperties = etlPlugin.getProperties();
    try {
        // spec
        if (runtimeEvaluator != null) {
            pluginProperties = pluginConfigurer.evaluateMacros(etlPlugin.getProperties(), runtimeEvaluator, options);
        }
        // Call to usePlugin may throw IllegalArgumentException if the plugin with the same id is already deployed.
        // This would mean there is a bug in the app and this can not be fixed by user. That is why it is not handled as
        // a ValidationFailure.
        plugin = pluginConfigurer.usePlugin(type, pluginName, stageName, PluginProperties.builder().addAll(pluginProperties).build(), pluginSelector);
    } catch (InvalidPluginConfigException e) {
        int numFailures = 0;
        for (String missingProperty : e.getMissingProperties()) {
            collector.addFailure(String.format("Required property '%s' has no value.", missingProperty), null).withConfigProperty(missingProperty);
            numFailures++;
        }
        for (InvalidPluginProperty invalidProperty : e.getInvalidProperties()) {
            collector.addFailure(e.getMessage(), null).withConfigProperty(invalidProperty.getName());
            numFailures++;
        }
        // create a generic failure
        if (numFailures == 0) {
            collector.addFailure(e.getMessage(), null);
        }
    } catch (Exception e) {
        // TODO: Catch specific exceptions when CDAP-15744 is fixed
        collector.addFailure(e.getMessage(), null).withStacktrace(e.getStackTrace());
    }
    // throw validation exception if any error occurred while creating a new instance of the plugin
    collector.getOrThrowException();
    if (plugin == null) {
        String errorMessage = String.format("Plugin named '%s' of type '%s' not found.", pluginName, type);
        String correctiveAction = String.format("Make sure plugin '%s' of type '%s' is already deployed.", pluginName, type);
        ArtifactSelectorConfig requested = etlPlugin.getArtifactConfig();
        ArtifactId requestedArtifactId = requested == null ? null : new ArtifactId(requested.getName(), new ArtifactVersion(requested.getVersion()), ArtifactScope.valueOf(requested.getScope()));
        ArtifactSelectorConfig suggestion = pluginSelector.getSuggestion();
        ArtifactId suggestedArtifactId = null;
        if (suggestion != null) {
            suggestedArtifactId = new ArtifactId(suggestion.getName(), new ArtifactVersion(suggestion.getVersion()), ArtifactScope.valueOf(suggestion.getScope()));
        }
        collector.addFailure(errorMessage, correctiveAction).withPluginNotFound(stageName, pluginName, type, requestedArtifactId, suggestedArtifactId);
        // throw validation exception if the plugin is not initialized
        collector.getOrThrowException();
    }
    return plugin;
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactSelectorConfig(io.cdap.cdap.etl.proto.ArtifactSelectorConfig) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) InvalidPluginConfigException(io.cdap.cdap.api.plugin.InvalidPluginConfigException) InvalidPluginProperty(io.cdap.cdap.api.plugin.InvalidPluginProperty) InvalidConfigPropertyException(io.cdap.cdap.etl.api.validation.InvalidConfigPropertyException) InvalidPluginConfigException(io.cdap.cdap.api.plugin.InvalidPluginConfigException) ValidationException(io.cdap.cdap.etl.api.validation.ValidationException) ConnectionBadRequestException(io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException) InvalidStageException(io.cdap.cdap.etl.api.validation.InvalidStageException)

Aggregations

ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)1 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)1 InvalidPluginConfigException (io.cdap.cdap.api.plugin.InvalidPluginConfigException)1 InvalidPluginProperty (io.cdap.cdap.api.plugin.InvalidPluginProperty)1 InvalidConfigPropertyException (io.cdap.cdap.etl.api.validation.InvalidConfigPropertyException)1 InvalidStageException (io.cdap.cdap.etl.api.validation.InvalidStageException)1 ValidationException (io.cdap.cdap.etl.api.validation.ValidationException)1 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)1 ConnectionBadRequestException (io.cdap.cdap.etl.proto.connection.ConnectionBadRequestException)1