mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Routed: fix create network exception when auto-allocation is disabled (#11624)
* Routed: fix create network exception when auto-allocation is disabled for regular users * routed: throw InvalidParameterValueException instead of CloudRuntimeException which gives vague message to regular users
This commit is contained in:
parent
6f931dbd00
commit
4327871036
@ -607,10 +607,20 @@ public class RoutedIpv4ManagerImpl extends ComponentLifecycleBase implements Rou
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Ipv4GuestSubnetNetworkMap getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(Integer cidrSize, Long ownerDomainId, Long ownerAccountId, Long zoneId) {
|
protected Ipv4GuestSubnetNetworkMap getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(Integer cidrSize, Long ownerDomainId, Long ownerAccountId, Long zoneId) {
|
||||||
validateNetworkCidrSize(ownerAccountId, cidrSize);
|
validateNetworkCidrSize(cidrSize);
|
||||||
List<DataCenterIpv4GuestSubnetVO> subnets = getZoneSubnetsForAccount(ownerDomainId, ownerAccountId, zoneId);
|
List<DataCenterIpv4GuestSubnetVO> subnets = getZoneSubnetsForAccount(ownerDomainId, ownerAccountId, zoneId);
|
||||||
for (DataCenterIpv4GuestSubnetVO subnet : subnets) {
|
for (DataCenterIpv4GuestSubnetVO subnet : subnets) {
|
||||||
Ipv4GuestSubnetNetworkMap result = getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, subnet);
|
Ipv4GuestSubnetNetworkMap result = getIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, subnet);
|
||||||
|
if (result != null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Boolean isAutoAllocationEnabled = RoutedIPv4NetworkCidrAutoAllocationEnabled.valueIn(ownerAccountId);
|
||||||
|
if (!Boolean.TRUE.equals(isAutoAllocationEnabled)) {
|
||||||
|
throw new InvalidParameterValueException("CIDR auto-allocation is disabled for this account");
|
||||||
|
}
|
||||||
|
for (DataCenterIpv4GuestSubnetVO subnet : subnets) {
|
||||||
|
Ipv4GuestSubnetNetworkMap result = createIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, subnet);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -618,11 +628,11 @@ public class RoutedIpv4ManagerImpl extends ComponentLifecycleBase implements Rou
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Ipv4GuestSubnetNetworkMap getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(Integer cidrSize, DataCenterIpv4GuestSubnetVO subnet) {
|
protected Ipv4GuestSubnetNetworkMap getIpv4SubnetForGuestNetworkOrVpcInternal(Integer cidrSize, DataCenterIpv4GuestSubnetVO subnet) {
|
||||||
Ipv4GuestSubnetNetworkMap map = ipv4GuestSubnetNetworkMapDao.findFirstAvailable(subnet.getId(), cidrSize);
|
return ipv4GuestSubnetNetworkMapDao.findFirstAvailable(subnet.getId(), cidrSize);
|
||||||
if (map != null) {
|
}
|
||||||
return map;
|
|
||||||
}
|
protected Ipv4GuestSubnetNetworkMap createIpv4SubnetForGuestNetworkOrVpcInternal(Integer cidrSize, DataCenterIpv4GuestSubnetVO subnet) {
|
||||||
try {
|
try {
|
||||||
return createIpv4SubnetFromParentSubnet(subnet, cidrSize);
|
return createIpv4SubnetFromParentSubnet(subnet, cidrSize);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
@ -631,6 +641,14 @@ public class RoutedIpv4ManagerImpl extends ComponentLifecycleBase implements Rou
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Ipv4GuestSubnetNetworkMap getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(Integer cidrSize, DataCenterIpv4GuestSubnetVO subnet) {
|
||||||
|
Ipv4GuestSubnetNetworkMap map = getIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, subnet);
|
||||||
|
if (map != null) {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
return createIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, subnet);
|
||||||
|
}
|
||||||
|
|
||||||
protected void getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(String networkCidr, Long ownerDomainId, Long ownerAccountId, Long zoneId) {
|
protected void getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(String networkCidr, Long ownerDomainId, Long ownerAccountId, Long zoneId) {
|
||||||
Ipv4GuestSubnetNetworkMapVO subnetMap = ipv4GuestSubnetNetworkMapDao.findBySubnet(networkCidr);
|
Ipv4GuestSubnetNetworkMapVO subnetMap = ipv4GuestSubnetNetworkMapDao.findBySubnet(networkCidr);
|
||||||
if (subnetMap != null) {
|
if (subnetMap != null) {
|
||||||
@ -693,13 +711,9 @@ public class RoutedIpv4ManagerImpl extends ComponentLifecycleBase implements Rou
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateNetworkCidrSize(long accountId, Integer networkCidrSize) {
|
private void validateNetworkCidrSize(Integer networkCidrSize) {
|
||||||
if (networkCidrSize == null) {
|
if (networkCidrSize == null) {
|
||||||
throw new CloudRuntimeException("network/vpc CidrSize is null");
|
throw new InvalidParameterValueException("network/vpc CidrSize is null");
|
||||||
}
|
|
||||||
Boolean isAutoAllocationEnabled = RoutedIPv4NetworkCidrAutoAllocationEnabled.valueIn(accountId);
|
|
||||||
if (!Boolean.TRUE.equals(isAutoAllocationEnabled)) {
|
|
||||||
throw new CloudRuntimeException("CIDR auto-allocation is disabled for this account");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -755,7 +769,7 @@ public class RoutedIpv4ManagerImpl extends ComponentLifecycleBase implements Rou
|
|||||||
// Allocate a subnet automatically
|
// Allocate a subnet automatically
|
||||||
String networkCidr = getFreeNetworkCidr(subnetsInFreeIpRanges, networkCidrSize);
|
String networkCidr = getFreeNetworkCidr(subnetsInFreeIpRanges, networkCidrSize);
|
||||||
if (networkCidr == null) {
|
if (networkCidr == null) {
|
||||||
throw new CloudRuntimeException("Failed to automatically allocate a subnet with specified cidrsize");
|
throw new InvalidParameterValueException("Failed to automatically allocate a subnet with specified cidrsize");
|
||||||
}
|
}
|
||||||
return networkCidr;
|
return networkCidr;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -545,12 +545,12 @@ public class RoutedIpv4ManagerImplTest {
|
|||||||
DataCenterIpv4GuestSubnetVO subnet3 = Mockito.mock(DataCenterIpv4GuestSubnetVO.class);
|
DataCenterIpv4GuestSubnetVO subnet3 = Mockito.mock(DataCenterIpv4GuestSubnetVO.class);
|
||||||
when(dataCenterIpv4GuestSubnetDao.listNonDedicatedByDataCenterId(zoneId)).thenReturn(Arrays.asList(subnet3));
|
when(dataCenterIpv4GuestSubnetDao.listNonDedicatedByDataCenterId(zoneId)).thenReturn(Arrays.asList(subnet3));
|
||||||
|
|
||||||
doReturn(null).doReturn(null).doReturn(ipv4GuestSubnetNetworkMap).when(routedIpv4Manager).getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(eq(cidrSize), any());
|
doReturn(null).doReturn(null).doReturn(ipv4GuestSubnetNetworkMap).when(routedIpv4Manager).getIpv4SubnetForGuestNetworkOrVpcInternal(eq(cidrSize), any());
|
||||||
|
|
||||||
Ipv4GuestSubnetNetworkMap result = routedIpv4Manager.getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, domainId, accountId, zoneId);
|
Ipv4GuestSubnetNetworkMap result = routedIpv4Manager.getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(cidrSize, domainId, accountId, zoneId);
|
||||||
|
|
||||||
Assert.assertEquals(ipv4GuestSubnetNetworkMap, result);
|
Assert.assertEquals(ipv4GuestSubnetNetworkMap, result);
|
||||||
verify(routedIpv4Manager, times(3)).getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal(eq(cidrSize), any());
|
verify(routedIpv4Manager, times(3)).getIpv4SubnetForGuestNetworkOrVpcInternal(eq(cidrSize), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user