//find CS service Offering ID
String instanceType = "m1.small";
if(request.getInstanceType() != null){
instanceType = request.getInstanceType();
}
CloudStackServiceOfferingVO svcOffering = getCSServiceOfferingId(instanceType);
if(svcOffering == null){
logger.info("No ServiceOffering found to be defined by name, please contact the administrator "+instanceType );
throw new EC2ServiceException(ClientError.Unsupported, "instanceType: [" + instanceType + "] not found!");
}
// zone stuff
String zoneId = toZoneId(request.getZoneName(), null);
List<CloudStackZone> zones = getApi().listZones(null, null, zoneId, null);
if (zones == null || zones.size() == 0) {
logger.info("EC2 RunInstances - zone [" + request.getZoneName() + "] not found!");
throw new EC2ServiceException(ClientError.InvalidZone_NotFound, "ZoneId [" + request.getZoneName() + "] not found!");
}
// we choose first zone?
CloudStackZone zone = zones.get(0);
// network
CloudStackNetwork network = findNetwork(zone);
// now actually deploy the vms
for( int i=0; i < createInstances; i++ ) {
try{
CloudStackUserVm resp = getApi().deployVirtualMachine(svcOffering.getId(),
request.getTemplateId(), zoneId, null, null, null, null,
null, null, null, request.getKeyName(), null, (network != null ? network.getId() : null),
null, constructList(request.getGroupSet()), request.getSize().longValue(), request.getUserData());
EC2Instance vm = new EC2Instance();
vm.setId(resp.getId().toString());
vm.setName(resp.getName());
vm.setZoneName(resp.getZoneName());
vm.setTemplateId(resp.getTemplateId().toString());
if (resp.getSecurityGroupList() != null && resp.getSecurityGroupList().size() > 0) {
List<CloudStackSecurityGroup> securityGroupList = resp.getSecurityGroupList();
for (CloudStackSecurityGroup securityGroup : securityGroupList) {
EC2SecurityGroup param = new EC2SecurityGroup();
param.setId(securityGroup.getId());
param.setName(securityGroup.getName());
vm.addGroupName(param);
}
}
vm.setState(resp.getState());
vm.setCreated(resp.getCreated());
List <CloudStackNic> nicList = resp.getNics();
for (CloudStackNic nic : nicList) {
if (nic.getIsDefault()) {
vm.setPrivateIpAddress(nic.getIpaddress());
break;
}
}
vm.setIpAddress(resp.getIpAddress());
vm.setAccountName(resp.getAccountName());
vm.setDomainId(resp.getDomainId());
vm.setHypervisor(resp.getHypervisor());
vm.setServiceOffering( svcOffering.getName());
vm.setKeyPairName(resp.getKeyPairName());
instances.addInstance(vm);
countCreated++;
}catch(Exception e){
logger.error("Failed to deploy VM number: "+ (i+1) +" due to error: "+e.getMessage());