brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/4] brooklyn-server git commit: Fix Azure VM names
Date Wed, 18 Jan 2017 12:16:03 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master db7418c6e -> 494ae6fe1


Fix Azure VM names


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

Branch: refs/heads/master
Commit: b969a3052e7d964e2c3c97dc1d93e0d8d83864f1
Parents: 2bdbf14
Author: Duncan Godwin <duncan.godwin@cloudsoftcorp.com>
Authored: Tue Jan 17 14:54:28 2017 +0000
Committer: Duncan Godwin <duncan.godwin@cloudsoftcorp.com>
Committed: Tue Jan 17 14:54:28 2017 +0000

----------------------------------------------------------------------
 .../location/cloud/CloudLocationConfig.java     |  3 +++
 .../cloud/names/BasicCloudMachineNamer.java     |  9 ++++++---
 .../location/jclouds/JcloudsMachineNamer.java   | 13 +++++++++++++
 .../brooklyn/util/text/StringShortener.java     | 19 ++++++++++++++++++-
 .../brooklyn/util/text/StringShortenerTest.java | 20 ++++++++++++++++++++
 5 files changed, 60 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b969a305/core/src/main/java/org/apache/brooklyn/core/location/cloud/CloudLocationConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/location/cloud/CloudLocationConfig.java
b/core/src/main/java/org/apache/brooklyn/core/location/cloud/CloudLocationConfig.java
index bd3add8..b355f60 100644
--- a/core/src/main/java/org/apache/brooklyn/core/location/cloud/CloudLocationConfig.java
+++ b/core/src/main/java/org/apache/brooklyn/core/location/cloud/CloudLocationConfig.java
@@ -74,6 +74,9 @@ public interface CloudLocationConfig {
     public static final ConfigKey<Integer> VM_NAME_MAX_LENGTH = ConfigKeys.newIntegerConfigKey(
         "vmNameMaxLength", "Maximum length of VM name", 60);
 
+    public static final ConfigKey<String> VM_NAME_DISALLOWED_PATTERN = ConfigKeys.newStringConfigKey(
+            "vmNameAllowedChars", "The character pattern to remove from a VM name", "[^a-zA-Z0-9\\-_]");
+
     public static final ConfigKey<Integer> VM_NAME_SALT_LENGTH = ConfigKeys.newIntegerConfigKey(
         "vmNameSaltLength", "Number of characters to use for a random identifier inserted
in hostname "
             + "to uniquely identify machines", 4);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b969a305/core/src/main/java/org/apache/brooklyn/core/location/cloud/names/BasicCloudMachineNamer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/location/cloud/names/BasicCloudMachineNamer.java
b/core/src/main/java/org/apache/brooklyn/core/location/cloud/names/BasicCloudMachineNamer.java
index 38ecbe5..4fb28af 100644
--- a/core/src/main/java/org/apache/brooklyn/core/location/cloud/names/BasicCloudMachineNamer.java
+++ b/core/src/main/java/org/apache/brooklyn/core/location/cloud/names/BasicCloudMachineNamer.java
@@ -36,8 +36,11 @@ public class BasicCloudMachineNamer extends AbstractCloudMachineNamer {
         Object context = setup.peek(CloudLocationConfig.CALLER_CONTEXT);
         Entity entity = null;
         if (context instanceof Entity) entity = (Entity) context;
-        
-        StringShortener shortener = Strings.shortener().separator("-");
+
+        StringShortener shortener = Strings.shortener();
+        shortener.setDisalowedCharacters(setup.get(CloudLocationConfig.VM_NAME_DISALLOWED_PATTERN));
+
+        shortener.separator("-");
         shortener.append("system", "brooklyn");
         
         /* timeStamp replaces the previously used randId. 
@@ -88,7 +91,7 @@ public class BasicCloudMachineNamer extends AbstractCloudMachineNamer {
                 .canRemove("user")
                 .canTruncate("appId", 2)
                 .canRemove("appId");
-        
+
         String s = shortener.getStringOfMaxLength(len);
         return sanitize(s).toLowerCase();
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b969a305/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsMachineNamer.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsMachineNamer.java
b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsMachineNamer.java
index 90e6629..0828d5f 100644
--- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsMachineNamer.java
+++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsMachineNamer.java
@@ -18,6 +18,7 @@
  */
 package org.apache.brooklyn.location.jclouds;
 
+import org.apache.brooklyn.core.location.cloud.CloudLocationConfig;
 import org.apache.brooklyn.core.location.cloud.names.BasicCloudMachineNamer;
 import org.apache.brooklyn.util.core.config.ConfigBag;
 
@@ -41,4 +42,16 @@ public class JcloudsMachineNamer extends BasicCloudMachineNamer {
         return null;
     }
 
+    protected String generateNewIdOfLength(ConfigBag setup, int len) {
+
+        // if it's azurecompute-arm it needs a different VM_NAME_ALLOWED_PATTERN
+        String pattern = setup.get(CloudLocationConfig.VM_NAME_DISALLOWED_PATTERN);
+        if ((pattern == null || pattern == CloudLocationConfig.VM_NAME_DISALLOWED_PATTERN.getDefaultValue())
&&
+                "azurecompute-arm".equals(setup.peek(JcloudsLocationConfig.CLOUD_PROVIDER)))
{
+        setup.put(CloudLocationConfig.VM_NAME_DISALLOWED_PATTERN, "[^a-zA-Z0-9]");
+        }
+
+        return super.generateNewIdOfLength(setup, len);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b969a305/utils/common/src/main/java/org/apache/brooklyn/util/text/StringShortener.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/text/StringShortener.java
b/utils/common/src/main/java/org/apache/brooklyn/util/text/StringShortener.java
index bcf88a3..7c7772b 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/text/StringShortener.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/text/StringShortener.java
@@ -28,6 +28,7 @@ public class StringShortener {
 
     protected Map<String,String> wordsByIdInOrder = new LinkedHashMap<String,String>();
     protected String separator = null;
+    protected String disalowedCharacters;
     
     protected interface ShorteningRule {
         /** returns the new list, with the relevant items in the list replaced */
@@ -55,6 +56,21 @@ public class StringShortener {
             return length;
         }
     }
+
+    protected String removeDisalowedCharacters(String input){
+        if (disalowedCharacters != null) input = input.replaceAll(disalowedCharacters, "");
+        return input;
+    }
+
+
+    public String getDisalowedCharacters() {
+        return disalowedCharacters;
+    }
+
+    public StringShortener setDisalowedCharacters(String disalowedCharacters) {
+        this.disalowedCharacters = disalowedCharacters;
+        return this;
+    }
     
     protected class RemovalRule implements ShorteningRule {
         public RemovalRule(String id) {
@@ -78,11 +94,12 @@ public class StringShortener {
     
 
     public StringShortener separator(String separator) {
-        this.separator = separator;
+        this.separator = removeDisalowedCharacters(separator);
         return this;
     }
 
     public StringShortener append(String id, String text) {
+        text = removeDisalowedCharacters(text);
         String old = wordsByIdInOrder.put(id, text);
         if (old!=null) {
             throw new IllegalStateException("Cannot append with id '"+id+"' when id already
present");

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b969a305/utils/common/src/test/java/org/apache/brooklyn/util/text/StringShortenerTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/text/StringShortenerTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/text/StringShortenerTest.java
index 90b0cad..034be8b 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/text/StringShortenerTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/text/StringShortenerTest.java
@@ -44,6 +44,26 @@ public class StringShortenerTest {
     }
 
     @Test
+    public void testDisalowedCharactersShortener() {
+        StringShortener ss = new StringShortener()
+                .setDisalowedCharacters("[^a-zA-Z0-9]")
+                .separator("-")
+                .append("1", "he-llo")
+                .append("2", "_new")
+                .append("3", "w.o.r.l.d")
+                .canRemove("2")
+                .canTruncate("1", 2)
+                .canTruncate("3", 2);
+
+        Assert.assertEquals(ss.getStringOfMaxLength(12), "helloworld");
+        Assert.assertEquals(ss.getStringOfMaxLength(9), "hellworld");
+        Assert.assertEquals(ss.getStringOfMaxLength(6), "heworl");
+        Assert.assertEquals(ss.getStringOfMaxLength(5), "hewor");
+        Assert.assertEquals(ss.getStringOfMaxLength(4), "hewo");
+        Assert.assertEquals(ss.getStringOfMaxLength(0), "");
+    }
+
+        @Test
     public void testEdgeCases() {
         StringShortener ss = new StringShortener();
         ss.separator(null);


Mime
View raw message