From 7a81769e1c06a433bdd38a0c3bec593b6b1ca173 Mon Sep 17 00:00:00 2001 From: vijaykiran Date: Fri, 3 Aug 2012 01:14:09 +0200 Subject: [PATCH] Issue-1051 overrideLoginUser not working --- .../EC2ComputeServiceDependenciesModule.java | 7 ++--- .../functions/CredentialsForInstance.java | 9 +++--- .../EC2CreateNodesInGroupThenAddToSet.java | 18 ++++++----- .../compute/EC2ComputeServiceExpectTest.java | 30 +++++++++++++++++-- ...EC2CreateNodesInGroupThenAddToSetTest.java | 18 ++++++++--- apis/ec2/src/test/resources/new_instance.xml | 4 +-- ...WSEC2ComputeServiceDependenciesModule.java | 3 +- .../AWSEC2CreateNodesInGroupThenAddToSet.java | 5 ++-- 8 files changed, 63 insertions(+), 31 deletions(-) diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java index 2ff35cc12b..462ecfc3a1 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java @@ -20,7 +20,6 @@ package org.jclouds.ec2.compute.config; import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT; -import java.security.SecureRandom; import java.util.Map; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; @@ -35,7 +34,6 @@ import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.extensions.ImageExtension; import org.jclouds.compute.options.TemplateOptions; -import org.jclouds.domain.Credentials; import org.jclouds.domain.LoginCredentials; import org.jclouds.ec2.compute.EC2ComputeService; import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey; @@ -67,7 +65,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Functions; import com.google.common.base.Predicate; -import com.google.common.base.Supplier; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -120,7 +117,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>() { + bind(new TypeLiteral>() { }).to(CredentialsForInstance.class); bind(new TypeLiteral>() { }).to(CreateUniqueKeyPair.class); @@ -157,7 +154,7 @@ public class EC2ComputeServiceDependenciesModule extends AbstractModule { @Provides @Singleton - protected LoadingCache credentialsMap(CacheLoader in) { + protected LoadingCache credentialsMap(CacheLoader in) { return CacheBuilder.newBuilder().build(in); } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/CredentialsForInstance.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/CredentialsForInstance.java index 90564ee392..e80504bf8b 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/CredentialsForInstance.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/CredentialsForInstance.java @@ -27,7 +27,6 @@ import javax.inject.Inject; import javax.inject.Singleton; import org.jclouds.compute.domain.Image; -import org.jclouds.domain.Credentials; import org.jclouds.domain.LoginCredentials; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.domain.KeyPair; @@ -44,7 +43,7 @@ import com.google.common.cache.LoadingCache; * @author Adrian Cole */ @Singleton -public class CredentialsForInstance extends CacheLoader { +public class CredentialsForInstance extends CacheLoader { private final ConcurrentMap credentialsMap; private final Supplier> imageMap; @@ -59,11 +58,11 @@ public class CredentialsForInstance extends CacheLoader instanceToCredentials; + final LoadingCache instanceToCredentials; final Map credentialStore; final Provider templateBuilderProvider; - - @Inject protected EC2CreateNodesInGroupThenAddToSet( EC2Client client, @@ -106,7 +107,7 @@ public class EC2CreateNodesInGroupThenAddToSet implements CreateNodesInGroupThen Provider templateBuilderProvider, CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, InstancePresent instancePresent, Function runningInstanceToNodeMetadata, - LoadingCache instanceToCredentials, Map credentialStore, + LoadingCache instanceToCredentials, Map credentialStore, ComputeUtils utils) { this.client = checkNotNull(client, "client"); this.elasticIpCache = checkNotNull(elasticIpCache, "elasticIpCache"); @@ -147,7 +148,7 @@ public class EC2CreateNodesInGroupThenAddToSet implements CreateNodesInGroupThen logger.debug("<< started instances(%s)", idsString); all(ids, instancePresent); logger.debug("<< present instances(%s)", idsString); - populateCredentials(started); + populateCredentials(started, template.getOptions()); } assignElasticIpsToInstances(ips, started); @@ -156,13 +157,14 @@ public class EC2CreateNodesInGroupThenAddToSet implements CreateNodesInGroupThen runningInstanceToNodeMetadata), goodNodes, badNodes, customizationResponses); } - protected void populateCredentials(Iterable started) { - Credentials credentials = null; + protected void populateCredentials(Iterable started, TemplateOptions options) { + LoginCredentials credentials = null; for (RunningInstance instance : started) { credentials = instanceToCredentials.apply(instance); if (credentials != null) break; } + credentials = overrideDefaultCredentialsWithOptionsIfPresent(credentials, options); if (credentials != null) for (RunningInstance instance : started) credentialStore.put("node#" + instance.getRegion() + "/" + instance.getId(), credentials); diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/EC2ComputeServiceExpectTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/EC2ComputeServiceExpectTest.java index 43e72afedd..1802e0d76f 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/EC2ComputeServiceExpectTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/EC2ComputeServiceExpectTest.java @@ -207,8 +207,6 @@ public class EC2ComputeServiceExpectTest extends BaseEC2ComputeServiceExpectTest .addFormParam("Action", "DescribeImages").build()); } - //TODO: FIXME!! - @Test(enabled = false) public void testCreateNodeWithGeneratedKeyPairAndOverriddenLoginUser() throws Exception { Builder requestResponseMap = ImmutableMap. builder(); requestResponseMap.put(describeRegionsRequest, describeRegionsResponse); @@ -228,8 +226,34 @@ public class EC2ComputeServiceExpectTest extends BaseEC2ComputeServiceExpectTest NodeMetadata node = Iterables.getOnlyElement(apiThatCreatesNode.createNodesInGroup("test", 1, blockUntilRunning(false).overrideLoginUser("ec2-user"))); assertEquals(node.getCredentials().getUser(), "ec2-user"); + System.out.println(node.getImageId()); assertNotNull(node.getCredentials().getPrivateKey()); } - + + //FIXME - issue-1051 + @Test(enabled = false) + public void testCreateNodeWithGeneratedKeyPairAndOverriddenLoginUserWithTemplateBuilder() throws Exception { + Builder requestResponseMap = ImmutableMap. builder(); + requestResponseMap.put(describeRegionsRequest, describeRegionsResponse); + requestResponseMap.put(describeAvailabilityZonesRequest, describeAvailabilityZonesResponse); + requestResponseMap.put(describeImagesRequest, describeImagesResponse); + requestResponseMap.put(createKeyPairRequest, createKeyPairResponse); + requestResponseMap.put(createSecurityGroupRequest, createSecurityGroupResponse); + requestResponseMap.put(describeSecurityGroupRequest, describeSecurityGroupResponse); + requestResponseMap.put(authorizeSecurityGroupIngressRequest22, authorizeSecurityGroupIngressResponse); + requestResponseMap.put(authorizeSecurityGroupIngressRequestGroup, authorizeSecurityGroupIngressResponse); + requestResponseMap.put(runInstancesRequest, runInstancesResponse); + requestResponseMap.put(describeInstanceRequest, describeInstanceResponse); + requestResponseMap.put(describeImageRequest, describeImagesResponse); + + ComputeService apiThatCreatesNode = requestsSendResponses(requestResponseMap.build()); + + NodeMetadata node = Iterables.getOnlyElement( + apiThatCreatesNode.createNodesInGroup("test", 1, + apiThatCreatesNode.templateBuilder().from("osDescriptionMatches=.*fedora.*,loginUser=ec2-user").build())); + assertEquals(node.getCredentials().getUser(), "ec2-user"); + assertNotNull(node.getCredentials().getPrivateKey()); + } + } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/EC2CreateNodesInGroupThenAddToSetTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/EC2CreateNodesInGroupThenAddToSetTest.java index c985dd429d..7c2c023b6a 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/EC2CreateNodesInGroupThenAddToSetTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/EC2CreateNodesInGroupThenAddToSetTest.java @@ -33,10 +33,10 @@ import org.jclouds.compute.config.CustomizationResponse; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.TemplateBuilder; -import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.predicates.AtomicNodeRunning; import org.jclouds.compute.strategy.GetNodeMetadataStrategy; import org.jclouds.compute.util.ComputeUtils; @@ -44,6 +44,7 @@ import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationScope; +import org.jclouds.domain.LoginCredentials; import org.jclouds.ec2.EC2Client; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; @@ -116,13 +117,17 @@ public class EC2CreateNodesInGroupThenAddToSetTest { Reservation.class.cast(reservation)); expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce(); // simulate a lazy credentials fetch - Credentials creds = new Credentials("foo", "bar"); + LoginCredentials creds = LoginCredentials.builder().user("foo").privateKey("bar").build(); expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds); expect(instance.getRegion()).andReturn(region).atLeastOnce(); expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds)).andReturn(null); expect(strategy.instancePresent.apply(new RegionAndName(region, instanceCreatedId))).andReturn(true); expect(input.template.getOptions()).andReturn(input.options).atLeastOnce(); + expect(input.options.getLoginUser()).andReturn(null); + expect(input.options.getLoginPassword()).andReturn(null); + expect(input.options.getLoginPrivateKey()).andReturn(null); + expect(input.options.shouldAuthenticateSudo()).andReturn(null); expect( strategy.utils.customizeNodesAndAddToGoodMapOrPutExceptionIntoBadMap(eq(input.options), @@ -213,13 +218,18 @@ public class EC2CreateNodesInGroupThenAddToSetTest { Reservation.class.cast(reservation)); expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce(); // simulate a lazy credentials fetch - Credentials creds = new Credentials("foo", "bar"); + LoginCredentials creds = LoginCredentials.builder().user("foo").privateKey("bar").build(); expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds); expect(instance.getRegion()).andReturn(region).atLeastOnce(); expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds)).andReturn(null); expect(strategy.instancePresent.apply(new RegionAndName(region, instanceCreatedId))).andReturn(true); expect(input.template.getOptions()).andReturn(input.options).atLeastOnce(); + expect(input.options.getLoginUser()).andReturn(null); + expect(input.options.getLoginPassword()).andReturn(null); + expect(input.options.getLoginPrivateKey()).andReturn(null); + expect(input.options.shouldAuthenticateSudo()).andReturn(null); + expect(strategy.runningInstanceToNodeMetadata.apply(instance)).andReturn(nodeMetadata); expect( @@ -310,7 +320,7 @@ public class EC2CreateNodesInGroupThenAddToSetTest { CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize = createMock(CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class); InstancePresent instancePresent = createMock(InstancePresent.class); RunningInstanceToNodeMetadata runningInstanceToNodeMetadata = createMock(RunningInstanceToNodeMetadata.class); - LoadingCache instanceToCredentials = createMock(LoadingCache.class); + LoadingCache instanceToCredentials = createMock(LoadingCache.class); LoadingCache elasticIpCache = createMock(LoadingCache.class); GetNodeMetadataStrategy nodeRunning = new GetNodeMetadataStrategy(){ diff --git a/apis/ec2/src/test/resources/new_instance.xml b/apis/ec2/src/test/resources/new_instance.xml index e11929cf0a..17a7b5fd33 100644 --- a/apis/ec2/src/test/resources/new_instance.xml +++ b/apis/ec2/src/test/resources/new_instance.xml @@ -20,7 +20,7 @@ - jclouds#test#1 + jclouds#test#0 0 t1.micro @@ -53,4 +53,4 @@ xen - \ No newline at end of file + diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceDependenciesModule.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceDependenciesModule.java index 4cdd8b344f..5a46d6c571 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceDependenciesModule.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceDependenciesModule.java @@ -45,7 +45,6 @@ import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.extensions.ImageExtension; import org.jclouds.compute.options.TemplateOptions; import org.jclouds.config.ValueOfConfigurationKeyOrNull; -import org.jclouds.domain.Credentials; import org.jclouds.domain.LoginCredentials; import org.jclouds.ec2.compute.config.EC2ComputeServiceDependenciesModule; import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey; @@ -90,7 +89,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>() { + bind(new TypeLiteral>() { }).to(CredentialsForInstance.class); bind(new TypeLiteral>() { }).annotatedWith(Names.named("SECURITY")).to(CreateSecurityGroupIfNeeded.class); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2CreateNodesInGroupThenAddToSet.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2CreateNodesInGroupThenAddToSet.java index 06d7fcf58d..e2939a316c 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2CreateNodesInGroupThenAddToSet.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2CreateNodesInGroupThenAddToSet.java @@ -47,6 +47,7 @@ import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.util.ComputeUtils; import org.jclouds.domain.Credentials; +import org.jclouds.domain.LoginCredentials; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.compute.strategy.EC2CreateNodesInGroupThenAddToSet; import org.jclouds.ec2.domain.RunningInstance; @@ -88,7 +89,7 @@ public class AWSEC2CreateNodesInGroupThenAddToSet extends EC2CreateNodesInGroupT CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, AWSEC2InstancePresent instancePresent, Function runningInstanceToNodeMetadata, - LoadingCache instanceToCredentials, Map credentialStore, + LoadingCache instanceToCredentials, Map credentialStore, ComputeUtils utils, SpotInstanceRequestToAWSRunningInstance spotConverter) { super(client, elasticIpCache, nodeRunning, templateBuilderProvider, createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, instancePresent, runningInstanceToNodeMetadata, instanceToCredentials, credentialStore, utils); @@ -146,4 +147,4 @@ public class AWSEC2CreateNodesInGroupThenAddToSet extends EC2CreateNodesInGroupT return options instanceof AWSEC2TemplateOptions ? AWSEC2TemplateOptions.class.cast(options).getSpotPrice() : null; } -} \ No newline at end of file +}