use of com.amazonaws.services.ec2.model.RunInstancesRequest in project camel by apache.
the class AmazonEC2ClientMock method runInstances.
@Override
public RunInstancesResult runInstances(RunInstancesRequest runInstancesRequest) {
RunInstancesResult result = new RunInstancesResult();
if (runInstancesRequest.getImageId().equals("test-1")) {
Reservation res = new Reservation();
res.setOwnerId("1");
res.setRequesterId("user-test");
res.setReservationId("res-1");
Collection<Instance> instances = new ArrayList();
Instance ins = new Instance();
ins.setImageId(runInstancesRequest.getImageId());
ins.setInstanceType(runInstancesRequest.getInstanceType());
ins.setInstanceId("instance-1");
if (runInstancesRequest.getSecurityGroups() != null) {
if (runInstancesRequest.getSecurityGroups().contains("secgroup-1") && runInstancesRequest.getSecurityGroups().contains("secgroup-2")) {
GroupIdentifier id1 = new GroupIdentifier();
id1.setGroupId("id-1");
id1.setGroupName("secgroup-1");
GroupIdentifier id2 = new GroupIdentifier();
id2.setGroupId("id-2");
id2.setGroupName("secgroup-2");
Collection secGroups = new ArrayList<GroupIdentifier>();
secGroups.add(id1);
secGroups.add(id2);
ins.setSecurityGroups(secGroups);
} else if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName())) {
if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName().contains("keypair-1"))) {
GroupIdentifier id1 = new GroupIdentifier();
id1.setGroupId("id-3");
id1.setGroupName("secgroup-3");
GroupIdentifier id2 = new GroupIdentifier();
id2.setGroupId("id-4");
id2.setGroupName("secgroup-4");
Collection secGroups = new ArrayList<GroupIdentifier>();
secGroups.add(id1);
secGroups.add(id2);
ins.setSecurityGroups(secGroups);
}
}
}
instances.add(ins);
res.setInstances(instances);
result.setReservation(res);
} else {
throw new AmazonServiceException("The image-id doesn't exists");
}
return result;
}
use of com.amazonaws.services.ec2.model.RunInstancesRequest in project GNS by MobilityFirst.
the class AWSEC2 method createInstanceAndWait.
/**
* Create an Instance
*
* @param ec2
* @param amiRecord
* @param key
* @param securityGroup
* @return the instanceID string
*/
public static String createInstanceAndWait(AmazonEC2 ec2, AMIRecord amiRecord, String key, SecurityGroup securityGroup) {
RunInstancesRequest runInstancesRequest;
if (amiRecord.getVpcSubnet() != null) {
System.out.println("subnet: " + amiRecord.getVpcSubnet() + " securityGroup: " + securityGroup.getGroupName());
// new VPC
runInstancesRequest = new RunInstancesRequest().withMinCount(1).withMaxCount(1).withImageId(amiRecord.getName()).withInstanceType(amiRecord.getInstanceType()).withKeyName(key).withSubnetId(amiRecord.getVpcSubnet()).withSecurityGroupIds(Arrays.asList(securityGroup.getGroupId()));
} else {
runInstancesRequest = new RunInstancesRequest(amiRecord.getName(), 1, 1);
runInstancesRequest.setInstanceType(amiRecord.getInstanceType());
runInstancesRequest.setSecurityGroups(new ArrayList<>(Arrays.asList(securityGroup.getGroupName())));
runInstancesRequest.setKeyName(key);
}
RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
Instance instance = runInstancesResult.getReservation().getInstances().get(0);
String createdInstanceId = instance.getInstanceId();
System.out.println("Waiting for instance " + amiRecord.getName() + " to start");
long startTime = System.currentTimeMillis();
do {
ThreadUtils.sleep(1000);
if (System.currentTimeMillis() - startTime > 90000) {
// give it a minute and a half
System.out.println(createdInstanceId + " timed out waiting for start.");
return null;
}
// regrab the instance data from the server
instance = findInstance(ec2, createdInstanceId);
//System.out.print(instance.getState().getName());
System.out.print(".");
} while (instance != null && !instance.getState().getName().equals(InstanceStateRecord.RUNNING.getName()));
System.out.println();
return createdInstanceId;
}
Aggregations