Skip to content

Commit b20a106

Browse files
committed
address comments - extract code + add appropriate operation name in logs
1 parent 1f27c6a commit b20a106

1 file changed

Lines changed: 27 additions & 24 deletions

File tree

  • plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ private void plugNicCommandInternal(String vmName, VirtualEthernetCardType nicDe
13291329
nicTo.getMac(), deviceNumber + 1, true, true);
13301330
}
13311331

1332-
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.ADD);
1332+
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.ADD, "PlugNicCommand");
13331333
}
13341334

13351335
private ReplugNicAnswer execute(ReplugNicCommand cmd) {
@@ -1390,7 +1390,7 @@ private ReplugNicAnswer execute(ReplugNicCommand cmd) {
13901390
VmwareHelper.updateNicDevice(nic, networkInfo.first(), networkInfo.second());
13911391
}
13921392

1393-
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.EDIT);
1393+
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.EDIT, "ReplugNicCommand");
13941394

13951395
return new ReplugNicAnswer(cmd, true, "success");
13961396
} catch (Exception e) {
@@ -1431,7 +1431,7 @@ private UnPlugNicAnswer execute(UnPlugNicCommand cmd) {
14311431
if (nic == null) {
14321432
return new UnPlugNicAnswer(cmd, true, "success");
14331433
}
1434-
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.REMOVE);
1434+
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.REMOVE, "unplugNicCommand");
14351435

14361436
return new UnPlugNicAnswer(cmd, true, "success");
14371437
} catch (Exception e) {
@@ -1478,7 +1478,7 @@ private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final IpA
14781478
device.setBacking(dataCenterMo.getDvPortBackingInfo(networkInfo));
14791479
}
14801480

1481-
configureNicDevice(vmMo, device, VirtualDeviceConfigSpecOperation.EDIT);
1481+
configureNicDevice(vmMo, device, VirtualDeviceConfigSpecOperation.EDIT, "plugPublicNic");
14821482
} catch (Exception e) {
14831483

14841484
// restore allocation mask in case of exceptions
@@ -1598,7 +1598,7 @@ private ExecutionResult cleanupNetworkElementCommand(IpAssocCommand cmd) {
15981598

15991599
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
16001600
// command may sometimes be redirect to a wrong host, we relax
1601-
// the check and will try to find it within cluster
1601+
// the check and will try to find it within datacenter
16021602
if (vmMo == null) {
16031603
if (hyperHost instanceof HostMO) {
16041604
final DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
@@ -1613,27 +1613,12 @@ private ExecutionResult cleanupNetworkElementCommand(IpAssocCommand cmd) {
16131613
}
16141614

16151615
if (ips.length == 1 && !ips[0].isAdd()) {
1616-
IpAddressTO ip = ips[0];
1617-
NicTO nicTO = ip.getNicTO();
1618-
URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
1619-
if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
1620-
throw new InternalErrorException(String.format("Unable to assign a public IP to a VIF on network %s", ip.getBroadcastUri()));
1621-
}
1622-
String vlanId = BroadcastDomainType.getValue(broadcastUri);
1623-
1624-
String publicNetworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
1625-
Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNetworkName);
1626-
1627-
if (s_logger.isDebugEnabled()) {
1628-
s_logger.debug(String.format("Find public NIC index, public network name: %s , index: %s", publicNetworkName, publicNicInfo.first()));
1629-
}
1630-
1631-
VirtualDevice nic = findVirtualNicDevice(vmMo, nicTO.getMac());
1616+
VirtualDevice nic = getVirtualDevice(vmMo, ips[0]);
16321617

16331618
if (nic == null) {
16341619
return new ExecutionResult(false, "Couldn't find NIC");
16351620
}
1636-
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.REMOVE);
1621+
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.REMOVE, "unplugNicCommand");
16371622
}
16381623
} catch (Throwable e) {
16391624
s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
@@ -1642,7 +1627,25 @@ private ExecutionResult cleanupNetworkElementCommand(IpAssocCommand cmd) {
16421627
return new ExecutionResult(true, null);
16431628
}
16441629

1645-
private void configureNicDevice(VirtualMachineMO vmMo, VirtualDevice nic, VirtualDeviceConfigSpecOperation operation) throws Exception {
1630+
private VirtualDevice getVirtualDevice(VirtualMachineMO vmMo, IpAddressTO ip) throws Exception {
1631+
NicTO nicTO = ip.getNicTO();
1632+
URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
1633+
if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
1634+
throw new InternalErrorException(String.format("Unable to assign a public IP to a VIF on network %s", ip.getBroadcastUri()));
1635+
}
1636+
String vlanId = BroadcastDomainType.getValue(broadcastUri);
1637+
1638+
String publicNetworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
1639+
Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNetworkName);
1640+
1641+
if (s_logger.isDebugEnabled()) {
1642+
s_logger.debug(String.format("Find public NIC index, public network name: %s , index: %s", publicNetworkName, publicNicInfo.first()));
1643+
}
1644+
1645+
return findVirtualNicDevice(vmMo, nicTO.getMac());
1646+
}
1647+
1648+
private void configureNicDevice(VirtualMachineMO vmMo, VirtualDevice nic, VirtualDeviceConfigSpecOperation operation, String commandName) throws Exception {
16461649
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
16471650
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
16481651
deviceConfigSpec.setDevice(nic);
@@ -1651,7 +1654,7 @@ private void configureNicDevice(VirtualMachineMO vmMo, VirtualDevice nic, Virtua
16511654

16521655
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
16531656
if (!vmMo.configureVm(vmConfigSpec)) {
1654-
throw new Exception("Failed to configure devices when running unplugNicCommand");
1657+
throw new Exception(String.format("Failed to configure devices when running %s", commandName));
16551658
}
16561659
}
16571660

0 commit comments

Comments
 (0)