Skip to content

Commit 6310991

Browse files
author
Murali Reddy
committed
bug 9419: implement api to reset resource count
added a command to reset resource count for account/domain based on real usage of resources
1 parent 8d56093 commit 6310991

26 files changed

Lines changed: 402 additions & 27 deletions

‎api/src/com/cloud/api/ResponseGenerator.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.cloud.api.response.PodResponse;
4646
import com.cloud.api.response.RemoteAccessVpnResponse;
4747
import com.cloud.api.response.ResourceLimitResponse;
48+
import com.cloud.api.response.ResourceCountResponse;
4849
import com.cloud.api.response.SecurityGroupResponse;
4950
import com.cloud.api.response.ServiceOfferingResponse;
5051
import com.cloud.api.response.SnapshotPolicyResponse;
@@ -63,6 +64,7 @@
6364
import com.cloud.capacity.Capacity;
6465
import com.cloud.configuration.Configuration;
6566
import com.cloud.configuration.ResourceLimit;
67+
import com.cloud.configuration.ResourceCount;
6668
import com.cloud.dc.DataCenter;
6769
import com.cloud.dc.Pod;
6870
import com.cloud.dc.Vlan;
@@ -108,6 +110,8 @@ public interface ResponseGenerator {
108110

109111
ResourceLimitResponse createResourceLimitResponse(ResourceLimit limit);
110112

113+
ResourceCountResponse createResourceCountResponse(ResourceCount resourceCount);
114+
111115
ServiceOfferingResponse createServiceOfferingResponse(ServiceOffering offering);
112116

113117
ConfigurationResponse createConfigurationResponse(Configuration cfg);

‎api/src/com/cloud/api/commands/UpdateResourceCountCmd.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public class UpdateResourceCountCmd extends BaseCmd {
4141
//////////////// API parameters /////////////////////
4242
/////////////////////////////////////////////////////
4343

44-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="Update resource for a specified account. Must be used with the domainId parameter.")
44+
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="Update resource count for a specified account. Must be used with the domainId parameter.")
4545
private String accountName;
4646

47-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="Update resource limits for all accounts in specified domain. If used with the account parameter, updates resource limits for a specified account in specified domain.")
47+
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="Update resource counts for all accounts & child domains in specified domain. If used with the account parameter, updates resource counts for a specified account in specified domain.")
4848
private Long domainId;
4949

5050
@Parameter(name=ApiConstants.RESOURCE_TYPE, type=CommandType.INTEGER, required=true, description="Type of resource to update. Values are 0, 1, 2, 3, and 4. 0 - Instance. Number of instances a user can create. " +

‎api/src/com/cloud/api/response/ResourceCountResponse.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import com.google.gson.annotations.SerializedName;
2222

2323
public class ResourceCountResponse extends BaseResponse {
24-
@SerializedName("account") @Param(description="the account of the resource count")
24+
@SerializedName("account") @Param(description="the account for which resource count's are updated")
2525
private String accountName;
2626

27-
@SerializedName("domainid") @Param(description="the domain ID of the resource count")
27+
@SerializedName("domainid") @Param(description="the domain ID for which resource count's are updated")
2828
private Long domainId;
2929

30-
@SerializedName("domain") @Param(description="the domain name of the resource count")
30+
@SerializedName("domain") @Param(description="the domain name for which resource count's are updated")
3131
private String domainName;
3232

3333
@SerializedName("resourcetype") @Param(description="resource type. Values include 0, 1, 2, 3, 4. See the resourceType parameter for more information on these values.")

‎api/src/com/cloud/user/AccountService.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import com.cloud.api.commands.LockUserCmd;
3333
import com.cloud.api.commands.UpdateAccountCmd;
3434
import com.cloud.api.commands.UpdateResourceLimitCmd;
35+
import com.cloud.api.commands.UpdateResourceCountCmd;
3536
import com.cloud.api.commands.UpdateUserCmd;
3637
import com.cloud.configuration.ResourceLimit;
38+
import com.cloud.configuration.ResourceCount;
3739
import com.cloud.domain.Domain;
3840
import com.cloud.exception.ConcurrentOperationException;
3941
import com.cloud.exception.ResourceUnavailableException;
@@ -141,6 +143,16 @@ public interface AccountService {
141143
*/
142144
ResourceLimit updateResourceLimit(UpdateResourceLimitCmd cmd);
143145

146+
147+
/**
148+
* Updates an existing resource count details for the account/domain
149+
*
150+
* @param cmd
151+
* the command that wraps the domainId, accountId, resource type parameters
152+
* @return the updated/created resource count
153+
*/
154+
ResourceCount updateResourceCount(UpdateResourceCountCmd cmd);
155+
144156
/**
145157
* Search for resource limits for the given id and/or account and/or type and/or domain.
146158
*

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ getCloudIdentifier=com.cloud.api.commands.GetCloudIdentifierCmd;15
3232

3333
#### Limit commands
3434
updateResourceLimit=com.cloud.api.commands.UpdateResourceLimitCmd;7
35+
updateResourceCount=com.cloud.api.commands.UpdateResourceCountCmd;7
3536
listResourceLimits=com.cloud.api.commands.ListResourceLimitsCmd;15
3637

3738
#### VM commands

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.cloud.api.response.PodResponse;
6060
import com.cloud.api.response.RemoteAccessVpnResponse;
6161
import com.cloud.api.response.ResourceLimitResponse;
62+
import com.cloud.api.response.ResourceCountResponse;
6263
import com.cloud.api.response.SecurityGroupResponse;
6364
import com.cloud.api.response.SecurityGroupResultObject;
6465
import com.cloud.api.response.ServiceOfferingResponse;
@@ -82,6 +83,7 @@
8283
import com.cloud.configuration.Configuration;
8384
import com.cloud.configuration.ResourceCount.ResourceType;
8485
import com.cloud.configuration.ResourceLimit;
86+
import com.cloud.configuration.ResourceCount;
8587
import com.cloud.dc.ClusterVO;
8688
import com.cloud.dc.DataCenter;
8789
import com.cloud.dc.DataCenter.NetworkType;
@@ -396,6 +398,28 @@ public ResourceLimitResponse createResourceLimitResponse(ResourceLimit limit) {
396398
return resourceLimitResponse;
397399
}
398400

401+
@Override
402+
public ResourceCountResponse createResourceCountResponse(ResourceCount resourceCount) {
403+
ResourceCountResponse resourceCountResponse = new ResourceCountResponse();
404+
405+
if (resourceCount.getAccountId() != null) {
406+
Account accountTemp = ApiDBUtils.findAccountById(resourceCount.getAccountId());
407+
if (accountTemp != null) {
408+
resourceCountResponse.setAccountName(accountTemp.getAccountName());
409+
resourceCountResponse.setDomainId(accountTemp.getDomainId());
410+
resourceCountResponse.setDomainName(ApiDBUtils.findDomainById(accountTemp.getDomainId()).getName());
411+
}
412+
} else if (resourceCount.getDomainId() != null) {
413+
resourceCountResponse.setDomainId(resourceCount.getDomainId());
414+
resourceCountResponse.setDomainName(ApiDBUtils.findDomainById(resourceCount.getDomainId()).getName());
415+
}
416+
417+
resourceCountResponse.setResourceType(Integer.valueOf(resourceCount.getType().ordinal()).toString());
418+
resourceCountResponse.setResourceCount(resourceCount.getCount());
419+
resourceCountResponse.setObjectName("resourcecount");
420+
return resourceCountResponse;
421+
}
422+
399423
@Override
400424
public ServiceOfferingResponse createServiceOfferingResponse(ServiceOffering offering) {
401425
ServiceOfferingResponse offeringResponse = new ServiceOfferingResponse();

‎server/src/com/cloud/configuration/dao/ResourceCountDao.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ public interface ResourceCountDao extends GenericDao<ResourceCountVO, Long> {
4040
*/
4141
public long getDomainCount(long domainId, ResourceType type);
4242

43+
/**
44+
* Set the count of in use resources for an account by type
45+
* @param accountId the id of the account to set the resource count
46+
* @param type the type of resource (e.g. user_vm, public_ip, volume)
47+
* @param the count of resources in use for the given type and account
48+
*/
49+
public void setAccountCount(long accountId, ResourceType type, long count);
50+
51+
/**
52+
* Get the count of in use resources for a domain by type
53+
* @param domainId the id of the domain to set the resource count
54+
* @param type the type of resource (e.g. user_vm, public_ip, volume)
55+
* @param the count of resources in use for the given type and domain
56+
*/
57+
public void setDomainCount(long domainId, ResourceType type, long count);
58+
4359
/**
4460
* Update the count of resources in use for the given account and given resource type
4561
* @param accountId the id of the account to update resource count

‎server/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ public long getDomainCount(long domainId, ResourceType type) {
7979
return (resourceCountVO != null) ? resourceCountVO.getCount() : 0;
8080
}
8181

82+
@Override
83+
public void setAccountCount(long accountId, ResourceType type, long count) {
84+
ResourceCountVO resourceCountVO = findByAccountIdAndType(accountId, type);
85+
if (resourceCountVO == null) {
86+
if (count != 0) {
87+
resourceCountVO = new ResourceCountVO(accountId, null, type, count);
88+
persist(resourceCountVO);
89+
}
90+
} else {
91+
resourceCountVO.setCount(count);
92+
update(resourceCountVO.getId(), resourceCountVO);
93+
}
94+
}
95+
96+
@Override
97+
public void setDomainCount(long domainId, ResourceType type, long count) {
98+
ResourceCountVO resourceCountVO = findByDomainIdAndType(domainId, type);
99+
if (resourceCountVO == null) {
100+
if (count != 0) {
101+
resourceCountVO = new ResourceCountVO(null, domainId, type, count);
102+
persist(resourceCountVO);
103+
}
104+
} else {
105+
resourceCountVO.setCount(count);
106+
update(resourceCountVO.getId(), resourceCountVO);
107+
}
108+
}
109+
82110
@Override
83111
public void updateAccountCount(long accountId, ResourceType type, boolean increment, long delta) {
84112
delta = increment ? delta : delta * -1;

‎server/src/com/cloud/domain/dao/DomainDao.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ public interface DomainDao extends GenericDao<DomainVO, Long> {
2929
public DomainVO findDomainByPath(String domainPath);
3030
public boolean isChildDomain(Long parentId, Long childId);
3131
DomainVO findImmediateChildForParent(Long parentId);
32+
List<DomainVO> findImmediateChildrenForParent(Long parentId);
3233
List<DomainVO> findAllChildren(String path, Long parentId);
3334
}

‎server/src/com/cloud/domain/dao/DomainDaoImpl.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public DomainDaoImpl () {
6666
FindAllChildrenSearch = createSearchBuilder();
6767
FindAllChildrenSearch.and("path", FindAllChildrenSearch.entity().getPath(), SearchCriteria.Op.LIKE);
6868
FindAllChildrenSearch.and("id", FindAllChildrenSearch.entity().getId(), SearchCriteria.Op.NEQ);
69-
FindAllChildrenSearch.done();
69+
FindAllChildrenSearch.done();
70+
7071
}
7172

7273
public void update(Long id, String domainName, String domainPath) {
@@ -196,14 +197,21 @@ public DomainVO findDomainByPath(String domainPath) {
196197
sc.addAnd("path", SearchCriteria.Op.EQ, domainPath);
197198
return findOneBy(sc);
198199
}
199-
200+
200201
@Override
201202
public DomainVO findImmediateChildForParent(Long parentId){
202203
SearchCriteria<DomainVO> sc = ImmediateChildDomainSearch.create();
203204
sc.setParameters("parent", parentId);
204205
return (listBy(sc).size() > 0 ? listBy(sc).get(0) : null);//may need to revisit for multiple children case
205206
}
206-
207+
208+
@Override
209+
public List<DomainVO> findImmediateChildrenForParent(Long parentId){
210+
SearchCriteria<DomainVO> sc = ImmediateChildDomainSearch.create();
211+
sc.setParameters("parent", parentId);
212+
return listBy(sc);
213+
}
214+
207215
@Override
208216
public List<DomainVO> findAllChildren(String path, Long parentId){
209217
SearchCriteria<DomainVO> sc = FindAllChildrenSearch.create();

0 commit comments

Comments
 (0)