You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2018/01/09 08:23:20 UTC

[GitHub] rhtyd closed pull request #2388: CLOUDSTACK-10212: Update Netmask/Gateway when Changing IPv4 address

rhtyd closed pull request #2388: CLOUDSTACK-10212: Update Netmask/Gateway when Changing IPv4 address
URL: https://github.com/apache/cloudstack/pull/2388
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index df9130c682d..b2978b1968f 100644
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -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;
@@ -473,7 +474,7 @@
     @Inject
     private UserStatisticsDao _userStatsDao;
     @Inject
-    private VlanDao _vlanDao;
+    protected VlanDao _vlanDao;
     @Inject
     VolumeService _volService;
     @Inject
@@ -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() {
@@ -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);
 
diff --git a/server/test/com/cloud/vm/UserVmManagerTest.java b/server/test/com/cloud/vm/UserVmManagerTest.java
index 89555a2c8c8..c61d5cdce1d 100644
--- a/server/test/com/cloud/vm/UserVmManagerTest.java
+++ b/server/test/com/cloud/vm/UserVmManagerTest.java
@@ -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;
@@ -195,6 +198,8 @@
     @Mock
     NicDao _nicDao;
     @Mock
+    VlanDao _vlanDao;
+    @Mock
     NicVO _nicMock;
     @Mock
     NetworkModel _networkModel;
@@ -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;
@@ -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);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services