From a168683851f57d2c47667038ea8b1d5021839738 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 26 Jul 2010 23:03:22 -0700 Subject: [PATCH] Issue 191: added helper classes that use the async client --- .../DeleteAllClientsAndNodesInList.java | 81 +++++++++++++ .../chef/strategy/GetAllNodesInList.java | 106 ++++++++++++++++++ .../org/jclouds/chef/ChefClientLiveTest.java | 4 + 3 files changed, 191 insertions(+) create mode 100644 chef/src/main/java/org/jclouds/chef/strategy/DeleteAllClientsAndNodesInList.java create mode 100644 chef/src/main/java/org/jclouds/chef/strategy/GetAllNodesInList.java diff --git a/chef/src/main/java/org/jclouds/chef/strategy/DeleteAllClientsAndNodesInList.java b/chef/src/main/java/org/jclouds/chef/strategy/DeleteAllClientsAndNodesInList.java new file mode 100644 index 0000000000..80b50212a8 --- /dev/null +++ b/chef/src/main/java/org/jclouds/chef/strategy/DeleteAllClientsAndNodesInList.java @@ -0,0 +1,81 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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 static com.google.common.collect.Maps.newHashMap; +import static org.jclouds.concurrent.ConcurrentUtils.awaitCompletion; + +import java.util.Map; +import java.util.concurrent.ExecutorService; + +import javax.annotation.Resource; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.jclouds.Constants; +import org.jclouds.chef.ChefAsyncClient; +import org.jclouds.chef.ChefClient; +import org.jclouds.logging.Logger; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.inject.Inject; + +/** + * + * + * @author Adrian Cole + */ +@Singleton +public class DeleteAllClientsAndNodesInList { + + protected final ChefClient chefClient; + protected final ChefAsyncClient chefAsyncClient; + protected final ExecutorService userExecutor; + @Resource + protected Logger logger = Logger.NULL; + + @Inject(optional = true) + @Named(Constants.PROPERTY_REQUEST_TIMEOUT) + protected Long maxTime; + + @Inject + DeleteAllClientsAndNodesInList(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, + ChefClient getAllNode, ChefAsyncClient ablobstore) { + this.userExecutor = userExecutor; + this.chefAsyncClient = ablobstore; + this.chefClient = getAllNode; + } + + public void execute() { + execute(chefClient.listNodes()); + } + + public void execute(Iterable names) { + Map exceptions = newHashMap(); + Map> responses = newHashMap(); + for (String name : names) { + responses.put(name, chefAsyncClient.deleteClient(name)); + responses.put(name, chefAsyncClient.deleteNode(name)); + } + exceptions = awaitCompletion(responses, userExecutor, maxTime, logger, String.format( + "getting deleting clients and nodes: %s", names)); + if (exceptions.size() > 0) + throw new RuntimeException(String.format("errors deleting clients and nodes: %s: %s", names, exceptions)); + } +} \ No newline at end of file diff --git a/chef/src/main/java/org/jclouds/chef/strategy/GetAllNodesInList.java b/chef/src/main/java/org/jclouds/chef/strategy/GetAllNodesInList.java new file mode 100644 index 0000000000..73b0093a34 --- /dev/null +++ b/chef/src/main/java/org/jclouds/chef/strategy/GetAllNodesInList.java @@ -0,0 +1,106 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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 static com.google.common.base.Throwables.propagate; +import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Maps.newHashMap; +import static com.google.common.collect.Sets.newHashSet; +import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; +import static org.jclouds.concurrent.ConcurrentUtils.awaitCompletion; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; + +import javax.annotation.Resource; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.jclouds.Constants; +import org.jclouds.chef.ChefAsyncClient; +import org.jclouds.chef.ChefClient; +import org.jclouds.chef.domain.Node; +import org.jclouds.logging.Logger; + +import com.google.common.base.Predicate; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.inject.Inject; + +/** + * + * + * @author Adrian Cole + */ +@Singleton +public class GetAllNodesInList { + + protected final ChefClient chefClient; + protected final ChefAsyncClient chefAsyncClient; + protected final ExecutorService userExecutor; + @Resource + protected Logger logger = Logger.NULL; + + @Inject(optional = true) + @Named(Constants.PROPERTY_REQUEST_TIMEOUT) + protected Long maxTime; + + @Inject + GetAllNodesInList(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor, ChefClient getAllNode, + ChefAsyncClient ablobstore) { + this.userExecutor = userExecutor; + this.chefAsyncClient = ablobstore; + this.chefClient = getAllNode; + } + + public Set execute() { + return execute(chefClient.listNodes()); + } + + public Set execute(Predicate nodeNameSelector) { + return execute(filter(chefClient.listNodes(), nodeNameSelector)); + } + + public Set execute(Iterable toGet) { + Map exceptions = newHashMap(); + final Set nodes = newHashSet(); + Map> responses = newHashMap(); + for (String nodeName : toGet) { + final ListenableFuture future = chefAsyncClient.getNode(nodeName); + future.addListener(new Runnable() { + @Override + public void run() { + try { + nodes.add(future.get()); + } catch (InterruptedException e) { + propagate(e); + } catch (ExecutionException e) { + propagate(e); + } + } + }, sameThreadExecutor()); + responses.put(nodeName, future); + } + exceptions = awaitCompletion(responses, userExecutor, maxTime, logger, String.format("getting nodes: %s", toGet)); + if (exceptions.size() > 0) + throw new RuntimeException(String.format("errors getting nodes: %s: %s", toGet, exceptions)); + return nodes; + } +} \ No newline at end of file diff --git a/chef/src/test/java/org/jclouds/chef/ChefClientLiveTest.java b/chef/src/test/java/org/jclouds/chef/ChefClientLiveTest.java index 9d9e9d53f6..494d76e6d3 100644 --- a/chef/src/test/java/org/jclouds/chef/ChefClientLiveTest.java +++ b/chef/src/test/java/org/jclouds/chef/ChefClientLiveTest.java @@ -210,6 +210,8 @@ public class ChefClientLiveTest { System.out.println(clientKey); clientConnection = createConnection(PREFIX, clientKey); clientConnection.getApi().clientExists(PREFIX); + Set clients = adminConnection.getApi().listClients(); + assert clients.contains(PREFIX) : String.format("client %s not in %s", PREFIX, clients); } @Test(dependsOnMethods = "testCreateClient") @@ -229,6 +231,8 @@ public class ChefClientLiveTest { node = clientConnection.getApi().createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]"))); // TODO check recipes assertNotNull(node); + Set nodes = adminConnection.getApi().listNodes(); + assert nodes.contains(PREFIX) : String.format("node %s not in %s", PREFIX, nodes); } @Test(dependsOnMethods = "testCreateNode")