AWSSecurityGroupApi securityGroupApi = view.unwrapApi(AWSEC2Api.class).getSecurityGroupApi().get();
KeyPairApi keyPairApi = view.unwrapApi(AWSEC2Api.class).getKeyPairApi().get();
InstanceApi instanceApi = view.unwrapApi(AWSEC2Api.class).getInstanceApi().get();
String group = this.group + "o";
Date before = new Date();
ImmutableMap<String, String> userMetadata = ImmutableMap.<String, String> of("test", group);
ImmutableSet<String> tags = ImmutableSet.of(group);
// note that if you change the location, you must also specify image parameters
Template template = client.templateBuilder().locationId(region).osFamily(AMZN_LINUX).os64Bit(true).build();
template.getOptions().tags(tags);
template.getOptions().userMetadata(userMetadata);
template.getOptions().tags(tags);
template.getOptions().as(AWSEC2TemplateOptions.class).enableMonitoring();
template.getOptions().as(AWSEC2TemplateOptions.class).spotPrice(0.3f);
String startedId = null;
try {
cleanupExtendedStuffInRegion(region, securityGroupApi, keyPairApi, group);
Thread.sleep(3000); // eventual consistency if deletes actually occurred.
// create a security group that allows ssh in so that our scripts later
// will work
String groupId = securityGroupApi.createSecurityGroupInRegionAndReturnId(region, group, group);
securityGroupApi.authorizeSecurityGroupIngressInRegion(region, groupId, permit(IpProtocol.TCP).port(22));
template.getOptions().as(AWSEC2TemplateOptions.class).securityGroupIds(groupId);
// create a keypair to pass in as well
KeyPair result = keyPairApi.createKeyPairInRegion(region, group);
template.getOptions().as(AWSEC2TemplateOptions.class).keyPair(result.getKeyName());
// pass in the private key, so that we can run a script with it
assert result.getKeyMaterial() != null : result;
template.getOptions().overrideLoginPrivateKey(result.getKeyMaterial());
Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template);
NodeMetadata first = getOnlyElement(nodes);
checkUserMetadataContains(first, userMetadata);
checkTagsInNodeEquals(first, tags);
assert first.getCredentials() != null : first;
assert first.getCredentials().identity != null : first;
startedId = first.getProviderId();
AWSRunningInstance instance = AWSRunningInstance.class.cast(getOnlyElement(getOnlyElement(instanceApi
.describeInstancesInRegion(region, startedId))));
assertEquals(instance.getKeyName(), group);
assert instance.getSpotInstanceRequestId() != null;
assertEquals(instance.getMonitoringState(), MonitoringState.ENABLED);