Skip to content

Commit 97b64bf

Browse files
committed
server: show the enforced limit for a domain that has no limit of its own
When a domain has no limit set, the domain response fell back to findDefaultResourceLimitForDomain, which only reads the max.domain.* default for storage types and answers unlimited for the rest. Creating a resource still stops at the default (or at a parent domain's limit), so for example the bucket limit showed unlimited while bucket creation stopped at 20. Use the same lookup that is enforced.
1 parent 8c4e483 commit 97b64bf

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,8 @@ public static long findCorrectResourceLimitForDomain(Long limit, ResourceType re
10051005
if (domainId == Domain.ROOT_DOMAIN) {
10061006
return Resource.RESOURCE_UNLIMITED;
10071007
}
1008-
//--If limit doesn't have a value then fetch default limit from the configs
1009-
return (limit == null) ? s_resourceLimitMgr.findDefaultResourceLimitForDomain(resourceType) : limit;
1008+
//--If limit doesn't have a value then fetch the limit that is enforced: a parent domain's limit or the default from the configs
1009+
return (limit == null) ? findCorrectResourceLimitForDomain(resourceType, domainId) : limit;
10101010
}
10111011

10121012
public static long getResourceCount(ResourceType type, long accountId) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api;
18+
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
22+
import org.junit.After;
23+
import org.junit.Assert;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.junit.MockitoJUnitRunner;
28+
29+
import com.cloud.configuration.Resource;
30+
import com.cloud.configuration.Resource.ResourceType;
31+
import com.cloud.domain.Domain;
32+
import com.cloud.domain.DomainVO;
33+
import com.cloud.domain.dao.DomainDao;
34+
import com.cloud.user.ResourceLimitService;
35+
36+
@RunWith(MockitoJUnitRunner.Silent.class)
37+
public class ApiDBUtilsTest {
38+
39+
private static final long DOMAIN_ID = 2L;
40+
41+
private DomainDao originalDomainDao;
42+
private ResourceLimitService originalResourceLimitMgr;
43+
private DomainVO domain;
44+
private ResourceLimitService resourceLimitMgr;
45+
46+
@Before
47+
public void setUp() {
48+
originalDomainDao = ApiDBUtils.s_domainDao;
49+
originalResourceLimitMgr = ApiDBUtils.s_resourceLimitMgr;
50+
51+
domain = mock(DomainVO.class);
52+
DomainDao domainDao = mock(DomainDao.class);
53+
when(domainDao.findById(DOMAIN_ID)).thenReturn(domain);
54+
resourceLimitMgr = mock(ResourceLimitService.class);
55+
ApiDBUtils.s_domainDao = domainDao;
56+
ApiDBUtils.s_resourceLimitMgr = resourceLimitMgr;
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
ApiDBUtils.s_domainDao = originalDomainDao;
62+
ApiDBUtils.s_resourceLimitMgr = originalResourceLimitMgr;
63+
}
64+
65+
@Test
66+
public void testDomainWithoutLimitGetsTheEnforcedLimit() {
67+
// max.domain.buckets is enforced when the domain has no bucket limit of its own
68+
when(resourceLimitMgr.findCorrectResourceLimitForDomain(domain, ResourceType.bucket, null)).thenReturn(20L);
69+
when(resourceLimitMgr.findDefaultResourceLimitForDomain(ResourceType.bucket)).thenReturn((long) Resource.RESOURCE_UNLIMITED);
70+
71+
Assert.assertEquals(20L, ApiDBUtils.findCorrectResourceLimitForDomain(null, ResourceType.bucket, DOMAIN_ID));
72+
}
73+
74+
@Test
75+
public void testDomainLimitIsUsedWhenSet() {
76+
Assert.assertEquals(7L, ApiDBUtils.findCorrectResourceLimitForDomain(7L, ResourceType.bucket, DOMAIN_ID));
77+
}
78+
79+
@Test
80+
public void testRootDomainIsUnlimited() {
81+
Assert.assertEquals(Resource.RESOURCE_UNLIMITED,
82+
ApiDBUtils.findCorrectResourceLimitForDomain(null, ResourceType.bucket, Domain.ROOT_DOMAIN));
83+
}
84+
}

0 commit comments

Comments
 (0)