@@ -824,7 +824,7 @@ private class VmIpAddrFetchThread extends ManagedContextRunnable {
824824 String networkCidr;
825825 String macAddress;
826826
827- public VmIpAddrFetchThread(long vmId, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
827+ public VmIpAddrFetchThread(long vmId, String vmUuid, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
828828 this.vmId = vmId;
829829 this.vmUuid = vmUuid;
830830 this.nicId = nicId;
@@ -846,8 +846,13 @@ protected void runInContext() {
846846 Answer answer = _agentMgr.send(hostId, cmd);
847847 if (answer.getResult()) {
848848 String vmIp = answer.getDetails();
849-
850- if (NetUtils.isValidIp4(vmIp)) {
849+ if (vmIp == null) {
850+ // we got a valid response and the NIC does not have an IP assigned, as such we will update the database with null
851+ if (nic.getIPv4Address() != null) {
852+ nic.setIPv4Address(null);
853+ _nicDao.update(nicId, nic);
854+ }
855+ } else if (NetUtils.isValidIp4(vmIp)) {
851856 // set this vm ip addr in vm nic.
852857 if (nic != null) {
853858 nic.setIPv4Address(vmIp);
@@ -862,12 +867,8 @@ protected void runInContext() {
862867 }
863868 }
864869 } else {
865- //previously vm has ip and nic table has ip address. After vm restart or stop/start
866- //if vm doesnot get the ip then set the ip in nic table to null
867- if (nic.getIPv4Address() != null) {
868- nic.setIPv4Address(null);
869- _nicDao.update(nicId, nic);
870- }
870+ // since no changes are being done, we should not decrement IP usage
871+ decrementCount = false;
871872 if (answer.getDetails() != null) {
872873 logger.debug("Failed to get vm ip for Vm [id: {}, uuid: {}, name: {}], details: {}",
873874 vmId, vmUuid, vmName, answer.getDetails());
@@ -2781,7 +2782,7 @@ protected void runInContext() {
27812782 VirtualMachine vm = vmProfile.getVirtualMachine();
27822783 boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
27832784
2784- _vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, nicId, vmInstance.getInstanceName(),
2785+ _vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, vmInstance.getUuid(), nicId, vmInstance.getInstanceName(),
27852786 isWindows, vm.getHostId(), network.getCidr(), nicVo.getMacAddress()));
27862787
27872788 }
0 commit comments