cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] GabrielBrascher commented on a change in pull request #2595: CLOUDSTACK-10199: Support requesting a specific IPv4 address
Date Mon, 12 Nov 2018 11:41:41 GMT
GabrielBrascher commented on a change in pull request #2595: CLOUDSTACK-10199: Support requesting
a specific IPv4 address
URL: https://github.com/apache/cloudstack/pull/2595#discussion_r232626456
 
 

 ##########
 File path: engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java
 ##########
 @@ -883,6 +885,73 @@ public void saveExtraDhcpOptions(final String networkUuid, final Long
nicId, fin
         return new Pair<NicProfile, Integer>(vmNic, Integer.valueOf(deviceId));
     }
 
+    /**
+     * If the requested IPv4 address from the NicProfile was configured then it configures
the IPv4 address, Netmask and Gateway to deploy the VM with the requested IP. 
+     */
+    protected void configureNicProfileBasedOnRequestedIp(NicProfile requestedNicProfile,
NicProfile nicProfile, Network network) {
+        String requestedIpv4Address = requestedNicProfile.getRequestedIPv4();
+        if (requestedIpv4Address == null) {
+            s_logger.debug(String.format("The requested [IPv4 address=%s] is null", requestedIpv4Address));
+            return;
+        }
+        if (org.apache.commons.lang3.StringUtils.isBlank(requestedIpv4Address)) {
+            throw new InvalidParameterValueException(String.format("The requested [IPv4 address='%s']
is empty or blank", requestedIpv4Address));
+        }
+        if (!NetUtils.isValidIp4(requestedIpv4Address)) {
+            throw new InvalidParameterValueException(String.format("The requested [IPv4 address='%s']
is not a valid IP address", requestedIpv4Address));
+        }
+
+        VlanVO vlanVo = _vlanDao.findByNetworkId(network.getId());
+        if (vlanVo == null) {
+            throw new CloudRuntimeException(String.format("Trying to configure a Nic with
the requested [IPv4='%s'] but cannot find a Vlan for the [network id='%s']",
+                    requestedIpv4Address, network.getId()));
+        }
+
+        acquireLockAndCheckIfIpv4IsFree(network, requestedIpv4Address);
+
+        String ipv4Gateway = vlanVo.getVlanGateway();
+        String ipv4Netmask = vlanVo.getVlanNetmask();
+
+        if (org.apache.commons.lang3.StringUtils.isBlank(ipv4Gateway) || !NetUtils.isValidIp4(ipv4Gateway))
{
+            throw new CloudRuntimeException(String.format("The [IPv4Gateway='%s'] from [VlanId='%s']
is not valid", ipv4Gateway, vlanVo.getId()));
+        }
+        if (org.apache.commons.lang3.StringUtils.isBlank(ipv4Netmask) || !NetUtils.isValidIp4Netmask(ipv4Netmask))
{
+            throw new CloudRuntimeException(String.format("The [IPv4Netmask='%s'] from [VlanId='%s']
is not valid", ipv4Netmask, vlanVo.getId()));
+        }
+
+        nicProfile.setIPv4Address(requestedIpv4Address);
+        nicProfile.setIPv4Gateway(ipv4Gateway);
+        nicProfile.setIPv4Netmask(ipv4Netmask);
+
+        if (nicProfile.getMacAddress() == null) {
+            try {
+                String macAddress = _networkModel.getNextAvailableMacAddressInNetwork(network.getId());
+                nicProfile.setMacAddress(macAddress);
+            } catch (InsufficientAddressCapacityException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     *  Acquires lock of in table "user_ip_address" and checks if the requested IPv4 address
is Free.
+     */
+    protected void acquireLockAndCheckIfIpv4IsFree(Network network, String requestedIpv4Address)
{
+        IPAddressVO ipVO = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), requestedIpv4Address);
+        if (ipVO == null) {
+            throw new CloudRuntimeException(String.format("Cannot find IPAddressVO for guest
[IPv4 address='%s'] and [network id=%s]", requestedIpv4Address, network.getId()));
+        }
+        IPAddressVO lockedIpVO = _ipAddressDao.acquireInLockTable(ipVO.getId());
 
 Review comment:
   @marcaurele are you LGTM on this one?
   Thanks for your reviews and hard work :+1: 

----------------------------------------------------------------
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

Mime
View raw message