use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class PluginInstantiator method substituteMacros.
public PluginProperties substituteMacros(Plugin plugin, @Nullable MacroEvaluator macroEvaluator) {
Map<String, String> properties = new HashMap<>();
Map<String, PluginPropertyField> pluginPropertyFieldMap = plugin.getPluginClass().getProperties();
// create macro evaluator and parser based on if it is config or runtime
boolean configTime = (macroEvaluator == null);
TrackingMacroEvaluator trackingMacroEvaluator = new TrackingMacroEvaluator();
for (Map.Entry<String, String> property : plugin.getProperties().getProperties().entrySet()) {
PluginPropertyField field = pluginPropertyFieldMap.get(property.getKey());
String propertyValue = property.getValue();
if (field != null && field.isMacroSupported()) {
// TODO: cleanup after endpoint to get plugin details is merged (#6089)
if (configTime) {
// parse for syntax check and check if trackingMacroEvaluator finds macro syntax present
MacroParser macroParser = new MacroParser(trackingMacroEvaluator, field.isMacroEscapingEnabled());
macroParser.parse(propertyValue);
propertyValue = getOriginalOrDefaultValue(propertyValue, property.getKey(), field.getType(), trackingMacroEvaluator);
} else {
MacroParser macroParser = new MacroParser(macroEvaluator, field.isMacroEscapingEnabled());
propertyValue = macroParser.parse(propertyValue);
}
}
properties.put(property.getKey(), propertyValue);
}
return PluginProperties.builder().addAll(properties).build();
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testPluginNamespaceIsolation.
@Test
public void testPluginNamespaceIsolation() throws Exception {
// add a system artifact
ArtifactId systemId = NamespaceId.SYSTEM.artifact("wordcount", "1.0.0");
addSystemArtifacts();
Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(systemId.getNamespace(), systemId.getArtifact(), new ArtifactVersion(systemId.getVersion()), true, new ArtifactVersion(systemId.getVersion()), true));
NamespaceId namespace1 = new NamespaceId("ns1");
NamespaceId namespace2 = new NamespaceId("ns2");
createNamespace(namespace1.getNamespace());
createNamespace(namespace2.getNamespace());
try {
// add some plugins in namespace1. Will contain Plugin1 and Plugin2
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
ArtifactId pluginsId1 = namespace1.artifact("plugins1", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId1), Plugin1.class, manifest, parents).getStatusLine().getStatusCode());
// add some plugins in namespace2. Will contain Plugin1 and Plugin2
manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
ArtifactId pluginsId2 = namespace2.artifact("plugins2", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId2), Plugin1.class, manifest, parents).getStatusLine().getStatusCode());
ArtifactSummary artifact1 = new ArtifactSummary(pluginsId1.getArtifact(), pluginsId1.getVersion(), ArtifactScope.USER);
ArtifactSummary artifact2 = new ArtifactSummary(pluginsId2.getArtifact(), pluginsId2.getVersion(), ArtifactScope.USER);
PluginSummary summary1Namespace1 = new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact1);
PluginSummary summary2Namespace1 = new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact1);
PluginSummary summary1Namespace2 = new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), artifact2);
PluginSummary summary2Namespace2 = new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), artifact2);
PluginInfo info1Namespace1 = new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", artifact1, ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true)), new HashSet<>());
PluginInfo info2Namespace1 = new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", artifact1, ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false)), new HashSet<>());
PluginInfo info1Namespace2 = new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", artifact2, ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true)), new HashSet<>());
PluginInfo info2Namespace2 = new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", artifact2, ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false)), new HashSet<>());
ArtifactId namespace1Artifact = namespace1.artifact(systemId.getArtifact(), systemId.getVersion());
ArtifactId namespace2Artifact = namespace2.artifact(systemId.getArtifact(), systemId.getVersion());
// should see same types in both namespaces
Assert.assertEquals(ImmutableSet.of("dummy", "callable"), getPluginTypes(namespace1Artifact, ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of("dummy", "callable"), getPluginTypes(namespace2Artifact, ArtifactScope.SYSTEM));
// should see that plugins in namespace1 come only from the namespace1 artifact
Assert.assertEquals(ImmutableSet.of(summary1Namespace1), getPluginSummaries(namespace1Artifact, "dummy", ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of(summary2Namespace1), getPluginSummaries(namespace1Artifact, "callable", ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of(info1Namespace1), getPluginInfos(namespace1Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of(info2Namespace1), getPluginInfos(namespace1Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
// should see that plugins in namespace2 come only from the namespace2 artifact
Assert.assertEquals(ImmutableSet.of(summary1Namespace2), getPluginSummaries(namespace2Artifact, "dummy", ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of(summary2Namespace2), getPluginSummaries(namespace2Artifact, "callable", ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of(info1Namespace2), getPluginInfos(namespace2Artifact, "dummy", "Plugin1", ArtifactScope.SYSTEM));
Assert.assertEquals(ImmutableSet.of(info2Namespace2), getPluginInfos(namespace2Artifact, "callable", "Plugin2", ArtifactScope.SYSTEM));
} finally {
deleteNamespace("iso1");
deleteNamespace("iso2");
cleanupSystemArtifactsDirectory();
}
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactHttpHandlerTest method testGetPlugins.
@Test
public void testGetPlugins() throws Exception {
// add an app for plugins to extend
ArtifactId wordCount1Id = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordCount1Id), WordCountApp.class).getStatusLine().getStatusCode());
ArtifactId wordCount2Id = NamespaceId.DEFAULT.artifact("wordcount", "2.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordCount2Id), WordCountApp.class).getStatusLine().getStatusCode());
// add some plugins.
// plugins-1.0.0 extends wordcount[1.0.0,2.0.0)
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, Plugin1.class.getPackage().getName());
ArtifactId pluginsId1 = NamespaceId.DEFAULT.artifact("plugins", "1.0.0");
Set<ArtifactRange> plugins1Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId1), Plugin1.class, manifest, plugins1Parents).getStatusLine().getStatusCode());
// plugin-2.0.0 extends wordcount[1.0.0,3.0.0)
ArtifactId pluginsId2 = NamespaceId.DEFAULT.artifact("plugins", "2.0.0");
Set<ArtifactRange> plugins2Parents = Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "wordcount", new ArtifactVersion("1.0.0"), new ArtifactVersion("3.0.0")));
Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(pluginsId2), Plugin1.class, manifest, plugins2Parents).getStatusLine().getStatusCode());
ArtifactSummary plugins1Artifact = new ArtifactSummary("plugins", "1.0.0");
ArtifactSummary plugins2Artifact = new ArtifactSummary("plugins", "2.0.0");
// get plugin types, should be the same for both
Set<String> expectedTypes = Sets.newHashSet("dummy", "callable");
Set<String> actualTypes = getPluginTypes(wordCount1Id);
Assert.assertEquals(expectedTypes, actualTypes);
actualTypes = getPluginTypes(wordCount2Id);
Assert.assertEquals(expectedTypes, actualTypes);
// get plugin summaries. wordcount1 should see plugins from both plugin artifacts
Set<PluginSummary> expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins1Artifact), new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact));
Set<PluginSummary> actualSummaries = getPluginSummaries(wordCount1Id, "dummy");
Assert.assertEquals(expectedSummaries, actualSummaries);
expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins1Artifact), new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
actualSummaries = getPluginSummaries(wordCount1Id, "callable");
Assert.assertEquals(expectedSummaries, actualSummaries);
// wordcount2 should only see plugins from plugins2 artifact
expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), plugins2Artifact));
actualSummaries = getPluginSummaries(wordCount2Id, "dummy");
Assert.assertEquals(expectedSummaries, actualSummaries);
expectedSummaries = Sets.newHashSet(new PluginSummary("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), plugins2Artifact));
actualSummaries = getPluginSummaries(wordCount2Id, "callable");
Assert.assertEquals(expectedSummaries, actualSummaries);
// get plugin info. Again, wordcount1 should see plugins from both artifacts
Map<String, PluginPropertyField> p1Properties = ImmutableMap.of("x", new PluginPropertyField("x", "", "int", true, false), "stuff", new PluginPropertyField("stuff", "", "string", true, true));
Map<String, PluginPropertyField> p2Properties = ImmutableMap.of("v", new PluginPropertyField("v", "value to return when called", "int", true, false));
Set<PluginInfo> expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", plugins1Artifact, p1Properties, new HashSet<>()), new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", plugins2Artifact, p1Properties, new HashSet<>()));
Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "dummy", "Plugin1"));
expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", plugins1Artifact, p2Properties, new HashSet<>()), new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", plugins2Artifact, p2Properties, new HashSet<>()));
Assert.assertEquals(expectedInfos, getPluginInfos(wordCount1Id, "callable", "Plugin2"));
// while wordcount2 should only see plugins from plugins2 artifact
expectedInfos = Sets.newHashSet(new PluginInfo("Plugin1", "dummy", "This is plugin1", Plugin1.class.getName(), "config", plugins2Artifact, p1Properties, new HashSet<>()));
Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "dummy", "Plugin1"));
expectedInfos = Sets.newHashSet(new PluginInfo("Plugin2", "callable", "Just returns the configured integer", Plugin2.class.getName(), "config", plugins2Artifact, p2Properties, new HashSet<>()));
Assert.assertEquals(expectedInfos, getPluginInfos(wordCount2Id, "callable", "Plugin2"));
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class LookupTransform method getPluginClass.
private static PluginClass getPluginClass() {
Map<String, PluginPropertyField> properties = new HashMap<>();
properties.put("lookupKey", new PluginPropertyField("fields", "", "string", true, false));
properties.put("destinationField", new PluginPropertyField("destinationField", "", "string", true, false));
properties.put("lookupName", new PluginPropertyField("lookupName", "", "string", true, false));
return new PluginClass(Transform.PLUGIN_TYPE, "Lookup", "", LookupTransform.class.getName(), "config", properties);
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class MockExternalSource method getPluginClass.
private static PluginClass getPluginClass() {
Map<String, PluginPropertyField> properties = new HashMap<>();
properties.put("name", new PluginPropertyField("name", "", "string", true, false));
properties.put("dirName", new PluginPropertyField("dirName", "", "string", true, false));
return new PluginClass(BatchSource.PLUGIN_TYPE, PLUGIN_NAME, "", MockExternalSource.class.getName(), "config", properties);
}
Aggregations