CLOUDSTACK-6699: listResouceDetails - enhance it to list by (key,value) pair - allowed to Admin only.

This commit is contained in:
Nitin Mehta 2014-05-18 23:20:12 -07:00
parent 2e9cc58af6
commit 667c8e7905
6 changed files with 78 additions and 4 deletions

View File

@ -46,6 +46,16 @@ public interface ResourceMetaDataService {
ResourceDetail getDetail(long resourceId, ResourceObjectType resourceType, String key);
/**
* List by key, value pair
* @param resourceType
* @param key
* @param value
* @param forDisplay
* @return
*/
List<? extends ResourceDetail> getDetails(ResourceObjectType resourceType, String key, String value, Boolean forDisplay);
Map<String, String> getDetailsMap(long resourceId, ResourceObjectType resourceType, Boolean forDisplay);
List<? extends ResourceDetail> getDetailsList(long resourceId, ResourceObjectType resourceType, Boolean forDisplay);

View File

@ -44,6 +44,10 @@ public class ListResourceDetailsCmd extends BaseListProjectAndAccountResourcesCm
@Parameter(name = ApiConstants.KEY, type = CommandType.STRING, description = "list by key")
private String key;
@Parameter(name = ApiConstants.VALUE, type = CommandType.STRING, description = "list by key, value. Needs to be passed only along with key" ,
since = "4.4", authorized = { RoleType.Admin })
private String value;
@Parameter(name = ApiConstants.FOR_DISPLAY, type = CommandType.BOOLEAN, description = "if set to true, only details marked with display=true, are returned."
+ " False by default", since = "4.3", authorized = { RoleType.Admin })
private Boolean forDisplay;
@ -56,6 +60,10 @@ public class ListResourceDetailsCmd extends BaseListProjectAndAccountResourcesCm
return key;
}
public String getValue() {
return value;
}
@Override
public String getCommandName() {
return s_name;

View File

@ -32,6 +32,15 @@ public interface ResourceDetailsDao<R extends ResourceDetail> extends GenericDao
*/
public R findDetail(long resourceId, String name);
/**
* Find details by key,value pair
* @param key
* @param value
* @param display
* @return
*/
public List<R> findDetails(String key, String value, Boolean display);
/**
* Removes all details for the resource specified
* @param resourceId

View File

@ -34,6 +34,7 @@ public abstract class ResourceDetailsDaoBase<R extends ResourceDetail> extends G
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("resourceId", AllFieldsSearch.entity().getResourceId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("name", AllFieldsSearch.entity().getName(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("value", AllFieldsSearch.entity().getValue(), SearchCriteria.Op.EQ);
// FIXME SnapshotDetailsVO doesn't have a display field
if (_allAttributes.containsKey("display")) {
AllFieldsSearch.and("display", AllFieldsSearch.entity().isDisplay(), SearchCriteria.Op.EQ);
@ -49,6 +50,25 @@ public abstract class ResourceDetailsDaoBase<R extends ResourceDetail> extends G
return findOneBy(sc);
}
public List<R> findDetails(String name, String value, Boolean display) {
SearchCriteria<R> sc = AllFieldsSearch.create();
if(display != null){
sc.setParameters("display", display);
}
if(name != null){
sc.setParameters("name", name);
}
if(value != null){
sc.setParameters("value", value);
}
List<R> results = search(sc, null);
return results;
}
public Map<String, String> listDetailsKeyPairs(long resourceId) {
SearchCriteria<R> sc = AllFieldsSearch.create();
sc.setParameters("resourceId", resourceId);

View File

@ -3895,23 +3895,40 @@ public class QueryManagerImpl extends ManagerBase implements QueryService {
Boolean forDisplay = cmd.getDisplay();
ResourceTag.ResourceObjectType resourceType = cmd.getResourceType();
String resourceIdStr = cmd.getResourceId();
String value = cmd.getValue();
Long resourceId = null;
//Validation - 1.1 - resourceId and value cant be null.
if(resourceIdStr == null && value == null){
throw new InvalidParameterValueException("Insufficient parameters passed for listing by resourceId OR key,value pair. Please check your params and try again.");
}
//Validation - 1.2 - Value has to be passed along with key.
if(value != null && key == null){
throw new InvalidParameterValueException("Listing by (key, value) but key is null. Please check the params and try again");
}
//Validation - 1.3
if (resourceIdStr != null) {
resourceId = _taggedResourceMgr.getResourceId(resourceIdStr, resourceType);
if (resourceId == null) {
throw new InvalidParameterValueException("Cannot find resource with resourceId " + resourceIdStr + " and of resource type " + resourceType);
}
}
if (resourceId == null) {
throw new InvalidParameterValueException("Cannot find resource with resourceId " + resourceIdStr + " and of resource type " + resourceType);
}
List<? extends ResourceDetail> detailList = new ArrayList<ResourceDetail>();
ResourceDetail requestedDetail = null;
if (key == null) {
detailList = _resourceMetaDataMgr.getDetailsList(resourceId, resourceType, forDisplay);
} else {
} else if (value == null){
requestedDetail = _resourceMetaDataMgr.getDetail(resourceId, resourceType, key);
if (requestedDetail != null && forDisplay != null && requestedDetail.isDisplay() != forDisplay) {
requestedDetail = null;
}
}else {
detailList = _resourceMetaDataMgr.getDetails(resourceType, key, value, forDisplay);
}
List<ResourceDetailResponse> responseList = new ArrayList<ResourceDetailResponse>();

View File

@ -224,6 +224,10 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements Resource
return dao.findDetail(resourceId, key);
}
private List<? extends ResourceDetail> getDetails(String key, String value, Boolean forDisplay) {
return dao.findDetails(key, value, forDisplay);
}
private void addDetail(long resourceId, String key, String value, boolean forDisplay) {
dao.addDetail(resourceId, key, value, forDisplay);
}
@ -257,6 +261,12 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements Resource
return newDetailDaoHelper.getDetail(resourceId, key);
}
@Override
public List<? extends ResourceDetail> getDetails(ResourceObjectType resourceType, String key, String value, Boolean forDisplay){
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
return newDetailDaoHelper.getDetails(key, value, forDisplay);
}
@Override
public Map<String, String> getDetailsMap(long resourceId, ResourceObjectType resourceType, Boolean forDisplay) {
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);