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/10 13:30:18 UTC
git commit: JCLOUDS-265: Add listEnvironmentNodes API in ChefService
Updated Branches:
refs/heads/master 476b957a0 -> 5733d694f
JCLOUDS-265: Add listEnvironmentNodes API in ChefService
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/5733d694
Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/tree/5733d694
Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/diff/5733d694
Branch: refs/heads/master
Commit: 5733d694f3f111033ab2a2e77ef2fb040823f5df
Parents: 476b957
Author: Noorul Islam K M <no...@noorul.com>
Authored: Sat Sep 7 12:17:35 2013 +0530
Committer: Ignasi Barrera <na...@apache.org>
Committed: Tue Sep 10 13:27:16 2013 +0200
----------------------------------------------------------------------
.../main/java/org/jclouds/chef/ChefService.java | 3 +
.../jclouds/chef/internal/BaseChefService.java | 10 +-
.../chef/strategy/ListEnvironmentNodes.java | 45 ++++++++
.../internal/ListEnvironmentNodesImpl.java | 109 +++++++++++++++++++
.../GetEnvironmentNodesImplLiveTest.java | 81 ++++++++++++++
5 files changed, 247 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/5733d694/core/src/main/java/org/jclouds/chef/ChefService.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 @@ public interface ChefService {
@SinceApiVersion("0.10.0")
Iterable<? extends Environment> listEnvironmentsNamed(Iterable<String> names);
+
+ @SinceApiVersion("0.10.0")
+ Iterable<? extends Node> listEnvironmentNodes(String environmentName);
}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/5733d694/core/src/main/java/org/jclouds/chef/internal/BaseChefService.java
----------------------------------------------------------------------
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.DeleteAllNodesInList;
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 @@ public class BaseChefService implements ChefService {
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 @@ public class BaseChefService implements ChefService {
UpdateAutomaticAttributesOnNode updateAutomaticAttributesOnNode, Supplier<PrivateKey> 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 @@ public class BaseChefService implements ChefService {
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 @@ public class BaseChefService implements ChefService {
return listEnvironments.execute(names);
}
+ @Override
+ public Iterable<? extends Node> listEnvironmentNodes(String environmentName) {
+ return listEnvironmentNodes.execute(environmentName);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/5733d694/core/src/main/java/org/jclouds/chef/strategy/ListEnvironmentNodes.java
----------------------------------------------------------------------
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<? extends Node> execute(String environmentName);
+
+ public Iterable<? extends Node> execute(String environmentName, Predicate<String> nodeNameSelector);
+
+ public Iterable<? extends Node> execute(String environmentName, Iterable<String> toGet);
+
+ public Iterable<? extends Node> execute(ListeningExecutorService executor, String environmentName);
+
+ public Iterable<? extends Node> execute(ListeningExecutorService executor, String environmentName, Predicate<String> nodeNameSelector);
+
+ public Iterable<? extends Node> execute(ListeningExecutorService executor, String environmentName, Iterable<String> toGet);
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/5733d694/core/src/main/java/org/jclouds/chef/strategy/internal/ListEnvironmentNodesImpl.java
----------------------------------------------------------------------
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<? extends Node> execute(String environmentName) {
+ return execute(userExecutor, environmentName);
+ }
+
+ @Override
+ public Iterable<? extends Node> execute(String environmentName, Predicate<String> nodeNameSelector) {
+ return execute(userExecutor, environmentName, nodeNameSelector);
+ }
+
+ @Override
+ public Iterable<? extends Node> execute(String environmentName, Iterable<String> toGet) {
+ return execute(userExecutor, environmentName, toGet);
+ }
+
+ @Override
+ public Iterable<? extends Node> execute(ListeningExecutorService executor, String environmentName) {
+ return execute(executor, environmentName, api.listEnvironmentNodes(environmentName));
+ }
+
+ @Override
+ public Iterable<? extends Node> execute(ListeningExecutorService executor, String environmentName, Predicate<String> nodeNameSelector) {
+ return execute(executor, environmentName, filter(api.listEnvironmentNodes(environmentName), nodeNameSelector));
+ }
+
+ @Override
+ public Iterable<? extends Node> execute(final ListeningExecutorService executor, String environmentName, Iterable<String> toGet) {
+ ListenableFuture<List<Node>> futures = allAsList(transform(toGet, new Function<String, ListenableFuture<Node>>() {
+ @Override
+ public ListenableFuture<Node> apply(final String input) {
+ return executor.submit(new Callable<Node>() {
+ @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);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-chef/blob/5733d694/core/src/test/java/org/jclouds/chef/strategy/internal/GetEnvironmentNodesImplLiveTest.java
----------------------------------------------------------------------
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<ChefApi> {
+
+ 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.<String> of());
+ creator.execute(prefix + 1, ImmutableSet.<String> 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<String>() {
+
+ @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);
+ }
+
+}