//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 Exception("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 Exception("zone not found");
}
// we choose first zone?
CloudStackZone zone = zones.get(0);
// network
//CloudStackNetwork network = findNetwork(zone);
// for EC2 security groups either a group ID or a group name is accepted
String[] sgIdList = request.getSecurityGroupIdSet();
String[] sgNameList = request.getSecurityGroupNameSet();
if (sgIdList.length != 0 && sgNameList.length != 0)
throw new EC2ServiceException(ClientError.InvalidParameterCombination,
" for EC2 groups either a group ID or a group name is accepted");
if (sgIdList.length != 0)
groupIds = constructList(sgIdList);
if (sgNameList.length != 0)
groupNames = constructList(sgNameList);
// 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, null,
groupIds, groupNames, 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( mapToAmazonHypervisorType(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());