From 454ccda77aba26dae1edb1b80e7f25e1d835dbaa Mon Sep 17 00:00:00 2001 From: Noorul Islam K M Date: Sat, 7 Sep 2013 12:17:35 +0530 Subject: [PATCH] JCLOUDS-265: Add listEnvironmentNodes API in ChefService --- .../main/java/org/jclouds/chef/ChefService.java | 3 + .../org/jclouds/chef/internal/BaseChefService.java | 10 +- .../chef/strategy/ListEnvironmentNodes.java | 45 +++++++++ .../internal/ListEnvironmentNodesImpl.java | 109 +++++++++++++++++++++ .../internal/GetEnvironmentNodesImplLiveTest.java | 81 +++++++++++++++ 5 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/jclouds/chef/strategy/ListEnvironmentNodes.java create mode 100644 core/src/main/java/org/jclouds/chef/strategy/internal/ListEnvironmentNodesImpl.java create mode 100644 core/src/test/java/org/jclouds/chef/strategy/internal/GetEnvironmentNodesImplLiveTest.java diff --git a/core/src/main/java/org/jclouds/chef/ChefService.java b/core/src/main/java/org/jclouds/chef/ChefService.java index a1c95b5..293fb5b 100644 --- a/core/src/main/java/org/jclouds/chef/ChefService.java +++ b/core/src/main/java/org/jclouds/chef/ChefService.java @@ -177,4 +177,7 @@ @SinceApiVersion("0.10.0") Iterable listEnvironmentsNamed(Iterable names); + + @SinceApiVersion("0.10.0") + Iterable listEnvironmentNodes(String environmentName); } diff --git a/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java b/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java index 02b2443..cf3f879 100644 --- a/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java +++ b/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java @@ -50,6 +50,7 @@ import org.jclouds.chef.strategy.ListClients; import org.jclouds.chef.strategy.ListCookbookVersions; import org.jclouds.chef.strategy.ListEnvironments; +import org.jclouds.chef.strategy.ListEnvironmentNodes; import org.jclouds.chef.strategy.ListNodes; import org.jclouds.chef.strategy.UpdateAutomaticAttributesOnNode; import org.jclouds.domain.JsonBall; @@ -90,6 +91,7 @@ private final RunListForGroup runListForGroup; private final ListCookbookVersions listCookbookVersions; private final ListEnvironments listEnvironments; + private final ListEnvironmentNodes listEnvironmentNodes; private final Json json; @Resource @Named(ChefProperties.CHEF_LOGGER) @@ -104,7 +106,7 @@ protected BaseChefService(ChefContext chefContext, ChefApi api, UpdateAutomaticAttributesOnNode updateAutomaticAttributesOnNode, Supplier privateKey, @Named(CHEF_BOOTSTRAP_DATABAG) String databag, GroupToBootScript groupToBootScript, BootstrapConfigForGroup bootstrapConfigForGroup, RunListForGroup runListForGroup, - ListEnvironments listEnvironments, Json json) { + ListEnvironments listEnvironments, ListEnvironmentNodes listEnvironmentNodes, Json json) { this.chefContext = checkNotNull(chefContext, "chefContext"); this.api = checkNotNull(api, "api"); this.cleanupStaleNodesAndClients = checkNotNull(cleanupStaleNodesAndClients, "cleanupStaleNodesAndClients"); @@ -123,6 +125,7 @@ protected BaseChefService(ChefContext chefContext, ChefApi api, this.bootstrapConfigForGroup = checkNotNull(bootstrapConfigForGroup, "bootstrapConfigForGroup"); this.runListForGroup = checkNotNull(runListForGroup, "runListForGroup"); this.listEnvironments = checkNotNull(listEnvironments, "listEnvironments"); + this.listEnvironmentNodes = checkNotNull(listEnvironmentNodes, "listEnvironmentNodes"); this.json = checkNotNull(json, "json"); } @@ -294,4 +297,9 @@ String buildBootstrapConfiguration(BootstrapConfig bootstrapConfig) { return listEnvironments.execute(names); } + @Override + public Iterable listEnvironmentNodes(String environmentName) { + return listEnvironmentNodes.execute(environmentName); + } + } diff --git a/core/src/main/java/org/jclouds/chef/strategy/ListEnvironmentNodes.java b/core/src/main/java/org/jclouds/chef/strategy/ListEnvironmentNodes.java new file mode 100644 index 0000000..6414994 --- /dev/null +++ b/core/src/main/java/org/jclouds/chef/strategy/ListEnvironmentNodes.java @@ -0,0 +1,45 @@ +/* + * 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.strategy; + +import org.jclouds.chef.domain.Node; +import org.jclouds.chef.strategy.internal.ListEnvironmentNodesImpl; + +import com.google.common.base.Predicate; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.inject.ImplementedBy; + +/** + * + * + * @author Noorul Islam K M + */ +@ImplementedBy(ListEnvironmentNodesImpl.class) +public interface ListEnvironmentNodes { + + public Iterable execute(String environmentName); + + public Iterable execute(String environmentName, Predicate nodeNameSelector); + + public Iterable execute(String environmentName, Iterable toGet); + + public Iterable execute(ListeningExecutorService executor, String environmentName); + + public Iterable execute(ListeningExecutorService executor, String environmentName, Predicate nodeNameSelector); + + public Iterable execute(ListeningExecutorService executor, String environmentName, Iterable toGet); +} diff --git a/core/src/main/java/org/jclouds/chef/strategy/internal/ListEnvironmentNodesImpl.java b/core/src/main/java/org/jclouds/chef/strategy/internal/ListEnvironmentNodesImpl.java new file mode 100644 index 0000000..a70db5a --- /dev/null +++ b/core/src/main/java/org/jclouds/chef/strategy/internal/ListEnvironmentNodesImpl.java @@ -0,0 +1,109 @@ +/* + * 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.strategy.internal; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Iterables.transform; +import static com.google.common.util.concurrent.Futures.allAsList; +import static com.google.common.util.concurrent.Futures.getUnchecked; + +import java.util.List; +import java.util.concurrent.Callable; + +import javax.annotation.Resource; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.jclouds.Constants; +import org.jclouds.chef.ChefApi; +import org.jclouds.chef.config.ChefProperties; +import org.jclouds.chef.domain.Node; +import org.jclouds.chef.strategy.ListEnvironmentNodes; +import org.jclouds.logging.Logger; + +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.base.Predicate; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.inject.Inject; + +/** + * + * + * @author Noorul Islam K M + */ +@Singleton +public class ListEnvironmentNodesImpl implements ListEnvironmentNodes { + + protected final ChefApi api; + protected final ListeningExecutorService userExecutor; + @Resource + @Named(ChefProperties.CHEF_LOGGER) + protected Logger logger = Logger.NULL; + + @Inject + ListEnvironmentNodesImpl(@Named(Constants.PROPERTY_USER_THREADS) ListeningExecutorService userExecutor, ChefApi api) { + this.userExecutor = checkNotNull(userExecutor, "userExecuor"); + this.api = checkNotNull(api, "api"); + } + + @Override + public Iterable execute(String environmentName) { + return execute(userExecutor, environmentName); + } + + @Override + public Iterable execute(String environmentName, Predicate nodeNameSelector) { + return execute(userExecutor, environmentName, nodeNameSelector); + } + + @Override + public Iterable execute(String environmentName, Iterable toGet) { + return execute(userExecutor, environmentName, toGet); + } + + @Override + public Iterable execute(ListeningExecutorService executor, String environmentName) { + return execute(executor, environmentName, api.listEnvironmentNodes(environmentName)); + } + + @Override + public Iterable execute(ListeningExecutorService executor, String environmentName, Predicate nodeNameSelector) { + return execute(executor, environmentName, filter(api.listEnvironmentNodes(environmentName), nodeNameSelector)); + } + + @Override + public Iterable execute(final ListeningExecutorService executor, String environmentName, Iterable toGet) { + ListenableFuture> futures = allAsList(transform(toGet, new Function>() { + @Override + public ListenableFuture apply(final String input) { + return executor.submit(new Callable() { + @Override + public Node call() throws Exception { + return api.getNode(input); + } + }); + } + })); + + logger.trace(String.format("getting nodes in environment %s: %s", environmentName, Joiner.on(',').join(toGet))); + return getUnchecked(futures); + } + +} diff --git a/core/src/test/java/org/jclouds/chef/strategy/internal/GetEnvironmentNodesImplLiveTest.java b/core/src/test/java/org/jclouds/chef/strategy/internal/GetEnvironmentNodesImplLiveTest.java new file mode 100644 index 0000000..706f275 --- /dev/null +++ b/core/src/test/java/org/jclouds/chef/strategy/internal/GetEnvironmentNodesImplLiveTest.java @@ -0,0 +1,81 @@ +/* + * 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.strategy.internal; + +import static com.google.common.collect.Iterables.size; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import org.jclouds.chef.ChefApi; +import org.jclouds.chef.internal.BaseChefLiveTest; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableSet; + +/** + * Tests behavior of {@code GetEnvironmentNodesImpl} strategies + * + * @author Noorul Islam K M + */ +@Test(groups = "live", testName = "GetEnvironmentNodesImplLiveTest") +public class GetEnvironmentNodesImplLiveTest extends BaseChefLiveTest { + + private ListEnvironmentNodesImpl strategy; + private CreateNodeAndPopulateAutomaticAttributesImpl creator; + + @Override + protected void initialize() { + super.initialize(); + this.creator = injector.getInstance(CreateNodeAndPopulateAutomaticAttributesImpl.class); + this.strategy = injector.getInstance(ListEnvironmentNodesImpl.class); + creator.execute(prefix, ImmutableSet. of()); + creator.execute(prefix + 1, ImmutableSet. of()); + } + + @AfterClass(groups = { "integration", "live" }) + @Override + protected void tearDown() { + api.deleteNode(prefix); + api.deleteNode(prefix + 1); + super.tearDown(); + } + + @Test + public void testExecute() { + assertTrue(size(strategy.execute("_default")) > 0, "Expected one or more elements"); + } + + @Test + public void testExecutePredicateOfString() { + assertEquals(size(strategy.execute("_default", new Predicate() { + + @Override + public boolean apply(String input) { + return input.startsWith(prefix); + } + + })), 2); + } + + @Test + public void testExecuteIterableOfString() { + assertEquals(size(strategy.execute("_default", ImmutableSet.of(prefix, prefix + 1))), 2); + } + +} -- 1.8.4