use of io.cdap.cdap.api.plugin.PluginConfigurer in project cdap by caskdata.
the class RemoteValidationTask method run.
@Override
public void run(RunnableTaskContext context) throws Exception {
SystemAppTaskContext systemAppContext = context.getRunnableTaskSystemAppContext();
RemoteValidationRequest remoteValidationRequest = GSON.fromJson(context.getParam(), RemoteValidationRequest.class);
String namespace = remoteValidationRequest.getNamespace();
String originalRequest = remoteValidationRequest.getRequest();
StageValidationRequest validationRequest;
try {
validationRequest = GSON.fromJson(originalRequest, StageValidationRequest.class);
validationRequest.validate();
} catch (JsonSyntaxException e) {
throw new IllegalArgumentException(String.format("Unable to decode request body %s", originalRequest), e);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid stage config", e);
}
Map<String, String> arguments = Collections.emptyMap();
// this option.
if (validationRequest.getResolveMacrosFromPreferences()) {
try {
arguments = systemAppContext.getPreferencesForNamespace(namespace, true);
} catch (IllegalArgumentException iae) {
// If this is the case, we return a 404 error.
throw new IllegalArgumentException(String.format(NAMESPACE_DOES_NOT_EXIST, namespace), iae);
}
}
Map<String, MacroEvaluator> evaluators = ImmutableMap.of(SecureStoreMacroEvaluator.FUNCTION_NAME, new SecureStoreMacroEvaluator(namespace, systemAppContext), OAuthMacroEvaluator.FUNCTION_NAME, new OAuthMacroEvaluator(systemAppContext), ConnectionMacroEvaluator.FUNCTION_NAME, new ConnectionMacroEvaluator(namespace, systemAppContext));
MacroEvaluator macroEvaluator = new DefaultMacroEvaluator(new BasicArguments(arguments), evaluators, DefaultMacroEvaluator.MAP_FUNCTIONS);
MacroParserOptions macroParserOptions = MacroParserOptions.builder().skipInvalidMacros().setEscaping(false).setFunctionWhitelist(evaluators.keySet()).build();
Function<Map<String, String>, Map<String, String>> macroFn = macroProperties -> systemAppContext.evaluateMacros(namespace, macroProperties, macroEvaluator, macroParserOptions);
PluginConfigurer pluginConfigurer = systemAppContext.createPluginConfigurer(namespace);
StageValidationResponse validationResponse = ValidationUtils.validate(namespace, validationRequest, pluginConfigurer, macroFn, systemAppContext);
// If the validation success and if it only involves system artifacts, then we don't need to restart task runner
if (validationResponse.getFailures().isEmpty()) {
StageSpec spec = validationResponse.getSpec();
if (spec != null) {
context.setTerminateOnComplete(!ArtifactScope.SYSTEM.equals(spec.getPlugin().getArtifact().getScope()));
}
}
context.writeResult(GSON.toJson(validationResponse).getBytes());
}
use of io.cdap.cdap.api.plugin.PluginConfigurer in project cdap by caskdata.
the class ValidationUtilsTest method getPluginConfigurer.
// Mock PluginConfigurer
private PluginConfigurer getPluginConfigurer(PluginClass pluginClass) {
return new PluginConfigurer() {
@Override
public <T> T usePlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) {
String tableName = properties.getProperties().get("tableName");
if (tableName == null || tableName.isEmpty()) {
throw new InvalidPluginConfigException(pluginClass, Collections.singleton("tableName"), new HashSet<>());
}
MockSource.Config config = new MockSource.Config();
MockSource.ConnectionConfig connectionConfig = new MockSource.ConnectionConfig();
String schema = properties.getProperties().get("schema");
String sleep = properties.getProperties().get("sleepInMillis");
connectionConfig.setTableName(tableName);
config.setConfig(connectionConfig, schema, null, sleep == null ? null : Long.parseLong(sleep));
return (T) new MockSource(config);
}
@Override
public <T> Class<T> usePluginClass(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) {
return null;
}
@Override
public Map<String, String> evaluateMacros(Map<String, String> properties, MacroEvaluator evaluator, MacroParserOptions options) throws InvalidMacroException {
return null;
}
};
}
Aggregations