@@ -772,7 +772,7 @@ private class VmIpAddrFetchThread extends ManagedContextRunnable {
772772 String networkCidr;
773773 String macAddress;
774774
775- public VmIpAddrFetchThread(long vmId, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
775+ public VmIpAddrFetchThread(long vmId, String vmUuid, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
776776 this.vmId = vmId;
777777 this.vmUuid = vmUuid;
778778 this.nicId = nicId;
@@ -794,8 +794,13 @@ protected void runInContext() {
794794 Answer answer = _agentMgr.send(hostId, cmd);
795795 if (answer.getResult()) {
796796 String vmIp = answer.getDetails();
797-
798- if (NetUtils.isValidIp4(vmIp)) {
797+ if (vmIp == null) {
798+ // we got a valid response and the NIC does not have an IP assigned, as such we will update the database with null
799+ if (nic.getIPv4Address() != null) {
800+ nic.setIPv4Address(null);
801+ _nicDao.update(nicId, nic);
802+ }
803+ } else if (NetUtils.isValidIp4(vmIp)) {
799804 // set this vm ip addr in vm nic.
800805 if (nic != null) {
801806 nic.setIPv4Address(vmIp);
@@ -810,12 +815,8 @@ protected void runInContext() {
810815 }
811816 }
812817 } else {
813- //previously vm has ip and nic table has ip address. After vm restart or stop/start
814- //if vm doesnot get the ip then set the ip in nic table to null
815- if (nic.getIPv4Address() != null) {
816- nic.setIPv4Address(null);
817- _nicDao.update(nicId, nic);
818- }
818+ // since no changes are being done, we should not decrement IP usage
819+ decrementCount = false;
819820 if (answer.getDetails() != null) {
820821 logger.debug("Failed to get vm ip for Vm [id: {}, uuid: {}, name: {}], details: {}",
821822 vmId, vmUuid, vmName, answer.getDetails());
@@ -2723,7 +2724,8 @@ protected void runInContext() {
27232724 VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(userVm);
27242725 VirtualMachine vm = vmProfile.getVirtualMachine();
27252726 boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
2726- _vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, nicId, vmInstance.getInstanceName(),
2727+
2728+ _vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, vmInstance.getUuid(), nicId, vmInstance.getInstanceName(),
27272729 isWindows, vm.getHostId(), network.getCidr(), nicVo.getMacAddress()));
27282730
27292731 }
0 commit comments