CLOUDSTACK-8112. CS allows creation of VM's with the same Display name when vm.instancename.flag is set to true.

Before registering a VM check if a different CS VM with same name exists in vCenter.

(cherry picked from commit 33179cce56b15f0632e38afa260cb829bb2a2273)
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Likitha Shetty 2014-11-10 10:58:11 +05:30 committed by Rohit Yadav
parent b2fa91629e
commit 45d32234a6
3 changed files with 52 additions and 2 deletions

View File

@ -1067,6 +1067,9 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
}
}
s_logger.info("Unable to start VM on " + dest.getHost() + " due to " + (startAnswer == null ? " no start answer" : startAnswer.getDetails()));
if (startAnswer.getContextParam("stopRetry") != null) {
break;
}
} catch (OperationTimedoutException e) {
s_logger.debug("Unable to send the start command to host " + dest.getHost());

View File

@ -1324,6 +1324,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
boolean vmAlreadyExistsInVcenter = false;
Pair<String, String> names = composeVmNames(vmSpec);
String vmInternalCSName = names.first();
@ -1335,6 +1336,17 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
VmwareHypervisorHost hyperHost = getHyperHost(context);
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
// Validate VM name is unique in Datacenter
VirtualMachineMO vmInVcenter = dcMo.checkIfVmAlreadyExistsInVcenter(vmNameOnVcenter, vmInternalCSName);
if(vmInVcenter != null) {
vmAlreadyExistsInVcenter = true;
String msg = "VM with name: " + vmNameOnVcenter +" already exists in vCenter.";
s_logger.error(msg);
throw new Exception(msg);
}
DiskTO[] disks = validateDisks(vmSpec.getDisks());
assert (disks.length > 0);
NicTO[] nics = vmSpec.getNics();
@ -1353,7 +1365,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
throw new Exception(msg);
}
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
VirtualMachineDiskInfoBuilder diskInfoBuilder = null;
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
boolean hasSnapshot = false;
@ -1738,7 +1749,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
String msg = "StartCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.warn(msg, e);
return new StartAnswer(cmd, msg);
StartAnswer startAnswer = new StartAnswer(cmd, msg);
if(vmAlreadyExistsInVcenter) {
startAnswer.setContextParam("stopRetry", "true");
}
return startAnswer;
} finally {
}
}

View File

@ -125,6 +125,38 @@ public class DatacenterMO extends BaseMO {
return list;
}
public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter, String vmNameInCS) throws Exception {
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
if (key == 0) {
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
}
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
if (ocs != null && ocs.size() > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
String vmVcenterName = null;
String vmInternalCSName = null;
for (DynamicProperty prop : props) {
if (prop.getName().equals("name")) {
vmVcenterName = prop.getVal().toString();
}
if (prop.getName().startsWith("value[") && prop.getVal() != null) {
vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
}
}
if (vmNameOnVcenter.equals(vmVcenterName)) {
if (vmInternalCSName != null && !vmInternalCSName.isEmpty() && !vmNameInCS.equals(vmInternalCSName)) {
return (new VirtualMachineMO(_context, oc.getObj()));
}
}
}
}
}
return null;
}
public List<Pair<ManagedObjectReference, String>> getAllVmsOnDatacenter() throws Exception {
List<Pair<ManagedObjectReference, String>> vms = new ArrayList<Pair<ManagedObjectReference, String>>();