This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 471003d76487fe92372a0f70f1336e2554185360 Author: Alex Heneveld <[email protected]> AuthorDate: Fri Aug 6 14:07:07 2021 +0100 notes on unsupported cases for scopeRoot --- .../camp/brooklyn/spi/creation/CampResolver.java | 27 ++++++++++++--------- .../camp/brooklyn/spi/dsl/DslYamlTest.java | 28 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampResolver.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampResolver.java index 548bc1e..ca27432 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampResolver.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/CampResolver.java @@ -213,12 +213,13 @@ class CampResolver { private static void fixScopeRoot(ManagementContext mgmt, Object value, Consumer<Object> updater) { Function<String,String> fixString = v -> "$brooklyn:self()" + Strings.removeFromStart((String)v, "$brooklyn:scopeRoot()"); - if (value instanceof String) { - if (((String)value).startsWith("$brooklyn:scopeRoot()")) { - updater.accept( fixString.apply((String)value) ); - } - return; - } + // TODO better approach to replacing scopeRoot + // we could replace within maps and strings, and inside DSL; currently only supported at root of config or flags + // but that's hard, we'd need to rebuild those maps and strings, which might be inside objects; + // and we'd need to replace references to scopeRoot inside a DSL, eg formatString; + // better would be to collect the DSL items we just created, and convert those if they belong to the now-root node (possibly promoted); + // it is a rare edge case however, so for now we use this poor-man's logic which captures the most common case -- + // see DslYamlTest.testDslScopeRootEdgeCases if (value instanceof BrooklynDslDeferredSupplier) { if (value.toString().startsWith("$brooklyn:scopeRoot()")) { updater.accept(DslUtils.parseBrooklynDsl(mgmt, fixString.apply(value.toString()))); @@ -226,17 +227,21 @@ class CampResolver { } } + // don't think blocks below here ever get used... + if (value instanceof String) { + if (((String)value).startsWith("$brooklyn:scopeRoot()")) { + updater.accept( fixString.apply((String)value) ); + } + return; + } + if (value instanceof DslComponent) { - // superseded by above - no longer used? + // superseded by above - no longer used if ( ((DslComponent)value).getScope() == Scope.SCOPE_ROOT ) { updater.accept( DslComponent.newInstanceChangingScope(Scope.THIS, (DslComponent) value, fixString) ); } return; } - - // TODO replace within maps and strings; currently only supported at root of config or flags - // or have some way to gather the scope root components created and convert them all; easy enough for outer, but harder for nested DSL items, - // we need to update the dsl string as well as the items themselves } } \ No newline at end of file diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java index 71238c9..d0128f6 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java @@ -15,6 +15,7 @@ */ package org.apache.brooklyn.camp.brooklyn.spi.dsl; +import org.apache.brooklyn.core.entity.EntityAsserts; import static org.testng.Assert.assertEquals; import java.util.Map; @@ -270,6 +271,33 @@ public class DslYamlTest extends AbstractYamlTest { } @Test + public void testDslScopeRootEdgeCases() throws Exception { + addCatalogItems( + "brooklyn.catalog:", + " version: " + TEST_VERSION, + " items:", + " - id: simple-item", + " itemType: entity", + " item:", + " type: "+ BasicEntity.class.getName(), + " brooklyn.config:", + " v: 1", + " refInDsl: $brooklyn:formatString(\"%s\", scopeRoot().config(\"v\"))", + " refInMap:", + " v: $brooklyn:scopeRoot().config(\"v\")"); + + final Entity app = createAndStartApplication( + "services:", + "- type: simple-item", + "brooklyn.config:", + " v: 2"); + Entity child = Iterables.get(app.getChildren(), 0); + // TODO - these should both be 1, but scopeRoot for the simple-item goes to the blueprint where it is used; see notes in CampResolver.fixScopeRoot + Asserts.assertEquals( child.getConfig(ConfigKeys.newConfigKey(Object.class, "refInDsl")), "2" ); + Asserts.assertEquals( child.getConfig(ConfigKeys.newConfigKey(Object.class, "refInMap")), MutableMap.of("v", 2) ); + } + + @Test public void testDslConfig() throws Exception { final Entity app = createAndStartApplication( "services:",
