Skip to content

Commit 6062c47

Browse files
committed
API: improve resource limits comprenhension
Add resource type name in request and response for listResources API call.
1 parent 88cd182 commit 6062c47

7 files changed

Lines changed: 57 additions & 16 deletions

File tree

api/src/com/cloud/user/ResourceLimitService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public interface ResourceLimitService {
6464
* TODO
6565
* @param domainId
6666
* TODO
67-
* @param type
67+
* @param resourceType
6868
* TODO
6969
* @return a list of limits that match the criteria
7070
*/
71-
public List<? extends ResourceLimit> searchForLimits(Long id, Long accountId, Long domainId, Integer type, Long startIndex, Long pageSizeVal);
71+
public List<? extends ResourceLimit> searchForLimits(Long id, Long accountId, Long domainId, ResourceType resourceType, Long startIndex, Long pageSizeVal);
7272

7373
/**
7474
* Finds the resource limit for a specified account and type. If the account has an infinite limit, will check

api/src/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public class ApiConstants {
216216
public static final String RECEIVED_BYTES = "receivedbytes";
217217
public static final String REQUIRES_HVM = "requireshvm";
218218
public static final String RESOURCE_TYPE = "resourcetype";
219+
public static final String RESOURCE_TYPE_NAME = "resourcetypename";
219220
public static final String RESPONSE = "response";
220221
public static final String REVERTABLE = "revertable";
221222
public static final String REGISTERED = "registered";

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import com.cloud.configuration.Resource;
23+
import com.cloud.exception.InvalidParameterValueException;
2224
import org.apache.cloudstack.api.APICommand;
2325
import org.apache.cloudstack.api.ApiConstants;
2426
import org.apache.cloudstack.api.BaseListProjectAndAccountResourcesCmd;
@@ -58,6 +60,21 @@ public class ListResourceLimitsCmd extends BaseListProjectAndAccountResourcesCmd
5860
+ "11 - SecondaryStorage. Total secondary storage space (in GiB) a user can use. ")
5961
private Integer resourceType;
6062

63+
@Parameter(name = ApiConstants.RESOURCE_TYPE_NAME, type = CommandType.STRING, description = "Type of resource (wins over resourceType if both are provided). Values are: "
64+
+ "user_vm - Instance. Number of instances a user can create. "
65+
+ "public_ip - IP. Number of public IP addresses an account can own. "
66+
+ "volume - Volume. Number of disk volumes an account can own. "
67+
+ "snapshot - Snapshot. Number of snapshots an account can own. "
68+
+ "template - Template. Number of templates an account can register/create. "
69+
+ "project - Project. Number of projects an account can own. "
70+
+ "network - Network. Number of networks an account can own. "
71+
+ "vpc - VPC. Number of VPC an account can own. "
72+
+ "cpu - CPU. Number of CPU an account can allocate for his resources. "
73+
+ "memory - Memory. Amount of RAM an account can allocate for his resources. "
74+
+ "primary_storage - PrimaryStorage. Total primary storage space (in GiB) a user can use. "
75+
+ "secondary_storage - SecondaryStorage. Total secondary storage space (in GiB) a user can use. ")
76+
private String resourceTypeName;
77+
6178
/////////////////////////////////////////////////////
6279
/////////////////// Accessors ///////////////////////
6380
/////////////////////////////////////////////////////
@@ -70,6 +87,10 @@ public Integer getResourceType() {
7087
return resourceType;
7188
}
7289

90+
public String getResourceTypeName() {
91+
return resourceTypeName;
92+
}
93+
7394
/////////////////////////////////////////////////////
7495
/////////////// API Implementation///////////////////
7596
/////////////////////////////////////////////////////
@@ -83,7 +104,7 @@ public String getCommandName() {
83104
public void execute() {
84105
List<? extends ResourceLimit> result =
85106
_resourceLimitService.searchForLimits(id, _accountService.finalyzeAccountId(this.getAccountName(), this.getDomainId(), this.getProjectId(), false), this.getDomainId(),
86-
resourceType, this.getStartIndex(), this.getPageSizeVal());
107+
getResourceTypeEnum(), this.getStartIndex(), this.getPageSizeVal());
87108
ListResponse<ResourceLimitResponse> response = new ListResponse<ResourceLimitResponse>();
88109
List<ResourceLimitResponse> limitResponses = new ArrayList<ResourceLimitResponse>();
89110
for (ResourceLimit limit : result) {
@@ -96,4 +117,24 @@ public void execute() {
96117
response.setResponseName(getCommandName());
97118
this.setResponseObject(response);
98119
}
120+
121+
private Resource.ResourceType getResourceTypeEnum() {
122+
// Map resource type
123+
Resource.ResourceType resourceTypeResult = null;
124+
if (resourceTypeName != null) {
125+
try {
126+
resourceTypeResult = Resource.ResourceType.valueOf(resourceTypeName);
127+
} catch (IllegalArgumentException e) {
128+
throw new InvalidParameterValueException("Please specify a valid resource type name.");
129+
}
130+
} else if (resourceType != null) {
131+
try {
132+
resourceTypeResult = Resource.ResourceType.values()[resourceType];
133+
} catch (ArrayIndexOutOfBoundsException e) {
134+
throw new InvalidParameterValueException("Please specify a valid resource type.");
135+
}
136+
}
137+
138+
return resourceTypeResult;
139+
}
99140
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class ResourceLimitResponse extends BaseResponse implements ControlledEnt
4444
@Param(description = "resource type. Values include 0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11. See the resourceType parameter for more information on these values.")
4545
private String resourceType;
4646

47+
@SerializedName(ApiConstants.RESOURCE_TYPE_NAME)
48+
@Param(description = "resource type name. Values include user_vm, public_ip, volume, snapshot, template, project, network, vpc, cpu, memory, primary_storage, secondary_storage.")
49+
private String resourceTypeName;
50+
4751
@SerializedName("max")
4852
@Param(description = "the maximum number of the resource. A -1 means the resource currently has no limit.")
4953
private Long max;
@@ -80,6 +84,10 @@ public void setResourceType(String resourceType) {
8084
this.resourceType = resourceType;
8185
}
8286

87+
public void setResourceTypeName(String resourceTypeName) {
88+
this.resourceTypeName = resourceTypeName;
89+
}
90+
8391
public void setMax(Long max) {
8492
this.max = max;
8593
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ public ResourceLimitResponse createResourceLimitResponse(ResourceLimit limit) {
412412
populateDomain(resourceLimitResponse, accountTemp.getDomainId());
413413
}
414414
resourceLimitResponse.setResourceType(Integer.toString(limit.getType().getOrdinal()));
415+
resourceLimitResponse.setResourceTypeName(limit.getType().getName());
415416

416417
if ((limit.getType() == ResourceType.primary_storage || limit.getType() == ResourceType.secondary_storage) && limit.getMax() >= 0) {
417418
resourceLimitResponse.setMax((long)Math.ceil((double)limit.getMax() / ResourceType.bytesToGiB));

server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) throws Resour
477477
}
478478

479479
@Override
480-
public List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domainId, Integer type, Long startIndex, Long pageSizeVal) {
480+
public List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domainId, ResourceType resourceType, Long startIndex, Long pageSizeVal) {
481481
Account caller = CallContext.current().getCallingAccount();
482482
List<ResourceLimitVO> limits = new ArrayList<ResourceLimitVO>();
483483
boolean isAccount = true;
@@ -510,16 +510,6 @@ public List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domai
510510
}
511511
}
512512

513-
// Map resource type
514-
ResourceType resourceType = null;
515-
if (type != null) {
516-
try {
517-
resourceType = ResourceType.values()[type];
518-
} catch (ArrayIndexOutOfBoundsException e) {
519-
throw new InvalidParameterValueException("Please specify a valid resource type.");
520-
}
521-
}
522-
523513
// If id is passed in, get the record and return it if permission check has passed
524514
if (id != null) {
525515
ResourceLimitVO vo = _resourceLimitDao.findById(id);

server/test/com/cloud/vpc/MockResourceLimitManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public List<? extends ResourceCount> recalculateResourceCount(Long accountId, Lo
5454
}
5555

5656
/* (non-Javadoc)
57-
* @see com.cloud.user.ResourceLimitService#searchForLimits(java.lang.Long, java.lang.Long, java.lang.Long, java.lang.Integer, java.lang.Long, java.lang.Long)
57+
* @see com.cloud.user.ResourceLimitService#searchForLimits(java.lang.Long, java.lang.Long, java.lang.Long, com.cloud.user.ResourceLimitService, java.lang.Long, java.lang.Long)
5858
*/
5959
@Override
60-
public List<? extends ResourceLimit> searchForLimits(Long id, Long accountId, Long domainId, Integer type, Long startIndex, Long pageSizeVal) {
60+
public List<? extends ResourceLimit> searchForLimits(Long id, Long accountId, Long domainId, ResourceType resourceType, Long startIndex, Long pageSizeVal) {
6161
// TODO Auto-generated method stub
6262
return null;
6363
}

0 commit comments

Comments
 (0)