JCLOUDS-265: Add listEnvironmentNodes API in ChefService

This commit is contained in:
Noorul Islam K M 2013-09-07 12:17:35 +05:30 committed by Ignasi Barrera
parent 0853a45526
commit 2a8b1d3513
5 changed files with 247 additions and 1 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}