mirror of https://github.com/apache/jclouds.git
change cacheloader that can return nulls to return optional so to avoid cacheload exceptions
This commit is contained in:
parent
785f616a95
commit
6f2c21ec7b
|
@ -64,6 +64,7 @@ import org.jclouds.predicates.RetryablePredicate;
|
|||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
|
@ -117,7 +118,7 @@ public class EC2ComputeServiceDependenciesModule extends AbstractModule {
|
|||
bind(TemplateBuilder.class).to(EC2TemplateBuilderImpl.class);
|
||||
bind(TemplateOptions.class).to(EC2TemplateOptions.class);
|
||||
bind(ComputeService.class).to(EC2ComputeService.class);
|
||||
bind(new TypeLiteral<CacheLoader<RunningInstance, LoginCredentials>>() {
|
||||
bind(new TypeLiteral<CacheLoader<RunningInstance, Optional<LoginCredentials>>>() {
|
||||
}).to(CredentialsForInstance.class);
|
||||
bind(new TypeLiteral<Function<RegionAndName, KeyPair>>() {
|
||||
}).to(CreateUniqueKeyPair.class);
|
||||
|
@ -154,7 +155,7 @@ public class EC2ComputeServiceDependenciesModule extends AbstractModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected LoadingCache<RunningInstance, LoginCredentials> credentialsMap(CacheLoader<RunningInstance, LoginCredentials> in) {
|
||||
protected LoadingCache<RunningInstance, Optional<LoginCredentials>> credentialsMap(CacheLoader<RunningInstance, Optional<LoginCredentials>> in) {
|
||||
return CacheBuilder.newBuilder().build(in);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.jclouds.ec2.domain.RunningInstance;
|
|||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
|
@ -43,7 +44,7 @@ import com.google.common.cache.LoadingCache;
|
|||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class CredentialsForInstance extends CacheLoader<RunningInstance, LoginCredentials> {
|
||||
public class CredentialsForInstance extends CacheLoader<RunningInstance, Optional<LoginCredentials>> {
|
||||
|
||||
private final ConcurrentMap<RegionAndName, KeyPair> credentialsMap;
|
||||
private final Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap;
|
||||
|
@ -58,13 +59,13 @@ public class CredentialsForInstance extends CacheLoader<RunningInstance, LoginCr
|
|||
}
|
||||
|
||||
@Override
|
||||
public LoginCredentials load(final RunningInstance instance) throws ExecutionException {
|
||||
public Optional<LoginCredentials> load(final RunningInstance instance) throws ExecutionException {
|
||||
if ("windows".equals(instance.getPlatform())) {
|
||||
return passwordCredentialsFromWindowsInstance.apply(instance);
|
||||
return Optional.of(passwordCredentialsFromWindowsInstance.apply(instance));
|
||||
} else if (instance.getKeyName() != null) {
|
||||
return LoginCredentials.builder().user(getLoginAccountFor(instance)).privateKey(getPrivateKeyOrNull(instance)).build();
|
||||
return Optional.of(LoginCredentials.builder().user(getLoginAccountFor(instance)).privateKey(getPrivateKeyOrNull(instance)).build());
|
||||
}
|
||||
return null;
|
||||
return Optional.absent();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
|
|
@ -57,6 +57,7 @@ import org.jclouds.logging.Logger;
|
|||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
@ -94,7 +95,7 @@ public class EC2CreateNodesInGroupThenAddToSet implements CreateNodesInGroupThen
|
|||
@VisibleForTesting
|
||||
final ComputeUtils utils;
|
||||
final InstancePresent instancePresent;
|
||||
final LoadingCache<RunningInstance, LoginCredentials> instanceToCredentials;
|
||||
final LoadingCache<RunningInstance, Optional<LoginCredentials>> instanceToCredentials;
|
||||
final Map<String, Credentials> credentialStore;
|
||||
final Provider<TemplateBuilder> templateBuilderProvider;
|
||||
|
||||
|
@ -107,7 +108,7 @@ public class EC2CreateNodesInGroupThenAddToSet implements CreateNodesInGroupThen
|
|||
Provider<TemplateBuilder> templateBuilderProvider,
|
||||
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize,
|
||||
InstancePresent instancePresent, Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata,
|
||||
LoadingCache<RunningInstance, LoginCredentials> instanceToCredentials, Map<String, Credentials> credentialStore,
|
||||
LoadingCache<RunningInstance, Optional<LoginCredentials>> instanceToCredentials, Map<String, Credentials> credentialStore,
|
||||
ComputeUtils utils) {
|
||||
this.client = checkNotNull(client, "client");
|
||||
this.elasticIpCache = checkNotNull(elasticIpCache, "elasticIpCache");
|
||||
|
@ -160,7 +161,7 @@ public class EC2CreateNodesInGroupThenAddToSet implements CreateNodesInGroupThen
|
|||
protected void populateCredentials(Iterable<? extends RunningInstance> started, TemplateOptions options) {
|
||||
LoginCredentials credentials = null;
|
||||
for (RunningInstance instance : started) {
|
||||
credentials = instanceToCredentials.apply(instance);
|
||||
credentials = instanceToCredentials.apply(instance).orNull();
|
||||
if (credentials != null)
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ import org.jclouds.ec2.services.InstanceClient;
|
|||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
@ -118,7 +119,7 @@ public class EC2CreateNodesInGroupThenAddToSetTest {
|
|||
expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce();
|
||||
// simulate a lazy credentials fetch
|
||||
LoginCredentials creds = LoginCredentials.builder().user("foo").privateKey("bar").build();
|
||||
expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds);
|
||||
expect(strategy.instanceToCredentials.apply(instance)).andReturn(Optional.of(creds));
|
||||
expect(instance.getRegion()).andReturn(region).atLeastOnce();
|
||||
expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds)).andReturn(null);
|
||||
|
||||
|
@ -219,7 +220,7 @@ public class EC2CreateNodesInGroupThenAddToSetTest {
|
|||
expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce();
|
||||
// simulate a lazy credentials fetch
|
||||
LoginCredentials creds = LoginCredentials.builder().user("foo").privateKey("bar").build();
|
||||
expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds);
|
||||
expect(strategy.instanceToCredentials.apply(instance)).andReturn(Optional.of(creds));
|
||||
expect(instance.getRegion()).andReturn(region).atLeastOnce();
|
||||
expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds)).andReturn(null);
|
||||
|
||||
|
@ -320,7 +321,7 @@ public class EC2CreateNodesInGroupThenAddToSetTest {
|
|||
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize = createMock(CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class);
|
||||
InstancePresent instancePresent = createMock(InstancePresent.class);
|
||||
RunningInstanceToNodeMetadata runningInstanceToNodeMetadata = createMock(RunningInstanceToNodeMetadata.class);
|
||||
LoadingCache<RunningInstance, LoginCredentials> instanceToCredentials = createMock(LoadingCache.class);
|
||||
LoadingCache<RunningInstance, Optional<LoginCredentials>> instanceToCredentials = createMock(LoadingCache.class);
|
||||
LoadingCache<RegionAndName, String> elasticIpCache = createMock(LoadingCache.class);
|
||||
GetNodeMetadataStrategy nodeRunning = new GetNodeMetadataStrategy(){
|
||||
|
||||
|
|
|
@ -66,6 +66,7 @@ import org.jclouds.predicates.PredicateWithResult;
|
|||
import org.jclouds.predicates.RetryablePredicate;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
|
@ -89,7 +90,7 @@ public class AWSEC2ComputeServiceDependenciesModule extends EC2ComputeServiceDep
|
|||
bind(TemplateBuilder.class).to(EC2TemplateBuilderImpl.class);
|
||||
bind(TemplateOptions.class).to(AWSEC2TemplateOptions.class);
|
||||
bind(ComputeService.class).to(AWSEC2ComputeService.class);
|
||||
bind(new TypeLiteral<CacheLoader<RunningInstance, LoginCredentials>>() {
|
||||
bind(new TypeLiteral<CacheLoader<RunningInstance, Optional<LoginCredentials>>>() {
|
||||
}).to(CredentialsForInstance.class);
|
||||
bind(new TypeLiteral<CacheLoader<RegionAndName, String>>() {
|
||||
}).annotatedWith(Names.named("SECURITY")).to(CreateSecurityGroupIfNeeded.class);
|
||||
|
|
|
@ -56,6 +56,7 @@ import org.jclouds.logging.Logger;
|
|||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
@ -89,7 +90,7 @@ public class AWSEC2CreateNodesInGroupThenAddToSet extends EC2CreateNodesInGroupT
|
|||
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize,
|
||||
AWSEC2InstancePresent instancePresent,
|
||||
Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata,
|
||||
LoadingCache<RunningInstance, LoginCredentials> instanceToCredentials, Map<String, Credentials> credentialStore,
|
||||
LoadingCache<RunningInstance, Optional<LoginCredentials>> instanceToCredentials, Map<String, Credentials> credentialStore,
|
||||
ComputeUtils utils, SpotInstanceRequestToAWSRunningInstance spotConverter) {
|
||||
super(client, elasticIpCache, nodeRunning, templateBuilderProvider, createKeyPairAndSecurityGroupsAsNeededAndReturncustomize,
|
||||
instancePresent, runningInstanceToNodeMetadata, instanceToCredentials, credentialStore, utils);
|
||||
|
|
Loading…
Reference in New Issue