brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [01/13] git commit: allow units to be passed for `minRam` and `minDisk`
Date Fri, 24 Oct 2014 11:54:26 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master e994a3ae4 -> 5b37ae053


allow units to be passed for `minRam` and `minDisk`


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

Branch: refs/heads/master
Commit: e157fdeb011d645cff424621e125ab41a3fdfbe8
Parents: 80c0e9a
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Oct 22 01:53:08 2014 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Oct 22 01:53:08 2014 +0100

----------------------------------------------------------------------
 .../location/cloud/CloudLocationConfig.java     | 11 ++--
 .../example_yaml/cluster-vm.yaml                |  4 +-
 .../example_yaml/simple-vm.yaml                 |  4 +-
 .../location/jclouds/JcloudsLocation.java       |  5 +-
 .../brooklyn/util/text/ByteSizeStrings.java     | 68 ++++++++++++++++++++
 .../brooklyn/util/text/ByteSizeStringsTest.java | 32 +++++++++
 6 files changed, 114 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e157fdeb/core/src/main/java/brooklyn/location/cloud/CloudLocationConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/cloud/CloudLocationConfig.java b/core/src/main/java/brooklyn/location/cloud/CloudLocationConfig.java
index 808a99a..a7bd5ac 100644
--- a/core/src/main/java/brooklyn/location/cloud/CloudLocationConfig.java
+++ b/core/src/main/java/brooklyn/location/cloud/CloudLocationConfig.java
@@ -72,11 +72,14 @@ public interface CloudLocationConfig {
             "(either a single port as a String, or an Iterable<Integer> or Integer[])",
null);
     public static final ConfigKey<Boolean> OS_64_BIT = ConfigKeys.newBooleanConfigKey("os64Bit",

         "Whether to require 64-bit OS images (true), 32-bit images (false), or either (null)");
-    public static final ConfigKey<Integer> MIN_RAM = new BasicConfigKey<Integer>(Integer.class,
"minRam",
-        "Minimum amount of RAM (in MB), for use in selecting the machine/hardware profile",
null);
+    
+    public static final ConfigKey<Object> MIN_RAM = new BasicConfigKey<Object>(Object.class,
"minRam",
+        "Minimum amount of RAM, either as string (4gb) or number of MB (4096), for use in
selecting the machine/hardware profile", null);
+    
     public static final ConfigKey<Integer> MIN_CORES = new BasicConfigKey<Integer>(Integer.class,
"minCores",
         "Minimum number of cores, for use in selecting the machine/hardware profile", null);
-    public static final ConfigKey<Double> MIN_DISK = new BasicConfigKey<Double>(Double.class,
"minDisk",
-        "Minimum size of disk (in GB), for use in selecting the machine/hardware profile",
null);
+    
+    public static final ConfigKey<Object> MIN_DISK = new BasicConfigKey<Object>(Object.class,
"minDisk",
+        "Minimum size of disk, either as string (100gb) or number of GB (100), for use in
selecting the machine/hardware profile", null);
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e157fdeb/docs/use/guide/defining-applications/example_yaml/cluster-vm.yaml
----------------------------------------------------------------------
diff --git a/docs/use/guide/defining-applications/example_yaml/cluster-vm.yaml b/docs/use/guide/defining-applications/example_yaml/cluster-vm.yaml
index 2cf71aa..a0a50da 100644
--- a/docs/use/guide/defining-applications/example_yaml/cluster-vm.yaml
+++ b/docs/use/guide/defining-applications/example_yaml/cluster-vm.yaml
@@ -7,6 +7,6 @@ services:
       type: brooklyn.entity.basic.EmptySoftwareProcess
       name: VM
       provisioning.properties:
-        minRam: 8192
+        minRam: 8g
         minCores: 4
-        minDisk: 100
+        minDisk: 100g

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e157fdeb/docs/use/guide/defining-applications/example_yaml/simple-vm.yaml
----------------------------------------------------------------------
diff --git a/docs/use/guide/defining-applications/example_yaml/simple-vm.yaml b/docs/use/guide/defining-applications/example_yaml/simple-vm.yaml
index d2756c6..042c425 100644
--- a/docs/use/guide/defining-applications/example_yaml/simple-vm.yaml
+++ b/docs/use/guide/defining-applications/example_yaml/simple-vm.yaml
@@ -3,6 +3,6 @@ services:
 - type: brooklyn.entity.basic.EmptySoftwareProcess
   name: VM
   provisioning.properties:
-    minRam: 8192
+    minRam: 8192m
     minCores: 4
-    minDisk: 100
+    minDisk: 100g

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e157fdeb/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
index 0b8807d..f10cca3 100644
--- a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
+++ b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
@@ -128,6 +128,7 @@ import brooklyn.util.ssh.IptablesCommands;
 import brooklyn.util.ssh.IptablesCommands.Chain;
 import brooklyn.util.ssh.IptablesCommands.Policy;
 import brooklyn.util.stream.Streams;
+import brooklyn.util.text.ByteSizeStrings;
 import brooklyn.util.text.Identifiers;
 import brooklyn.util.text.KeyValueParser;
 import brooklyn.util.text.Strings;
@@ -789,7 +790,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation
im
                     }})
             .put(MIN_RAM, new CustomizeTemplateBuilder() {
                     public void apply(TemplateBuilder tb, ConfigBag props, Object v) {
-                        tb.minRam(TypeCoercions.coerce(v, Integer.class));
+                        tb.minRam( (int)(ByteSizeStrings.parse(""+v, "m")/1024/1024) );
                     }})
             .put(MIN_CORES, new CustomizeTemplateBuilder() {
                     public void apply(TemplateBuilder tb, ConfigBag props, Object v) {
@@ -797,7 +798,7 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation
im
                     }})
             .put(MIN_DISK, new CustomizeTemplateBuilder() {
                     public void apply(TemplateBuilder tb, ConfigBag props, Object v) {
-                        tb.minDisk(TypeCoercions.coerce(v, Double.class));
+                        tb.minDisk( (int)(ByteSizeStrings.parse(""+v, "g")/1024/1024/1024)
);
                     }})
             .put(HARDWARE_ID, new CustomizeTemplateBuilder() {
                     public void apply(TemplateBuilder tb, ConfigBag props, Object v) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e157fdeb/utils/common/src/main/java/brooklyn/util/text/ByteSizeStrings.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/text/ByteSizeStrings.java b/utils/common/src/main/java/brooklyn/util/text/ByteSizeStrings.java
index ff34c48..ac38e61 100644
--- a/utils/common/src/main/java/brooklyn/util/text/ByteSizeStrings.java
+++ b/utils/common/src/main/java/brooklyn/util/text/ByteSizeStrings.java
@@ -21,6 +21,8 @@ package brooklyn.util.text;
 import java.util.Formattable;
 import java.util.FormattableFlags;
 import java.util.Formatter;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.annotation.Nullable;
 
@@ -345,4 +347,70 @@ public class ByteSizeStrings implements Function<Long, String>
{
         return makeSizeString(input);
     }
 
+    public static long parse(String sizeString) {
+        return parse(sizeString, null);
+    }
+    public static long parse(String sizeString, String defaultUnits) {
+        return parse(sizeString, defaultUnits, null);
+    }
+    /** parses the given string as a byte size string, e.g. "4gb"
+     * @param sizeString string to parse
+     * @param defaultUnit optional units to append if a number (no units) are supplied
+     * @param bytesMode optional evaluation mode to force 1024 or 1000 as the interpretation
of the unit prefix;
+     *   if omitted, it will depend on the units supplied,
+     *   1000 for {@link #metric()} (e.g. "1kB"), and   
+     *   1024 for {@link #java()} (e.g. "1k") and {@link #iso()} (e.g. "1KiB")
+     * @return number of bytes represented by this string
+     */
+    public static long parse(String sizeStringOriginal, String defaultUnit, ByteSizeStrings
bytesMode) {
+        String sizeString = sizeStringOriginal.trim();
+        String units;
+        Matcher matcher = Pattern.compile("[A-Za-z]+").matcher(sizeString);
+        if (!matcher.find()) {
+            if (defaultUnit==null) {
+                throw new IllegalArgumentException("Cannot parse '"+sizeStringOriginal+"'
as a size string");
+            }
+            units = defaultUnit;
+        } else {
+            units = matcher.group();
+            int unitsIndex = sizeString.indexOf(units);
+            if (sizeString.length() > unitsIndex+units.length()) {
+                throw new IllegalArgumentException("Cannot parse '"+sizeStringOriginal+"'
as a size string");
+            }
+            sizeString = sizeString.substring(0, unitsIndex).trim();
+        }
+        
+        int exponent = -1;
+        ByteSizeStrings matchedMode = null;
+        for (ByteSizeStrings mode: new ByteSizeStrings[] { ISO, JAVA, METRIC } ) {
+            matchedMode = mode;
+            if (units.equalsIgnoreCase(mode.suffixBytes.trim())) { exponent = 0; break; }
+            if (units.equalsIgnoreCase(mode.suffixKilo.trim())) { exponent = 1; break; }
+            if (units.equalsIgnoreCase(mode.suffixMega.trim())) { exponent = 2; break; }
+            if (units.equalsIgnoreCase(mode.suffixGiga.trim())) { exponent = 3; break; }
+            if (units.equalsIgnoreCase(mode.suffixTera.trim())) { exponent = 4; break; }
+        }
+        
+        if (exponent==-1) {
+            // did not match; try other standard ones 
+            if (units.equalsIgnoreCase("t")) { 
+                exponent = 4;
+                matchedMode = java();
+            } else {
+                throw new IllegalArgumentException("Cannot parse '"+sizeStringOriginal+"'
as a size string (as '"+sizeString+"' "+units+")");
+            }
+        }
+        
+        double base = Double.parseDouble(sizeString.trim());
+        
+        if (bytesMode==null) bytesMode=matchedMode;
+        
+        while (exponent>0) {
+            base *= bytesMode.bytesPerMetricUnit;
+            exponent--;
+        }
+        
+        return (long)base;
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e157fdeb/utils/common/src/test/java/brooklyn/util/text/ByteSizeStringsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/text/ByteSizeStringsTest.java b/utils/common/src/test/java/brooklyn/util/text/ByteSizeStringsTest.java
index 8f42d94..982acc1 100644
--- a/utils/common/src/test/java/brooklyn/util/text/ByteSizeStringsTest.java
+++ b/utils/common/src/test/java/brooklyn/util/text/ByteSizeStringsTest.java
@@ -128,4 +128,36 @@ public class ByteSizeStringsTest extends FixedLocaleTest {
         assertEquals(Iterables.get(bytes, 1), "21.8 GiB");
         assertEquals(Iterables.get(bytes, 2), "21.3 TiB");
     }
+    
+    public void testParse() {
+        assertEquals(ByteSizeStrings.parse("1", "k"), 1024);
+        
+        // basics
+        assertEquals(ByteSizeStrings.parse("1b"), 1);
+        assertEquals(ByteSizeStrings.parse("1k"), 1024);
+        assertEquals(ByteSizeStrings.parse("1m"), 1024*1024);
+        assertEquals(ByteSizeStrings.parse("1g"), 1024*1024*1024);
+        assertEquals(ByteSizeStrings.parse("1t"), 1024L*1024*1024*1024);
+
+        // iso
+        assertEquals(ByteSizeStrings.parse("64.0 KiB"), 65536);
+        // metric
+        assertEquals(ByteSizeStrings.parse("64.0 KB"), 64000);
+        // java
+        assertEquals(ByteSizeStrings.parse("64.0k"), 65536);
+        
+        // spaces and things
+        assertEquals(ByteSizeStrings.parse("64k"), 65536);
+        assertEquals(ByteSizeStrings.parse("64 k"), 65536);
+        
+        // smaller than zero
+        assertEquals(ByteSizeStrings.parse("0.5t"), 512L*1024*1024*1024);
+
+        // applying default unit
+        assertEquals(ByteSizeStrings.parse("1", "k"), 1024);
+        // not applying default unit
+        assertEquals(ByteSizeStrings.parse("1k", "m"), 1024);
+        // forcing use of metric
+        assertEquals(ByteSizeStrings.parse("1k", "m", ByteSizeStrings.metric()), 1000);
+    }
 }


Mime
View raw message