JCLOUDS-467. Properly iterate over node names for EC2 instance creation.

This commit is contained in:
Andrew Bayer 2014-02-14 14:23:30 -08:00
parent a0ad055e63
commit a656c7292f
3 changed files with 197 additions and 34 deletions

View File

@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.compute.config.ComputeServiceProperties.RESOURCENAME_DELIMITER;
@ -32,16 +33,33 @@ import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_GENERATE_INSTA
import static org.jclouds.ec2.util.Tags.resourceToTagsAsMap;
import static org.jclouds.util.Predicates2.retry;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
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.base.Supplier;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.inject.Inject;
import org.jclouds.Constants;
import org.jclouds.aws.util.AWSUtils;
import org.jclouds.collect.Memoized;
@ -85,23 +103,6 @@ import org.jclouds.ec2.util.TagFilterBuilder;
import org.jclouds.scriptbuilder.functions.InitAdminAccess;
import org.jclouds.util.Strings2;
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.base.Supplier;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.inject.Inject;
/**
* @author Adrian Cole
*/
@ -175,12 +176,13 @@ public class EC2ComputeService extends BaseComputeService {
Map<String, ? extends NodeMetadata> instancesById = Maps.uniqueIndex(input, instanceId);
ImmutableSet.Builder<NodeMetadata> builder = ImmutableSet.<NodeMetadata> builder();
List<String> namesToUse = newArrayList(nodeNames);
if (generateInstanceNames && !common.containsKey("Name")) {
for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) {
String id = entry.getKey();
String name;
if (!nodeNames.isEmpty()) {
name = Iterables.get(nodeNames, 0);
if (!namesToUse.isEmpty()) {
name = namesToUse.remove(0);
} else {
name = id.replaceAll(".*-", group + "-");
}

View File

@ -22,6 +22,7 @@ import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import javax.ws.rs.core.MediaType;
import java.util.Set;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
@ -190,8 +191,8 @@ public class EC2ComputeServiceExpectTest extends BaseEC2ComputeServiceExpectTest
assertEquals(node.getCredentials().getUser(), "ec2-user", "User should be ec2-user");
}
public void testCreateNodeWithSpecifiedName() throws Exception {
HttpRequest createNamedTagsRequest =
public void testCreateThreeNodesWithSpecifiedName() throws Exception {
HttpRequest createFirstNamedTagRequest =
formSigner.filter(HttpRequest.builder()
.method("POST")
.endpoint("https://ec2.us-east-1.amazonaws.com/")
@ -199,7 +200,7 @@ public class EC2ComputeServiceExpectTest extends BaseEC2ComputeServiceExpectTest
.payload(
payloadFromStringWithContentType(
"Action=CreateTags" +
"&ResourceId.1=i-2baa5550" +
"&ResourceId.1=i-2ba64342" +
"&Signature=Trp5e5%2BMqeBeBZbLYa9s9gxahQ9nkx6ETfsGl82IV8Y%3D" +
"&SignatureMethod=HmacSHA256" +
"&SignatureVersion=2" +
@ -211,6 +212,46 @@ public class EC2ComputeServiceExpectTest extends BaseEC2ComputeServiceExpectTest
"application/x-www-form-urlencoded"))
.build());
HttpRequest createSecondNamedTagRequest =
formSigner.filter(HttpRequest.builder()
.method("POST")
.endpoint("https://ec2.us-east-1.amazonaws.com/")
.addHeader("Host", "ec2.us-east-1.amazonaws.com")
.payload(
payloadFromStringWithContentType(
"Action=CreateTags" +
"&ResourceId.1=i-2bc64242" +
"&Signature=Trp5e5%2BMqeBeBZbLYa9s9gxahQ9nkx6ETfsGl82IV8Y%3D" +
"&SignatureMethod=HmacSHA256" +
"&SignatureVersion=2" +
"&Tag.1.Key=Name" +
"&Tag.1.Value=second-node" +
"&Timestamp=2012-04-16T15%3A54%3A08.897Z" +
"&Version=2010-08-31" +
"&AWSAccessKeyId=identity",
"application/x-www-form-urlencoded"))
.build());
HttpRequest createThirdNamedTagRequest =
formSigner.filter(HttpRequest.builder()
.method("POST")
.endpoint("https://ec2.us-east-1.amazonaws.com/")
.addHeader("Host", "ec2.us-east-1.amazonaws.com")
.payload(
payloadFromStringWithContentType(
"Action=CreateTags" +
"&ResourceId.1=i-2be64332" +
"&Signature=Trp5e5%2BMqeBeBZbLYa9s9gxahQ9nkx6ETfsGl82IV8Y%3D" +
"&SignatureMethod=HmacSHA256" +
"&SignatureVersion=2" +
"&Tag.1.Key=Name" +
"&Tag.1.Value=third-node" +
"&Timestamp=2012-04-16T15%3A54%3A08.897Z" +
"&Version=2010-08-31" +
"&AWSAccessKeyId=identity",
"application/x-www-form-urlencoded"))
.build());
HttpResponse describeNamedInstanceResponse =
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType(
@ -226,19 +267,27 @@ public class EC2ComputeServiceExpectTest extends BaseEC2ComputeServiceExpectTest
requestResponseMap.put(describeSecurityGroupRequest, describeSecurityGroupResponse);
requestResponseMap.put(authorizeSecurityGroupIngressRequest22, authorizeSecurityGroupIngressResponse);
requestResponseMap.put(authorizeSecurityGroupIngressRequestGroup, authorizeSecurityGroupIngressResponse);
requestResponseMap.put(runInstancesRequest, runInstancesResponse);
requestResponseMap.put(runThreeInstancesRequest, runThreeInstancesResponse);
requestResponseMap.put(describeInstanceRequest, describeNamedInstanceResponse);
requestResponseMap.put(describeInstanceMultiIdsRequest, describeInstanceMultiIdsResponse);
requestResponseMap.put(describeInstanceThreeIdsRequest, describeInstanceThreeIdsResponse);
requestResponseMap.put(describeImageRequest, describeImagesResponse);
requestResponseMap.put(createNamedTagsRequest, createTagsResponse);
requestResponseMap.put(createFirstNamedTagRequest, createTagsResponse);
requestResponseMap.put(createSecondNamedTagRequest, createTagsResponse);
requestResponseMap.put(createThirdNamedTagRequest, createTagsResponse);
ComputeService apiThatCreatesNode = requestsSendResponses(requestResponseMap.build());
NodeMetadata node = Iterables.getOnlyElement(apiThatCreatesNode.createNodesInGroup("test", 1,
blockUntilRunning(false).overrideLoginUser("ec2-user").nodeNames(ImmutableSet.of("test-node"))));
assertEquals(node.getCredentials().getUser(), "ec2-user");
assertNotNull(node.getCredentials().getPrivateKey());
Set<? extends NodeMetadata> nodes = apiThatCreatesNode.createNodesInGroup("test", 3,
maxCount(3).blockUntilRunning(false).overrideLoginUser("ec2-user").nodeNames(ImmutableSet.of("test-node", "second-node", "third-node")));
NodeMetadata node = Iterables.get(nodes, 0);
assertEquals(node.getName(), "test-node");
NodeMetadata secondNode = Iterables.get(nodes, 1);
assertEquals(secondNode.getName(), "second-node");
NodeMetadata thirdNode = Iterables.get(nodes, 2);
assertEquals(thirdNode.getName(), "third-node");
}
//FIXME - issue-1051

View File

@ -13,7 +13,7 @@
</groupSet>
<instancesSet>
<item>
<instanceId>i-2baa5550</instanceId>
<instanceId>i-2ba64342</instanceId>
<imageId>ami-aecd60c7</imageId>
<instanceState>
<code>16</code>
@ -68,6 +68,118 @@
</tagSet>
<hypervisor>xen</hypervisor>
</item>
<item>
<instanceId>i-2bc64242</instanceId>
<imageId>ami-aecd60c7</imageId>
<instanceState>
<code>16</code>
<name>running</name>
</instanceState>
<privateDnsName>ip-10-28-89-195.ec2.internal</privateDnsName>
<dnsName>ec2-50-16-1-166.compute-1.amazonaws.com</dnsName>
<reason/>
<keyName>jclouds#mygroup2#81</keyName>
<amiLaunchIndex>0</amiLaunchIndex>
<productCodes/>
<instanceType>t1.micro</instanceType>
<launchTime>2012-08-02T04:28:30.000Z</launchTime>
<placement>
<availabilityZone>us-east-1e</availabilityZone>
<groupName/>
<tenancy>default</tenancy>
</placement>
<kernelId>aki-88aa75e1</kernelId>
<monitoring>
<state>disabled</state>
</monitoring>
<privateIpAddress>10.28.89.195</privateIpAddress>
<ipAddress>50.16.1.166</ipAddress>
<groupSet>
<item>
<groupId>sg-3c6ef654</groupId>
<groupName>jclouds#mygroup2</groupName>
</item>
</groupSet>
<architecture>x86_64</architecture>
<rootDeviceType>ebs</rootDeviceType>
<rootDeviceName>/dev/sda1</rootDeviceName>
<blockDeviceMapping>
<item>
<deviceName>/dev/sda1</deviceName>
<ebs>
<volumeId>vol-f2d7c993</volumeId>
<status>attached</status>
<attachTime>2012-08-02T04:28:56.000Z</attachTime>
<deleteOnTermination>true</deleteOnTermination>
</ebs>
</item>
</blockDeviceMapping>
<virtualizationType>paravirtual</virtualizationType>
<clientToken/>
<tagSet>
<item>
<key>Name</key>
<value>second-node</value>
</item>
</tagSet>
<hypervisor>xen</hypervisor>
</item>
<item>
<instanceId>i-2be64332</instanceId>
<imageId>ami-aecd60c7</imageId>
<instanceState>
<code>16</code>
<name>running</name>
</instanceState>
<privateDnsName>ip-10-28-89-195.ec2.internal</privateDnsName>
<dnsName>ec2-50-16-1-166.compute-1.amazonaws.com</dnsName>
<reason/>
<keyName>jclouds#mygroup2#81</keyName>
<amiLaunchIndex>0</amiLaunchIndex>
<productCodes/>
<instanceType>t1.micro</instanceType>
<launchTime>2012-08-02T04:28:30.000Z</launchTime>
<placement>
<availabilityZone>us-east-1e</availabilityZone>
<groupName/>
<tenancy>default</tenancy>
</placement>
<kernelId>aki-88aa75e1</kernelId>
<monitoring>
<state>disabled</state>
</monitoring>
<privateIpAddress>10.28.89.195</privateIpAddress>
<ipAddress>50.16.1.166</ipAddress>
<groupSet>
<item>
<groupId>sg-3c6ef654</groupId>
<groupName>jclouds#mygroup2</groupName>
</item>
</groupSet>
<architecture>x86_64</architecture>
<rootDeviceType>ebs</rootDeviceType>
<rootDeviceName>/dev/sda1</rootDeviceName>
<blockDeviceMapping>
<item>
<deviceName>/dev/sda1</deviceName>
<ebs>
<volumeId>vol-f2d7c993</volumeId>
<status>attached</status>
<attachTime>2012-08-02T04:28:56.000Z</attachTime>
<deleteOnTermination>true</deleteOnTermination>
</ebs>
</item>
</blockDeviceMapping>
<virtualizationType>paravirtual</virtualizationType>
<clientToken/>
<tagSet>
<item>
<key>Name</key>
<value>third-node</value>
</item>
</tagSet>
<hypervisor>xen</hypervisor>
</item>
</instancesSet>
</item>
</reservationSet>