Issue 191: added helper classes that use the async client

This commit is contained in:
Adrian Cole 2010-07-26 23:03:22 -07:00
parent 360343020a
commit a168683851
3 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,81 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<String> names) {
Map<String, Exception> exceptions = newHashMap();
Map<String, ListenableFuture<?>> 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));
}
}

View File

@ -0,0 +1,106 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<Node> execute() {
return execute(chefClient.listNodes());
}
public Set<Node> execute(Predicate<String> nodeNameSelector) {
return execute(filter(chefClient.listNodes(), nodeNameSelector));
}
public Set<Node> execute(Iterable<String> toGet) {
Map<String, Exception> exceptions = newHashMap();
final Set<Node> nodes = newHashSet();
Map<String, ListenableFuture<?>> responses = newHashMap();
for (String nodeName : toGet) {
final ListenableFuture<? extends Node> 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;
}
}

View File

@ -210,6 +210,8 @@ public class ChefClientLiveTest {
System.out.println(clientKey); System.out.println(clientKey);
clientConnection = createConnection(PREFIX, clientKey); clientConnection = createConnection(PREFIX, clientKey);
clientConnection.getApi().clientExists(PREFIX); clientConnection.getApi().clientExists(PREFIX);
Set<String> clients = adminConnection.getApi().listClients();
assert clients.contains(PREFIX) : String.format("client %s not in %s", PREFIX, clients);
} }
@Test(dependsOnMethods = "testCreateClient") @Test(dependsOnMethods = "testCreateClient")
@ -229,6 +231,8 @@ public class ChefClientLiveTest {
node = clientConnection.getApi().createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]"))); node = clientConnection.getApi().createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]")));
// TODO check recipes // TODO check recipes
assertNotNull(node); assertNotNull(node);
Set<String> nodes = adminConnection.getApi().listNodes();
assert nodes.contains(PREFIX) : String.format("node %s not in %s", PREFIX, nodes);
} }
@Test(dependsOnMethods = "testCreateNode") @Test(dependsOnMethods = "testCreateNode")