Caller caller,
String sshKeyName)
throws Exception {
if (result == null) {
throw new CannotTranslateException("creation result is missing");
}
final VM[] vms = result.getVMs();
if (vms == null || vms.length == 0) {
throw new CannotTranslateException("creation result is empty?");
}
if (result.getCoscheduledID() != null) {
throw new CannotTranslateException("not expecting " +
"coscheduling ID in any cases yet");
}
final String groupid = result.getGroupID();
if (groupid == null && vms.length != 1) {
throw new CannotTranslateException("expecting a groupID if " +
"more than one VM was created");
}
final String vmidWhenJustOne;
final String resID;
// these mappings may exist already, for secondary idempotent launches
if (groupid == null) {
vmidWhenJustOne = vms[0].getID();
resID = this.ids.getOrNewInstanceReservationID(vmidWhenJustOne, sshKeyName);
} else {
vmidWhenJustOne = null;
resID = this.ids.getOrNewGroupReservationID(groupid);
}
final RunningInstancesSetType rist = new RunningInstancesSetType();
final RunningInstancesItemType[] riits =
new RunningInstancesItemType[vms.length];
final String msg = "New reservation ID '" + resID + "' for ";
final StringBuffer buf = new StringBuffer(msg);
if (vmidWhenJustOne == null) {
buf.append("VM group '").append(groupid);
} else {
buf.append("single VM '").append(vmidWhenJustOne);
}
buf.append("'. Members:");
for (int i = 0; i < vms.length; i++) {
final VM vm = vms[i];
final String id = vm.getID();
if (id == null) {
throw new CannotTranslateException("VM has no ID");
}
final String instID;
if (vmidWhenJustOne != null) {
// mapping already created:
instID = this.ids.managerInstanceToElasticInstance(vmidWhenJustOne);
} else {
// this mapping may exist already, for secondary idempotent launches
instID = this.ids.getOrNewInstanceID(vm.getID(), resID, sshKeyName);
}
if (i != 0) {
buf.append(",");
}
buf.append(" id-").append(id)
.append("='").append(instID).append("'");
riits[i] = this.getOneCreatedVM(vm, instID, resID, sshKeyName);
}
logger.info(buf.toString());
final RunInstancesResponseType ret = new RunInstancesResponseType();
ret.setGroupSet(getGroupStub());
final String ownerID = this.container.getOwnerID(caller);
if (ownerID == null) {
throw new CannotTranslateException("Cannot find owner ID");
}
ret.setOwnerId(ownerID);
ret.setReservationId(resID);
rist.setItem(riits);
ret.setInstancesSet(rist);