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 <[email protected]> Authored: Tue Sep 10 13:26:22 2013 +0200 Committer: Ignasi Barrera <[email protected]> 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); + } +}
