Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/src/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
import com.cloud.dc.dao.DedicatedResourceDao;
import com.cloud.dc.dao.HostPodDao;
import com.cloud.dc.dao.VlanDao;
import com.cloud.dc.Vlan;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.deploy.DataCenterDeployment;
Expand Down Expand Up @@ -473,7 +474,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
@Inject
private UserStatisticsDao _userStatsDao;
@Inject
private VlanDao _vlanDao;
protected VlanDao _vlanDao;
@Inject
VolumeService _volService;
@Inject
Expand Down Expand Up @@ -1565,6 +1566,12 @@ public UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd) {
if (ipaddr == null) {
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
}

final IPAddressVO newIp = _ipAddressDao.findByIpAndDcId(dc.getId(), ipaddr);
final Vlan vlan = _vlanDao.findById(newIp.getVlanId());
nicVO.setIPv4Gateway(vlan.getVlanGateway());
nicVO.setIPv4Netmask(vlan.getVlanNetmask());

final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nicVO.getNetworkId(), nicVO.getIPv4Address());
if (ip != null) {
Transaction.execute(new TransactionCallbackNoReturn() {
Expand All @@ -1584,7 +1591,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
return null;
}

// update nic ipaddress
s_logger.debug("Updating IPv4 address of NIC " + nicVO + " to " + ipaddr + "/" + nicVO.getIPv4Netmask() + " with gateway " + nicVO.getIPv4Gateway());
nicVO.setIPv4Address(ipaddr);
_nicDao.persist(nicVO);

Expand Down
15 changes: 15 additions & 0 deletions server/test/com/cloud/vm/UserVmManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import java.util.Map;
import java.util.UUID;

import com.cloud.dc.VlanVO;
import com.cloud.dc.dao.VlanDao;
import com.cloud.network.dao.IPAddressVO;
import com.cloud.network.element.UserDataServiceProvider;
import com.cloud.storage.Storage;
import com.cloud.user.User;
Expand Down Expand Up @@ -195,6 +198,8 @@ public class UserVmManagerTest {
@Mock
NicDao _nicDao;
@Mock
VlanDao _vlanDao;
@Mock
NicVO _nicMock;
@Mock
NetworkModel _networkModel;
Expand Down Expand Up @@ -243,6 +248,7 @@ public void setup() {
_userVmMgr._vmSnapshotDao = _vmSnapshotDao;
_userVmMgr._configDao = _configDao;
_userVmMgr._nicDao = _nicDao;
_userVmMgr._vlanDao = _vlanDao;
_userVmMgr._networkModel = _networkModel;
_userVmMgr._networkDao = _networkDao;
_userVmMgr._dcDao = _dcDao;
Expand Down Expand Up @@ -843,9 +849,18 @@ public void testUpdateVmNicIpSuccess2() throws Exception {
when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);

IPAddressVO newIp = mock(IPAddressVO.class);
when(newIp.getVlanId()).thenReturn(1L);

VlanVO vlan = mock(VlanVO.class);
when(vlan.getVlanGateway()).thenReturn("10.10.10.1");
when(vlan.getVlanNetmask()).thenReturn("255.255.255.0");

when(_ipAddrMgr.allocatePublicIpForGuestNic(Mockito.eq(_networkMock), anyLong(), Mockito.eq(_accountMock), anyString())).thenReturn("10.10.10.10");
when(_ipAddressDao.findByIpAndSourceNetworkId(anyLong(), anyString())).thenReturn(null);
when(_nicDao.persist(any(NicVO.class))).thenReturn(nic);
when(_ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(newIp);
when(_vlanDao.findById(anyLong())).thenReturn(vlan);

Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
Expand Down