You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2013/09/12 21:54:13 UTC

[1/3] git commit: JCLOUDS-253: Properly parse Chef Server version

Updated Branches:
  refs/heads/253-parseversion [created] fb8c7f1c3


JCLOUDS-253: Properly parse Chef Server version


Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/commit/35cb1834
Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/tree/35cb1834
Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/diff/35cb1834

Branch: refs/heads/253-parseversion
Commit: 35cb183445315aa6eeaea8cf3a6773133b0c4bd6
Parents: 22f9233
Author: Ignasi Barrera <na...@apache.org>
Authored: Tue Sep 10 13:26:22 2013 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Tue Sep 10 14:26:31 2013 +0200

----------------------------------------------------------------------
 .../jclouds/chef/config/ChefParserModule.java   | 29 ++------
 .../chef/suppliers/ChefVersionSupplier.java     | 76 ++++++++++++++++++++
 .../chef/suppliers/ChefVersionSupplierTest.java | 47 ++++++++++++
 3 files changed, 130 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/35cb1834/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java b/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
index f7457fe..a1012c0 100644
--- a/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
+++ b/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
@@ -31,8 +31,6 @@ import java.security.cert.X509Certificate;
 import java.security.spec.InvalidKeySpecException;
 import java.util.Map;
 import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -42,6 +40,7 @@ import org.jclouds.chef.functions.ParseCookbookDefinitionFromJson;
 import org.jclouds.chef.functions.ParseCookbookVersionsV09FromJson;
 import org.jclouds.chef.functions.ParseCookbookVersionsV10FromJson;
 import org.jclouds.chef.functions.ParseKeySetFromJson;
+import org.jclouds.chef.suppliers.ChefVersionSupplier;
 import org.jclouds.crypto.Crypto;
 import org.jclouds.crypto.Pems;
 import org.jclouds.http.HttpResponse;
@@ -49,7 +48,6 @@ import org.jclouds.json.config.GsonModule.DateAdapter;
 import org.jclouds.json.config.GsonModule.Iso8601DateAdapter;
 import org.jclouds.json.internal.NullFilteringTypeAdapterFactories.MapTypeAdapterFactory;
 import org.jclouds.json.internal.NullHackJsonLiteralAdapter;
-import org.jclouds.rest.annotations.ApiVersion;
 
 import com.google.common.base.Charsets;
 import com.google.common.base.Function;
@@ -254,7 +252,8 @@ public class ChefParserModule extends AbstractModule {
             K name = keyAdapter.read(in);
             V value = valueAdapter.read(in);
             if (value != null) {
-               // If there are repeated keys, overwrite them to only keep the last one
+               // If there are repeated keys, overwrite them to only keep the
+               // last one
                result.put(name, value);
             }
          }
@@ -307,31 +306,17 @@ public class ChefParserModule extends AbstractModule {
    @Provides
    @Singleton
    @CookbookParser
-   public Function<HttpResponse, Set<String>> provideCookbookDefinitionAdapter(@ApiVersion String apiVersion,
+   public Function<HttpResponse, Set<String>> provideCookbookDefinitionAdapter(ChefVersionSupplier chefVersionSupplier,
          ParseCookbookDefinitionFromJson v10parser, ParseKeySetFromJson v09parser) {
-      Pattern versionPattern = Pattern.compile("\\d\\.(\\d)\\.\\d");
-      Matcher m = versionPattern.matcher(apiVersion);
-      if (m.matches()) {
-         return Integer.valueOf(m.group(1)) > 9 ? v10parser : v09parser;
-      } else {
-         // Default to the latest version of the parser
-         return v10parser;
-      }
+      return chefVersionSupplier.get() >= 10 ? v10parser : v09parser;
    }
 
    @Provides
    @Singleton
    @CookbookVersionsParser
-   public Function<HttpResponse, Set<String>> provideCookbookDefinitionAdapter(@ApiVersion String apiVersion,
+   public Function<HttpResponse, Set<String>> provideCookbookDefinitionAdapter(ChefVersionSupplier chefVersionSupplier,
          ParseCookbookVersionsV10FromJson v10parser, ParseCookbookVersionsV09FromJson v09parser) {
-      Pattern versionPattern = Pattern.compile("\\d\\.(\\d)\\.\\d");
-      Matcher m = versionPattern.matcher(apiVersion);
-      if (m.matches()) {
-         return Integer.valueOf(m.group(1)) > 9 ? v10parser : v09parser;
-      } else {
-         // Default to the latest version of the parser
-         return v10parser;
-      }
+      return chefVersionSupplier.get() >= 10 ? v10parser : v09parser;
    }
 
    @Override

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/35cb1834/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java b/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
new file mode 100644
index 0000000..8d9e627
--- /dev/null
+++ b/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
@@ -0,0 +1,76 @@
+/*
+ * 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.jclouds.chef.suppliers;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.jclouds.chef.config.ChefProperties;
+import org.jclouds.logging.Logger;
+import org.jclouds.rest.annotations.ApiVersion;
+
+import com.google.common.base.Supplier;
+
+/**
+ * Properly supply the version of the Chef Server.
+ * 
+ * @author Ignasi Barrera
+ */
+@Singleton
+public class ChefVersionSupplier implements Supplier<Integer> {
+
+   /** The default version to assume in case we can not parse it. */
+   public static final Integer DEFAULT_VERSION = 10;
+
+   @Resource
+   @Named(ChefProperties.CHEF_LOGGER)
+   private Logger logger = Logger.NULL;
+
+   /** The configured version of the Chef Server API. */
+   private final String apiVersion;
+
+   @Inject
+   ChefVersionSupplier(@ApiVersion String apiVersion) {
+      this.apiVersion = checkNotNull(apiVersion, "apiVersion must not be null");
+   }
+
+   @Override
+   public Integer get() {
+      Pattern versionPattern = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)*");
+      Matcher m = versionPattern.matcher(apiVersion);
+      if (!m.matches()) {
+         logger.warn("Configured version does not match the standard version pattern. Assuming version %s",
+               DEFAULT_VERSION);
+         return DEFAULT_VERSION;
+      }
+
+      int major = Integer.parseInt(m.group(1));
+      int minor = Integer.parseInt(m.group(2));
+
+      // Old versions of Chef have versions like 0.9.x, 0.10.x, but newer versions
+      // are in the format 10.x.y, 11.x.y
+      return major == 0? minor : major;
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/35cb1834/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java b/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
new file mode 100644
index 0000000..d5efd90
--- /dev/null
+++ b/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.jclouds.chef.suppliers;
+
+import static org.jclouds.chef.suppliers.ChefVersionSupplier.DEFAULT_VERSION;
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+/**
+ * Unit tests for the {@link ChefVersionSupplier} class.
+ * 
+ * @author Ignasi Barrera
+ */
+@Test(groups = "unit", testName = "ChefVersionSupplierTest")
+public class ChefVersionSupplierTest {
+
+   public void testReturnsDefaultVersion() {
+      assertEquals(new ChefVersionSupplier("15").get(), DEFAULT_VERSION);
+   }
+
+   public void testReturnsMajorVersionIfNotZero() {
+      assertEquals(new ChefVersionSupplier("11.6").get().intValue(), 11);
+      assertEquals(new ChefVersionSupplier("11.6.0").get().intValue(), 11);
+      assertEquals(new ChefVersionSupplier("11.6.0.1").get().intValue(), 11);
+   }
+
+   public void testReturnsMinorVersionIfMajorIsZero() {
+      assertEquals(new ChefVersionSupplier("0.9").get().intValue(), 9);
+      assertEquals(new ChefVersionSupplier("0.9.8").get().intValue(), 9);
+      assertEquals(new ChefVersionSupplier("0.9.8.2").get().intValue(), 9);
+   }
+}


[3/3] git commit: Improved the regular expression to match the version number we want to get

Posted by na...@apache.org.
Improved the regular expression to match the version number we want to get


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

Branch: refs/heads/253-parseversion
Commit: fb8c7f1c38bc11cd7c758f5042244320ea9e60e1
Parents: 5565365
Author: Ignasi Barrera <na...@apache.org>
Authored: Thu Sep 12 21:54:01 2013 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Thu Sep 12 21:54:01 2013 +0200

----------------------------------------------------------------------
 .../jclouds/chef/suppliers/ChefVersionSupplier.java    | 13 ++++++-------
 .../chef/suppliers/ChefVersionSupplierTest.java        |  2 ++
 2 files changed, 8 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/fb8c7f1c/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java b/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
index 8d9e627..a08fc40 100644
--- a/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
+++ b/core/src/main/java/org/jclouds/chef/suppliers/ChefVersionSupplier.java
@@ -16,6 +16,7 @@
  */
 package org.jclouds.chef.suppliers;
 
+import static com.google.common.base.Objects.firstNonNull;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.util.regex.Matcher;
@@ -57,7 +58,10 @@ public class ChefVersionSupplier implements Supplier<Integer> {
 
    @Override
    public Integer get() {
-      Pattern versionPattern = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)*");
+      // Old versions of Chef have versions like 0.9.x, 0.10.x, but newer
+      // versions are in the format 10.x.y, 11.x.y
+      Pattern versionPattern = Pattern.compile("(?:0\\.(\\d+)|(\\d+)\\.\\d+)(?:\\.\\d)*");
+
       Matcher m = versionPattern.matcher(apiVersion);
       if (!m.matches()) {
          logger.warn("Configured version does not match the standard version pattern. Assuming version %s",
@@ -65,12 +69,7 @@ public class ChefVersionSupplier implements Supplier<Integer> {
          return DEFAULT_VERSION;
       }
 
-      int major = Integer.parseInt(m.group(1));
-      int minor = Integer.parseInt(m.group(2));
-
-      // Old versions of Chef have versions like 0.9.x, 0.10.x, but newer versions
-      // are in the format 10.x.y, 11.x.y
-      return major == 0? minor : major;
+      return Integer.valueOf(firstNonNull(m.group(1), m.group(2)));
    }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/fb8c7f1c/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java b/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
index d5efd90..7bde5ac 100644
--- a/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
+++ b/core/src/test/java/org/jclouds/chef/suppliers/ChefVersionSupplierTest.java
@@ -31,6 +31,8 @@ public class ChefVersionSupplierTest {
 
    public void testReturnsDefaultVersion() {
       assertEquals(new ChefVersionSupplier("15").get(), DEFAULT_VERSION);
+      assertEquals(new ChefVersionSupplier("0").get(), DEFAULT_VERSION);
+      assertEquals(new ChefVersionSupplier("0.").get(), DEFAULT_VERSION);
    }
 
    public void testReturnsMajorVersionIfNotZero() {


[2/3] git commit: Removed extra line break

Posted by na...@apache.org.
Removed extra line break


Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/commit/5565365d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/tree/5565365d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/diff/5565365d

Branch: refs/heads/253-parseversion
Commit: 5565365d124303b698bfe5284ad14741dbfe9fa2
Parents: 35cb183
Author: Ignasi Barrera <na...@apache.org>
Authored: Wed Sep 11 00:12:26 2013 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Wed Sep 11 00:12:26 2013 +0200

----------------------------------------------------------------------
 core/src/main/java/org/jclouds/chef/config/ChefParserModule.java | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/5565365d/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java b/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
index a1012c0..67dd35a 100644
--- a/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
+++ b/core/src/main/java/org/jclouds/chef/config/ChefParserModule.java
@@ -252,8 +252,7 @@ public class ChefParserModule extends AbstractModule {
             K name = keyAdapter.read(in);
             V value = valueAdapter.read(in);
             if (value != null) {
-               // If there are repeated keys, overwrite them to only keep the
-               // last one
+               // If there are repeated keys, overwrite them to only keep the last one
                result.put(name, value);
             }
          }