Skip to content

Commit 5828e52

Browse files
sanjaytripathiminchen07
authored andcommitted
CLOUDSTACK-713: Limit Resources(CPU and Memory) to domain/accounts
Addition of two new resource types i.e. CPU and Memory in the existing pool of resource types. Added some methods to set the limits on these resources using updateResourceLimit API command and to get a count using updateResourceCount. Also added calls in the Virtual machine life cycle to check these limits and to increment/decrement the new resource types Resource Name :: Resource type number CPU 8 Memory 9 Also added Unit Tests for the same.
1 parent 1e24892 commit 5828e52

16 files changed

Lines changed: 395 additions & 42 deletions

File tree

api/src/com/cloud/configuration/Resource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public enum ResourceType {
2828
template("template", 4, ResourceOwnerType.Account, ResourceOwnerType.Domain),
2929
project("project", 5, ResourceOwnerType.Account, ResourceOwnerType.Domain),
3030
network("network", 6, ResourceOwnerType.Account, ResourceOwnerType.Domain),
31-
vpc("vpc", 7, ResourceOwnerType.Account, ResourceOwnerType.Domain);
31+
vpc("vpc", 7, ResourceOwnerType.Account, ResourceOwnerType.Domain),
32+
cpu("cpu", 8, ResourceOwnerType.Account, ResourceOwnerType.Domain),
33+
memory("memory", 9, ResourceOwnerType.Account, ResourceOwnerType.Domain);
3234

3335
private String name;
3436
private ResourceOwnerType[] supportedOwners;

api/src/org/apache/cloudstack/api/command/user/resource/UpdateResourceCountCmd.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@ public class UpdateResourceCountCmd extends BaseCmd {
5353
required=true, description="If account parameter specified then updates resource counts for a specified account in this domain else update resource counts for all accounts & child domains in specified domain.")
5454
private Long domainId;
5555

56-
@Parameter(name=ApiConstants.RESOURCE_TYPE, type=CommandType.INTEGER, description= "Type of resource to update. If specifies valid values are 0, 1, 2, 3, and 4. If not specified will update all resource counts" +
56+
@Parameter(name=ApiConstants.RESOURCE_TYPE, type=CommandType.INTEGER, description= "Type of resource to update. If specifies valid values are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. If not specified will update all resource counts" +
5757
"0 - Instance. Number of instances a user can create. " +
5858
"1 - IP. Number of public IP addresses a user can own. " +
5959
"2 - Volume. Number of disk volumes a user can create." +
6060
"3 - Snapshot. Number of snapshots a user can create." +
61-
"4 - Template. Number of templates that a user can register/create.")
61+
"4 - Template. Number of templates that a user can register/create." +
62+
"5 - Project. Number of projects that a user can create." +
63+
"6 - Network. Number of guest network a user can create." +
64+
"7 - VPC. Number of VPC a user can create." +
65+
"8 - CPU. Total number of CPU cores a user can use." +
66+
"9 - Memory. Total Memory (in MB) a user can use." )
6267
private Integer resourceType;
6368

6469
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.UUID, entityType = ProjectResponse.class,

api/src/org/apache/cloudstack/api/command/user/resource/UpdateResourceLimitCmd.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ public class UpdateResourceLimitCmd extends BaseCmd {
5454
@Parameter(name=ApiConstants.MAX, type=CommandType.LONG, description=" Maximum resource limit.")
5555
private Long max;
5656

57-
@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. " +
57+
@Parameter(name=ApiConstants.RESOURCE_TYPE, type=CommandType.INTEGER, required=true, description="Type of resource to update. Values are 0, 1, 2, 3, 4, 6, 7, 8 and 9. 0 - Instance. Number of instances a user can create. " +
5858
"1 - IP. Number of public IP addresses a user can own. " +
5959
"2 - Volume. Number of disk volumes a user can create." +
6060
"3 - Snapshot. Number of snapshots a user can create." +
61-
"4 - Template. Number of templates that a user can register/create.")
61+
"4 - Template. Number of templates that a user can register/create." +
62+
"6 - Network. Number of guest network a user can create." +
63+
"7 - VPC. Number of VPC a user can create." +
64+
"8 - CPU. Total number of CPU cores a user can use." +
65+
"9 - Memory. Total Memory (in MB) a user can use." )
6266
private Integer resourceType;
6367

6468
/////////////////////////////////////////////////////

api/src/org/apache/cloudstack/api/response/AccountResponse.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ public class AccountResponse extends BaseResponse {
132132
@SerializedName("vpcavailable") @Param(description="the total number of vpcs available to be created for this account", since="4.0.0")
133133
private String vpcAvailable;
134134

135+
@SerializedName("cpulimit") @Param(description="the total number of cpu cores the account can own", since="4.1.0")
136+
private String cpuLimit;
137+
138+
@SerializedName("cputotal") @Param(description="the total number of cpu cores owned by account", since="4.1.0")
139+
private Long cpuTotal;
140+
141+
@SerializedName("cpuavailable") @Param(description="the total number of cpu cores available to be created for this account", since="4.1.0")
142+
private String cpuAvailable;
143+
144+
@SerializedName("memorylimit") @Param(description="the total memory (in MB) the account can own", since="4.1.0")
145+
private String memoryLimit;
146+
147+
@SerializedName("memorytotal") @Param(description="the total memory (in MB) owned by account", since="4.1.0")
148+
private Long memoryTotal;
149+
150+
@SerializedName("memoryavailable") @Param(description="the total memory (in MB) available to be created for this account", since="4.1.0")
151+
private String memoryAvailable;
152+
135153

136154
@SerializedName(ApiConstants.STATE) @Param(description="the state of the account")
137155
private String state;
@@ -294,6 +312,30 @@ public void setNetworkAvailable(String networkAvailable) {
294312
this.networkAvailable = networkAvailable;
295313
}
296314

315+
public void setCpuLimit(String cpuLimit) {
316+
this.cpuLimit = cpuLimit;
317+
}
318+
319+
public void setCpuTotal(Long cpuTotal) {
320+
this.cpuTotal = cpuTotal;
321+
}
322+
323+
public void setCpuAvailable(String cpuAvailable) {
324+
this.cpuAvailable = cpuAvailable;
325+
}
326+
327+
public void setMemoryLimit(String memoryLimit) {
328+
this.memoryLimit = memoryLimit;
329+
}
330+
331+
public void setMemoryTotal(Long memoryTotal) {
332+
this.memoryTotal = memoryTotal;
333+
}
334+
335+
public void setMemoryAvailable(String memoryAvailable) {
336+
this.memoryAvailable = memoryAvailable;
337+
}
338+
297339
public void setDefaultZone(String defaultZoneId) {
298340
this.defaultZoneId = defaultZoneId;
299341
}

api/src/org/apache/cloudstack/api/response/ResourceCountResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ResourceCountResponse extends BaseResponse implements ControlledEnt
4040
@SerializedName(ApiConstants.DOMAIN) @Param(description="the domain name for which resource count's are updated")
4141
private String domainName;
4242

43-
@SerializedName(ApiConstants.RESOURCE_TYPE) @Param(description="resource type. Values include 0, 1, 2, 3, 4. See the resourceType parameter for more information on these values.")
43+
@SerializedName(ApiConstants.RESOURCE_TYPE) @Param(description="resource type. Values include 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. See the resourceType parameter for more information on these values.")
4444
private String resourceType;
4545

4646
@SerializedName("resourcecount") @Param(description="resource count")

api/src/org/apache/cloudstack/api/response/ResourceLimitResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ResourceLimitResponse extends BaseResponse implements ControlledEnt
3636
@SerializedName(ApiConstants.DOMAIN) @Param(description="the domain name of the resource limit")
3737
private String domainName;
3838

39-
@SerializedName(ApiConstants.RESOURCE_TYPE) @Param(description="resource type. Values include 0, 1, 2, 3, 4. See the resourceType parameter for more information on these values.")
39+
@SerializedName(ApiConstants.RESOURCE_TYPE) @Param(description="resource type. Values include 0, 1, 2, 3, 4, 6, 7, 8, 9. See the resourceType parameter for more information on these values.")
4040
private String resourceType;
4141

4242
@SerializedName("max") @Param(description="the maximum number of the resource. A -1 means the resource currently has no limit.")

server/src/com/cloud/api/query/dao/AccountJoinDaoImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ public AccountResponse newAccountResponse(AccountJoinVO account) {
157157
accountResponse.setNetworkTotal(vpcTotal);
158158
accountResponse.setNetworkAvailable(vpcAvail);
159159

160+
//get resource limits for cpu cores
161+
long cpuLimit = ApiDBUtils.findCorrectResourceLimit(account.getCpuLimit(), account.getType(), ResourceType.cpu);
162+
String cpuLimitDisplay = (accountIsAdmin || cpuLimit == -1) ? "Unlimited" : String.valueOf(cpuLimit);
163+
long cpuTotal = (account.getCpuTotal() == null) ? 0 : account.getCpuTotal();
164+
String cpuAvail = (accountIsAdmin || cpuLimit == -1) ? "Unlimited" : String.valueOf(cpuLimit - cpuTotal);
165+
accountResponse.setCpuLimit(cpuLimitDisplay);
166+
accountResponse.setCpuTotal(cpuTotal);
167+
accountResponse.setCpuAvailable(cpuAvail);
168+
169+
//get resource limits for memory
170+
long memoryLimit = ApiDBUtils.findCorrectResourceLimit(account.getMemoryLimit(), account.getType(), ResourceType.memory);
171+
String memoryLimitDisplay = (accountIsAdmin || memoryLimit == -1) ? "Unlimited" : String.valueOf(memoryLimit);
172+
long memoryTotal = (account.getMemoryTotal() == null) ? 0 : account.getMemoryTotal();
173+
String memoryAvail = (accountIsAdmin || memoryLimit == -1) ? "Unlimited" : String.valueOf(memoryLimit - memoryTotal);
174+
accountResponse.setMemoryLimit(memoryLimitDisplay);
175+
accountResponse.setMemoryTotal(memoryTotal);
176+
accountResponse.setMemoryAvailable(memoryAvail);
177+
160178
// adding all the users for an account as part of the response obj
161179
List<UserAccountJoinVO> usersForAccount = ApiDBUtils.findUserViewByAccountId(account.getId());
162180
List<UserResponse> userResponses = ViewResponseHelper.createUserResponse(usersForAccount.toArray(new UserAccountJoinVO[usersForAccount.size()]));

server/src/com/cloud/api/query/vo/AccountJoinVO.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ public class AccountJoinVO extends BaseViewVO implements InternalIdentity, Ident
148148
@Column(name="vpcTotal")
149149
private Long vpcTotal;
150150

151+
152+
@Column(name="cpuLimit")
153+
private Long cpuLimit;
154+
155+
@Column(name="cpuTotal")
156+
private Long cpuTotal;
157+
158+
159+
@Column(name="memoryLimit")
160+
private Long memoryLimit;
161+
162+
@Column(name="memoryTotal")
163+
private Long memoryTotal;
164+
151165
@Column(name="job_id")
152166
private long jobId;
153167

@@ -445,7 +459,6 @@ public void setNetworkTotal(Long networkTotal) {
445459
}
446460

447461

448-
449462
public Long getVpcTotal() {
450463
return vpcTotal;
451464
}
@@ -456,6 +469,25 @@ public void setVpcTotal(Long vpcTotal) {
456469
}
457470

458471

472+
public Long getCpuTotal() {
473+
return cpuTotal;
474+
}
475+
476+
477+
public void setCpuTotal(Long cpuTotal) {
478+
this.cpuTotal = cpuTotal;
479+
}
480+
481+
public Long getMemoryTotal() {
482+
return memoryTotal;
483+
}
484+
485+
486+
public void setMemoryTotal(Long memoryTotal) {
487+
this.memoryTotal = memoryTotal;
488+
}
489+
490+
459491
public Long getVmLimit() {
460492
return vmLimit;
461493
}
@@ -536,6 +568,26 @@ public void setVpcLimit(Long vpcLimit) {
536568
}
537569

538570

571+
public Long getCpuLimit() {
572+
return cpuLimit;
573+
}
574+
575+
576+
public void setCpuLimit(Long cpuLimit) {
577+
this.cpuLimit = cpuLimit;
578+
}
579+
580+
581+
public Long getMemoryLimit() {
582+
return memoryLimit;
583+
}
584+
585+
586+
public void setMemoryLimit(Long memoryLimit) {
587+
this.memoryLimit = memoryLimit;
588+
}
589+
590+
539591
public long getJobId() {
540592
return jobId;
541593
}

server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
239239
_configMgr.checkZoneAccess(owner, dc);
240240
}
241241

242-
// check if account/domain is with in resource limits to create a new vm
243-
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.user_vm);
244-
245242
ServiceOfferingVO offering = _serviceOfferingDao.findById(cmd.getServiceOfferingId());
246243
if (offering == null || offering.getRemoved() != null) {
247244
throw new InvalidParameterValueException("Unable to find service offering: " + cmd.getServiceOfferingId());
248245
}
249-
246+
247+
// check if account/domain is with in resource limits to create a new vm
248+
resourceLimitCheck(owner, new Long(offering.getCpu()), new Long(offering.getRamSize()));
249+
250250
VMTemplateVO template = _templateDao.findById(cmd.getTemplateId());
251251
// Make sure a valid template ID was specified
252252
if (template == null || template.getRemoved() != null) {
@@ -362,7 +362,7 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
362362
vm.getHostName(), offering.getId(), template.getId(), HypervisorType.BareMetal.toString(),
363363
VirtualMachine.class.getName(), vm.getUuid());
364364

365-
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.user_vm);
365+
resourceCountIncrement(accountId, new Long(offering.getCpu()), new Long(offering.getRamSize()));
366366

367367
// Assign instance to the group
368368
try {

server/src/com/cloud/configuration/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ public enum Config {
308308
DefaultMaxAccountVolumes("Account Defaults", ManagementServer.class, Long.class, "max.account.volumes", "20", "The default maximum number of volumes that can be created for an account", null),
309309
DefaultMaxAccountNetworks("Account Defaults", ManagementServer.class, Long.class, "max.account.networks", "20", "The default maximum number of networks that can be created for an account", null),
310310
DefaultMaxAccountVpcs("Account Defaults", ManagementServer.class, Long.class, "max.account.vpcs", "20", "The default maximum number of vpcs that can be created for an account", null),
311+
DefaultMaxAccountCpus("Account Defaults", ManagementServer.class, Long.class, "max.account.cpus", "40", "The default maximum number of cpu cores that can be used for an account", null),
312+
DefaultMaxAccountMemory("Account Defaults", ManagementServer.class, Long.class, "max.account.memory", "40960", "The default maximum memory (in MB) that can be used for an account", null),
311313

312314

313315
ResourceCountCheckInterval("Advanced", ManagementServer.class, Long.class, "resourcecount.check.interval", "0", "Time (in seconds) to wait before retrying resource count check task. Default is 0 which is to never run the task", "Seconds"),
@@ -332,6 +334,8 @@ public enum Config {
332334
DefaultMaxProjectVolumes("Project Defaults", ManagementServer.class, Long.class, "max.project.volumes", "20", "The default maximum number of volumes that can be created for a project", null),
333335
DefaultMaxProjectNetworks("Project Defaults", ManagementServer.class, Long.class, "max.project.networks", "20", "The default maximum number of networks that can be created for a project", null),
334336
DefaultMaxProjectVpcs("Project Defaults", ManagementServer.class, Long.class, "max.project.vpcs", "20", "The default maximum number of vpcs that can be created for a project", null),
337+
DefaultMaxProjectCpus("Project Defaults", ManagementServer.class, Long.class, "max.project.cpus", "40", "The default maximum number of cpu cores that can be used for a project", null),
338+
DefaultMaxProjectMemory("Project Defaults", ManagementServer.class, Long.class, "max.project.memory", "40960", "The default maximum memory (in MB) that can be used for a project", null),
335339

336340
ProjectInviteRequired("Project Defaults", ManagementServer.class, Boolean.class, "project.invite.required", "false", "If invitation confirmation is required when add account to project. Default value is false", null),
337341
ProjectInvitationExpirationTime("Project Defaults", ManagementServer.class, Long.class, "project.invite.timeout", "86400", "Invitation expiration time (in seconds). Default is 1 day - 86400 seconds", null),

0 commit comments

Comments
 (0)