Repository: brooklyn-server
Updated Branches:
  refs/heads/master 04c3283f6 -> 96768de81


Fix requiredUnless config key constraint, with attributeWhenReady

Previously this caused the entity’s startup to hang.

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/2ff53c7a
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/2ff53c7a
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/2ff53c7a

Branch: refs/heads/master
Commit: 2ff53c7a2d3551dfca69aaca296b7872da97f8cf
Parents: 04c3283
Author: Aled Sage <aled.s...@gmail.com>
Authored: Wed Oct 24 16:05:52 2018 +0100
Committer: Aled Sage <aled.s...@gmail.com>
Committed: Wed Oct 24 16:05:52 2018 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/ConfigParametersYamlTest.java | 40 ++++++++++++++++++++
 .../brooklyn/core/config/ConfigConstraints.java | 12 +++++-
 2 files changed, 51 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2ff53c7a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
----------------------------------------------------------------------
diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
index 5f277ee..5db4601 100644
--- 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
@@ -1181,6 +1181,46 @@ public class ConfigParametersYamlTest extends 
AbstractYamlRebindTest {
         assertKeyEquals(newEntity, "testRequired", null, String.class, null, 
"myprefix-myVal");
     }
 
+    @Test
+    public void 
testConfigParameterConstraintOnOtherKeyWithAttributeWhenReady() throws 
Exception {
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  itemType: entity",
+                "  items:",
+                "  - id: entity-with-keys",
+                "    item:",
+                "      type: "+TestEntity.class.getName(),
+                "      brooklyn.parameters:",
+                "      - name: key1",
+                "        type: String",
+                "        constraints:",
+                "        - requiredUnless: key2",
+                "      - name: key2",
+                "        type: String",
+                "        constraints:",
+                "        - requiredUnless: key1");
+        
+        String yaml = Joiner.on("\n").join(
+                "services:",
+                "- type: entity-with-keys",
+                "  brooklyn.config:",
+                "    key1: 
$brooklyn:root().attributeWhenReady(\"myattribute\")");
+
+        Entity app = createStartWaitAndLogApplication(yaml);
+        TestEntity entity = (TestEntity) 
Iterables.getOnlyElement(app.getChildren());
+        
+        Predicate<?> constraint = 
entity.getEntityType().getConfigKey("key1").getConstraint();
+        assertEquals(constraint.toString(), "requiredUnless(\"key2\")");
+
+        // Rebind, and then check again that the config key is listed
+        Entity newApp = rebind();
+        TestEntity newEntity = (TestEntity) 
Iterables.getOnlyElement(newApp.getChildren());
+        
+        Predicate<?> newConstraint = 
newEntity.getEntityType().getConfigKey("key1").getConstraint();
+        assertEquals(newConstraint.toString(), "requiredUnless(\"key2\")");
+    }
+
+
     public static class PredicateRegexPojo implements Predicate<Object> {
         private String regex;
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2ff53c7a/core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java 
b/core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java
index b0ed3f8..bcc9a25 100644
--- a/core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java
+++ b/core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java
@@ -300,7 +300,17 @@ public abstract class ConfigConstraints<T extends 
BrooklynObject> {
             // would be nice to offer an explanation, but that will need a 
richer API or a thread local
             List<Object> vals = new ArrayList<>();
             for (String otherKeyName : otherKeyNames) {
-                
vals.add(context.config().get(ConfigKeys.newConfigKey(Object.class, 
otherKeyName)));
+                // Use getNonBlocking, in case the value is an 
`attributeWhenReady` and the 
+                // attribute is not yet available - don't want to hang.
+                ConfigKey<Object> otherKey = 
ConfigKeys.newConfigKey(Object.class, otherKeyName);
+                BrooklynObjectInternal.ConfigurationSupportInternal 
configInternal = ((BrooklynObjectInternal) context).config();
+                
+                Maybe<?> maybeValue = configInternal.getNonBlocking(otherKey);
+                if (maybeValue.isPresent()) {
+                    vals.add(maybeValue.get());
+                } else {
+                    return true; // abort; assume valid
+                }
             }
             return test(input, vals);
         }

Reply via email to