Skip to content

Commit 9db6309

Browse files
winterhazelDaan Hoogland
authored andcommitted
Address public IP limit validations
1 parent c6b20b8 commit 9db6309

8 files changed

Lines changed: 135 additions & 95 deletions

File tree

engine/schema/src/main/java/com/cloud/dc/dao/AccountVlanMapDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public interface AccountVlanMapDao extends GenericDao<AccountVlanMapVO, Long> {
2727

2828
public List<AccountVlanMapVO> listAccountVlanMapsByVlan(long vlanDbId);
2929

30-
public AccountVlanMapVO findAccountVlanMap(long accountId, long vlanDbId);
30+
public AccountVlanMapVO findAccountVlanMap(Long accountId, long vlanDbId);
3131

3232
}

engine/schema/src/main/java/com/cloud/dc/dao/AccountVlanMapDaoImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public List<AccountVlanMapVO> listAccountVlanMapsByVlan(long vlanDbId) {
4848
}
4949

5050
@Override
51-
public AccountVlanMapVO findAccountVlanMap(long accountId, long vlanDbId) {
51+
public AccountVlanMapVO findAccountVlanMap(Long accountId, long vlanDbId) {
5252
SearchCriteria<AccountVlanMapVO> sc = AccountVlanSearch.create();
53-
sc.setParameters("accountId", accountId);
53+
sc.setParametersIfNotNull("accountId", accountId);
5454
sc.setParameters("vlanDbId", vlanDbId);
5555
return findOneIncludingRemovedBy(sc);
5656
}

engine/schema/src/main/java/com/cloud/dc/dao/DomainVlanMapDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
public interface DomainVlanMapDao extends GenericDao<DomainVlanMapVO, Long> {
2525
public List<DomainVlanMapVO> listDomainVlanMapsByDomain(long domainId);
2626
public List<DomainVlanMapVO> listDomainVlanMapsByVlan(long vlanDbId);
27-
public DomainVlanMapVO findDomainVlanMap(long domainId, long vlanDbId);
27+
public DomainVlanMapVO findDomainVlanMap(Long domainId, long vlanDbId);
2828
}

engine/schema/src/main/java/com/cloud/dc/dao/DomainVlanMapDaoImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public List<DomainVlanMapVO> listDomainVlanMapsByVlan(long vlanDbId) {
4646
}
4747

4848
@Override
49-
public DomainVlanMapVO findDomainVlanMap(long domainId, long vlanDbId) {
49+
public DomainVlanMapVO findDomainVlanMap(Long domainId, long vlanDbId) {
5050
SearchCriteria<DomainVlanMapVO> sc = DomainVlanSearch.create();
51-
sc.setParameters("domainId", domainId);
51+
sc.setParametersIfNotNull("domainId", domainId);
5252
sc.setParameters("vlanDbId", vlanDbId);
5353
return findOneIncludingRemovedBy(sc);
5454
}

server/src/main/java/com/cloud/api/ApiDBUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,10 @@ public static boolean isAdmin(Account account) {
22912291
return s_accountService.isAdmin(account.getId());
22922292
}
22932293

2294+
public static Account getSystemAccount() {
2295+
return s_accountService.getSystemAccount();
2296+
}
2297+
22942298
public static List<ResourceTagJoinVO> listResourceTagViewByResourceUUID(String resourceUUID, ResourceObjectType resourceType) {
22952299
return s_tagJoinDao.listBy(resourceUUID, resourceType);
22962300
}

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 105 additions & 79 deletions
Large diffs are not rendered by default.

server/src/main/java/com/cloud/network/NetworkServiceImpl.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import javax.inject.Inject;
4242
import javax.naming.ConfigurationException;
4343

44+
import com.cloud.resourcelimit.CheckedReservation;
4445
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
4546
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4647
import org.apache.cloudstack.alert.AlertService;
@@ -76,6 +77,7 @@
7677
import org.apache.cloudstack.network.RoutedIpv4Manager;
7778
import org.apache.cloudstack.network.dao.NetworkPermissionDao;
7879
import org.apache.cloudstack.network.element.InternalLoadBalancerElementService;
80+
import org.apache.cloudstack.reservation.dao.ReservationDao;
7981
import org.apache.commons.collections.CollectionUtils;
8082
import org.apache.commons.collections.MapUtils;
8183
import org.apache.commons.lang3.BooleanUtils;
@@ -335,6 +337,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService, C
335337
@Inject
336338
ResourceLimitService _resourceLimitMgr;
337339
@Inject
340+
ReservationDao reservationDao;
341+
@Inject
338342
DomainManager _domainMgr;
339343
@Inject
340344
ProjectManager _projectMgr;
@@ -1152,15 +1156,10 @@ public IpAddress reserveIpAddress(Account account, Boolean displayIp, Long ipAdd
11521156
if (ipDedicatedAccountId != null && !ipDedicatedAccountId.equals(account.getAccountId())) {
11531157
throw new InvalidParameterValueException("Unable to reserve a IP because it is dedicated to another account.");
11541158
}
1155-
if (ipDedicatedAccountId == null) {
1156-
// Check that the maximum number of public IPs for the given accountId will not be exceeded
1157-
try {
1158-
_resourceLimitMgr.checkResourceLimit(account, Resource.ResourceType.public_ip);
1159-
} catch (ResourceAllocationException ex) {
1160-
logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + account);
1161-
throw new AccountLimitException("Maximum number of public IP addresses for account: " + account.getAccountName() + " has been exceeded.");
1162-
}
1163-
}
1159+
1160+
long reservedIpAddressesAmount = ipDedicatedAccountId == null ? 1L : 0L;
1161+
try (CheckedReservation publicIpAddressReservation = new CheckedReservation(account, Resource.ResourceType.public_ip, reservedIpAddressesAmount, reservationDao, _resourceLimitMgr)) {
1162+
11641163
List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByVlan(ipVO.getVlanId());
11651164
ipVO.setAllocatedTime(new Date());
11661165
ipVO.setAllocatedToAccountId(account.getAccountId());
@@ -1170,10 +1169,15 @@ public IpAddress reserveIpAddress(Account account, Boolean displayIp, Long ipAdd
11701169
ipVO.setDisplay(displayIp);
11711170
}
11721171
ipVO = _ipAddressDao.persist(ipVO);
1173-
if (ipDedicatedAccountId == null) {
1172+
if (reservedIpAddressesAmount > 0) {
11741173
_resourceLimitMgr.incrementResourceCount(account.getId(), Resource.ResourceType.public_ip);
11751174
}
11761175
return ipVO;
1176+
1177+
} catch (ResourceAllocationException ex) {
1178+
logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + account);
1179+
throw new AccountLimitException("Maximum number of public IP addresses for account: " + account.getAccountName() + " has been exceeded.");
1180+
}
11771181
}
11781182

11791183
@Override

server/src/main/java/com/cloud/resourcelimit/CheckedReservation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Objects;
2424
import java.util.stream.Collectors;
2525

26+
import com.cloud.api.ApiDBUtils;
2627
import org.apache.cloudstack.context.CallContext;
2728
import org.apache.cloudstack.reservation.ReservationVO;
2829
import org.apache.cloudstack.reservation.dao.ReservationDao;
@@ -146,6 +147,11 @@ public CheckedReservation(Account account, Long domainId, ResourceType resourceT
146147

147148
this.reservationDao = reservationDao;
148149
this.resourceLimitService = resourceLimitService;
150+
151+
// When allocating to a domain instead of a specific account, consider the system account as the owner for the validations here.
152+
if (account == null) {
153+
account = ApiDBUtils.getSystemAccount();
154+
}
149155
this.account = account;
150156

151157
if (domainId == null) {

0 commit comments

Comments
 (0)