Skip to content

Commit 33179cc

Browse files
author
Likitha Shetty
committed
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.
1 parent 6475323 commit 33179cc

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,9 @@ public void orchestrateStart(String vmUuid, Map<VirtualMachineProfile.Param, Obj
10741074
}
10751075
}
10761076
s_logger.info("Unable to start VM on " + dest.getHost() + " due to " + (startAnswer == null ? " no start answer" : startAnswer.getDetails()));
1077+
if (startAnswer.getContextParam("stopRetry") != null) {
1078+
break;
1079+
}
10771080

10781081
} catch (OperationTimedoutException e) {
10791082
s_logger.debug("Unable to send the start command to host " + dest.getHost());

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ protected StartAnswer execute(StartCommand cmd) {
13241324
}
13251325

13261326
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
1327+
boolean vmAlreadyExistsInVcenter = false;
13271328

13281329
Pair<String, String> names = composeVmNames(vmSpec);
13291330
String vmInternalCSName = names.first();
@@ -1335,6 +1336,17 @@ protected StartAnswer execute(StartCommand cmd) {
13351336
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
13361337

13371338
VmwareHypervisorHost hyperHost = getHyperHost(context);
1339+
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
1340+
1341+
// Validate VM name is unique in Datacenter
1342+
VirtualMachineMO vmInVcenter = dcMo.checkIfVmAlreadyExistsInVcenter(vmNameOnVcenter, vmInternalCSName);
1343+
if(vmInVcenter != null) {
1344+
vmAlreadyExistsInVcenter = true;
1345+
String msg = "VM with name: " + vmNameOnVcenter +" already exists in vCenter.";
1346+
s_logger.error(msg);
1347+
throw new Exception(msg);
1348+
}
1349+
13381350
DiskTO[] disks = validateDisks(vmSpec.getDisks());
13391351
assert (disks.length > 0);
13401352
NicTO[] nics = vmSpec.getNics();
@@ -1353,7 +1365,6 @@ protected StartAnswer execute(StartCommand cmd) {
13531365
throw new Exception(msg);
13541366
}
13551367

1356-
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
13571368
VirtualMachineDiskInfoBuilder diskInfoBuilder = null;
13581369
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
13591370
boolean hasSnapshot = false;
@@ -1738,7 +1749,11 @@ protected StartAnswer execute(StartCommand cmd) {
17381749

17391750
String msg = "StartCommand failed due to " + VmwareHelper.getExceptionMessage(e);
17401751
s_logger.warn(msg, e);
1741-
return new StartAnswer(cmd, msg);
1752+
StartAnswer startAnswer = new StartAnswer(cmd, msg);
1753+
if(vmAlreadyExistsInVcenter) {
1754+
startAnswer.setContextParam("stopRetry", "true");
1755+
}
1756+
return startAnswer;
17421757
} finally {
17431758
}
17441759
}

vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,38 @@ public List<VirtualMachineMO> findVmByNameAndLabel(String vmLabel) throws Except
125125
return list;
126126
}
127127

128+
public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter, String vmNameInCS) throws Exception {
129+
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
130+
if (key == 0) {
131+
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
132+
}
133+
134+
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
135+
if (ocs != null && ocs.size() > 0) {
136+
for (ObjectContent oc : ocs) {
137+
List<DynamicProperty> props = oc.getPropSet();
138+
if (props != null) {
139+
String vmVcenterName = null;
140+
String vmInternalCSName = null;
141+
for (DynamicProperty prop : props) {
142+
if (prop.getName().equals("name")) {
143+
vmVcenterName = prop.getVal().toString();
144+
}
145+
if (prop.getName().startsWith("value[") && prop.getVal() != null) {
146+
vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
147+
}
148+
}
149+
if (vmNameOnVcenter.equals(vmVcenterName)) {
150+
if (vmInternalCSName != null && !vmInternalCSName.isEmpty() && !vmNameInCS.equals(vmInternalCSName)) {
151+
return (new VirtualMachineMO(_context, oc.getObj()));
152+
}
153+
}
154+
}
155+
}
156+
}
157+
return null;
158+
}
159+
128160
public List<Pair<ManagedObjectReference, String>> getAllVmsOnDatacenter() throws Exception {
129161
List<Pair<ManagedObjectReference, String>> vms = new ArrayList<Pair<ManagedObjectReference, String>>();
130162

0 commit comments

Comments
 (0)