brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [2/3] brooklyn-server git commit: Make BasicJcloudsLocationCustomizer an EntityInitializer
Date Thu, 17 Nov 2016 10:33:30 GMT
Make BasicJcloudsLocationCustomizer an EntityInitializer


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

Branch: refs/heads/master
Commit: 413fb03a79cee4244e7de2b6601a60e512cb42f2
Parents: ac9d9e9
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Thu Nov 17 10:01:14 2016 +0000
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Thu Nov 17 10:01:14 2016 +0000

----------------------------------------------------------------------
 .../core/entity/BrooklynConfigKeys.java         |  11 +-
 .../jclouds/BasicJcloudsLocationCustomizer.java |  68 ++++++++++-
 .../BasicJcloudsLocationCustomizerTest.java     | 113 +++++++++++++++++++
 .../entity/software/base/SoftwareProcess.java   |   7 +-
 4 files changed, 186 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/413fb03a/core/src/main/java/org/apache/brooklyn/core/entity/BrooklynConfigKeys.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/BrooklynConfigKeys.java b/core/src/main/java/org/apache/brooklyn/core/entity/BrooklynConfigKeys.java
index a97a5d9..5e77613 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/BrooklynConfigKeys.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/BrooklynConfigKeys.java
@@ -23,7 +23,6 @@ import static org.apache.brooklyn.core.config.ConfigKeys.newConfigKey;
 import static org.apache.brooklyn.core.config.ConfigKeys.newConfigKeyWithPrefix;
 import static org.apache.brooklyn.core.config.ConfigKeys.newStringConfigKey;
 
-import com.google.common.annotations.Beta;
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.BasicConfigInheritance;
@@ -37,6 +36,7 @@ import org.apache.brooklyn.util.core.internal.ssh.ShellTool;
 import org.apache.brooklyn.util.core.internal.ssh.SshTool;
 import org.apache.brooklyn.util.time.Duration;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 
@@ -237,4 +237,13 @@ public class BrooklynConfigKeys {
     public static final ConfigKey<String> SSH_CONFIG_DIRECT_HEADER = newConfigKeyWithPrefix(BROOKLYN_SSH_CONFIG_KEY_PREFIX,
ShellTool.PROP_DIRECT_HEADER);
     public static final ConfigKey<Boolean> SSH_CONFIG_NO_DELETE_SCRIPT = newConfigKeyWithPrefix(BROOKLYN_SSH_CONFIG_KEY_PREFIX,
ShellTool.PROP_NO_DELETE_SCRIPT);
 
+    public static final MapConfigKey<Object> PROVISIONING_PROPERTIES = new MapConfigKey.Builder<Object>(Object.class,
"provisioning.properties")
+            .description("Custom properties to be passed in when provisioning a new machine")
+            .defaultValue(ImmutableMap.<String, Object>of())
+            .typeInheritance(BasicConfigInheritance.DEEP_MERGE)
+            .runtimeInheritance(BasicConfigInheritance.NOT_REINHERITED_ELSE_DEEP_MERGE)
+            .build();
+
+    private BrooklynConfigKeys() {}
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/413fb03a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java
b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java
index 500442e..317f4e9 100644
--- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java
+++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizer.java
@@ -18,19 +18,73 @@
  */
 package org.apache.brooklyn.location.jclouds;
 
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntityInitializer;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
+import org.apache.brooklyn.core.location.LocationConfigKeys;
+import org.apache.brooklyn.core.objs.BasicConfigurableObject;
+import org.apache.brooklyn.util.core.config.ConfigBag;
 import org.jclouds.compute.ComputeService;
 import org.jclouds.compute.domain.Template;
 import org.jclouds.compute.domain.TemplateBuilder;
 import org.jclouds.compute.options.TemplateOptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import org.apache.brooklyn.api.entity.Entity;
-import org.apache.brooklyn.core.location.LocationConfigKeys;
-import org.apache.brooklyn.core.objs.BasicConfigurableObject;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 
 /**
- * A default no-op implementation, which can be extended to override the appropriate methods.
+ * A default no-op location customizer, which can be extended to override the appropriate
methods.
+ * <p>
+ * When the class is used as an {@link EntityInitializer} it inserts itself into the entity's
+ * {@link JcloudsLocationConfig#JCLOUDS_LOCATION_CUSTOMIZERS} under the
+ * {@link BrooklynConfigKeys#PROVISIONING_PROPERTIES} key.
  */
-public class BasicJcloudsLocationCustomizer extends BasicConfigurableObject implements JcloudsLocationCustomizer
{
+public class BasicJcloudsLocationCustomizer extends BasicConfigurableObject implements JcloudsLocationCustomizer,
EntityInitializer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(BasicJcloudsLocationCustomizer.class);
+
+    public BasicJcloudsLocationCustomizer() {
+        this(ImmutableMap.<String, String>of());
+    }
+
+    public BasicJcloudsLocationCustomizer(Map<String, String> params) {
+        this(ConfigBag.newInstance(params));
+    }
+
+    public BasicJcloudsLocationCustomizer(final ConfigBag params) {
+        for (Map.Entry<String, Object> entry : params.getAllConfig().entrySet()) {
+            config().set(ConfigKeys.newConfigKey(Object.class, entry.getKey()), entry.getValue());
+        }
+    }
+
+    @Override
+    public void apply(EntityLocal entity) {
+        ConfigKey<Object> subkey = BrooklynConfigKeys.PROVISIONING_PROPERTIES.subKey(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS.getName());
+        // newInstance handles the case that provisioning properties is null.
+        ConfigBag provisioningProperties = ConfigBag.newInstance(entity.config().get(BrooklynConfigKeys.PROVISIONING_PROPERTIES));
+        Collection<JcloudsLocationCustomizer> existingCustomizers = provisioningProperties.get(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS);
+        List<? super JcloudsLocationCustomizer> merged;
+        if (existingCustomizers == null) {
+            merged = ImmutableList.<JcloudsLocationCustomizer>of(this);
+        } else {
+            merged = Lists.newArrayListWithCapacity(1 + existingCustomizers.size());
+            merged.addAll(existingCustomizers);
+            merged.add(this);
+        }
+        LOG.debug("{} set location customizers on {}: {}", new Object[]{this, entity, Iterables.toString(merged)});
+        entity.config().set(subkey, merged);
+    }
 
     @Override
     public void customize(JcloudsLocation location, ComputeService computeService, TemplateBuilder
templateBuilder) {
@@ -62,7 +116,9 @@ public class BasicJcloudsLocationCustomizer extends BasicConfigurableObject
impl
         // no-op
     }
 
-    /** @return the calling entity */
+    /**
+     * @return the calling entity
+     */
     protected Entity getCallerContext(JcloudsMachineLocation machine) {
         SudoTtyFixingCustomizer s;
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/413fb03a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizerTest.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizerTest.java
b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizerTest.java
new file mode 100644
index 0000000..ecdde08
--- /dev/null
+++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/BasicJcloudsLocationCustomizerTest.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.location.jclouds;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.apache.brooklyn.entity.group.DynamicCluster;
+import org.apache.brooklyn.util.text.Strings;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+
+public class BasicJcloudsLocationCustomizerTest extends BrooklynAppUnitTestSupport {
+
+    private BasicJcloudsLocationCustomizer testCustomizer;
+
+    @BeforeMethod
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        testCustomizer = new BasicJcloudsLocationCustomizer();
+    }
+
+    @Test
+    public void testCustomiserIncluded() {
+        TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)
+                .addInitializer(testCustomizer));
+        assertCustomizers(entity, 1);
+    }
+
+    @Test
+    public void testCustomizersMerged() {
+        TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)
+                .addInitializer(testCustomizer)
+                .configure(
+                        BrooklynConfigKeys.PROVISIONING_PROPERTIES.subKey(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS.getName()),
+                        ImmutableList.of(new BasicJcloudsLocationCustomizer())));
+        assertCustomizers(entity, 2);
+    }
+
+    @Test
+    public void testInitialiserAppliedToMembersOfCluster() {
+        final EntitySpec<TestEntity> memberSpec = EntitySpec.create(TestEntity.class)
+                .addInitializer(testCustomizer);
+        DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class)
+                .configure(DynamicCluster.INITIAL_SIZE, 3)
+                .configure(DynamicCluster.MEMBER_SPEC, memberSpec));
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+        for (Entity member : cluster.getMembers()) {
+            assertCustomizers(member, 1);
+        }
+    }
+
+    @Test
+    public void testApplyToEntity() {
+        TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        assertNoCustomizers(entity);
+        testCustomizer.apply(entity);
+        assertCustomizers(entity, 1);
+    }
+
+    @SuppressWarnings("unchecked")
+    private void assertCustomizers(Entity entity, int numberCustomizers) {
+        Object object = entity.config().get(BrooklynConfigKeys.PROVISIONING_PROPERTIES.subKey(
+                JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS.getName()));
+        assertNotNull(object, "expected value for customizers in " + entity.config().get(BrooklynConfigKeys.PROVISIONING_PROPERTIES));
+        assertTrue(object instanceof Collection, "expected collection, got " + object.getClass());
+        Collection<JcloudsLocationCustomizer> customizers = (Collection<JcloudsLocationCustomizer>)
object;
+        assertEquals(customizers.size(), numberCustomizers,
+                "expected " + numberCustomizers + " customizer" + Strings.s(numberCustomizers)
+ " in " + Iterables.toString(customizers));
+        assertTrue(customizers.contains(testCustomizer), "expected to find testCustomizer
in " + Iterables.toString(customizers));
+    }
+
+    @SuppressWarnings("unchecked")
+    private void assertNoCustomizers(Entity entity) {
+        Object object = entity.config().get(BrooklynConfigKeys.PROVISIONING_PROPERTIES.subKey(
+                JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS.getName()));
+        if (object != null) {
+            assertTrue(object instanceof Collection, "expected collection, got " + object.getClass());
+            Collection<JcloudsLocationCustomizer> customizers = (Collection<JcloudsLocationCustomizer>)
object;
+            assertEquals(customizers.size(), 0, "expected no entries in " + Iterables.toString(customizers));
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/413fb03a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/SoftwareProcess.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/SoftwareProcess.java
b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/SoftwareProcess.java
index 325d5a5..e8dfced 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/SoftwareProcess.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/SoftwareProcess.java
@@ -264,12 +264,7 @@ public interface SoftwareProcess extends Entity, Startable {
             .build();
 
     @SetFromFlag("provisioningProperties")
-    MapConfigKey<Object> PROVISIONING_PROPERTIES = new MapConfigKey.Builder<Object>(Object.class,
"provisioning.properties")
-            .description("Custom properties to be passed in when provisioning a new machine")
-            .defaultValue(ImmutableMap.<String, Object>of())
-            .typeInheritance(BasicConfigInheritance.DEEP_MERGE)
-            .runtimeInheritance(BasicConfigInheritance.NOT_REINHERITED_ELSE_DEEP_MERGE)
-            .build();
+    MapConfigKey<Object> PROVISIONING_PROPERTIES = BrooklynConfigKeys.PROVISIONING_PROPERTIES;
 
     @SetFromFlag("maxRebindSensorsDelay")
     ConfigKey<Duration> MAXIMUM_REBIND_SENSOR_CONNECT_DELAY = ConfigKeys.newConfigKey(Duration.class,


Mime
View raw message