Added resource tag support for Zone

This commit is contained in:
Alena Prokharchyk 2013-10-17 14:56:55 -07:00
parent 12189074b2
commit aa570783e8
8 changed files with 87 additions and 19 deletions

View File

@ -518,7 +518,7 @@ public class ApiConstants {
public static final String ROUTING = "isrouting"; public static final String ROUTING = "isrouting";
public static final String MAX_CONNECTIONS = "maxconnections"; public static final String MAX_CONNECTIONS = "maxconnections";
public static final String SERVICE_STATE = "servicestate"; public static final String SERVICE_STATE = "servicestate";
public static final String RESOURCE_TAG = "resourcetag"; public static final String RESOURCE_TAGS = "resourcetags";
public enum HostDetails { public enum HostDetails {
all, capacity, events, stats, min; all, capacity, events, stats, min;
} }

View File

@ -25,12 +25,10 @@ import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseListCmd; import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.BaseCmd.CommandType;
import org.apache.cloudstack.api.response.DomainResponse; import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ListResponse; import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.ServiceOfferingResponse; import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.InvalidParameterValueException;
@ -66,7 +64,7 @@ public class ListServiceOfferingsCmd extends BaseListCmd {
@Parameter(name=ApiConstants.SYSTEM_VM_TYPE, type=CommandType.STRING, description="the system VM type. Possible types are \"consoleproxy\", \"secondarystoragevm\" or \"domainrouter\".") @Parameter(name=ApiConstants.SYSTEM_VM_TYPE, type=CommandType.STRING, description="the system VM type. Possible types are \"consoleproxy\", \"secondarystoragevm\" or \"domainrouter\".")
private String systemVmType; private String systemVmType;
@Parameter(name = ApiConstants.RESOURCE_TAG, type = CommandType.MAP, description = "List service offerings by resource tags (key/value pairs)", since="4.3") @Parameter(name = ApiConstants.RESOURCE_TAGS, type = CommandType.MAP, description = "List service offerings by resource tags (key/value pairs)", since="4.3")
private Map resourceTag; private Map resourceTag;

View File

@ -16,21 +16,21 @@
// under the License. // under the License.
package org.apache.cloudstack.api.command.user.zone; package org.apache.cloudstack.api.command.user.zone;
import java.util.ArrayList; import java.util.Collection;
import java.util.List; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseListCmd; import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.BaseCmd.CommandType;
import org.apache.cloudstack.api.response.DomainResponse; import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ListResponse; import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.api.response.ZoneResponse; import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.dc.DataCenter; import com.cloud.exception.InvalidParameterValueException;
@APICommand(name = "listZones", description="Lists zones", responseObject=ZoneResponse.class) @APICommand(name = "listZones", description="Lists zones", responseObject=ZoneResponse.class)
public class ListZonesByCmd extends BaseListCmd { public class ListZonesByCmd extends BaseListCmd {
@ -62,6 +62,9 @@ public class ListZonesByCmd extends BaseListCmd {
@Parameter(name=ApiConstants.SHOW_CAPACITIES, type=CommandType.BOOLEAN, description="flag to display the capacity of the zones") @Parameter(name=ApiConstants.SHOW_CAPACITIES, type=CommandType.BOOLEAN, description="flag to display the capacity of the zones")
private Boolean showCapacities; private Boolean showCapacities;
@Parameter(name = ApiConstants.TAGS, type = CommandType.MAP, description = "List zones by resource tags (key/value pairs)", since="4.3")
private Map tags;
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
/////////////////// Accessors /////////////////////// /////////////////// Accessors ///////////////////////
@ -90,6 +93,25 @@ public class ListZonesByCmd extends BaseListCmd {
public Boolean getShowCapacities() { public Boolean getShowCapacities() {
return showCapacities; return showCapacities;
} }
public Map<String, String> getTags() {
Map<String, String> tagsMap = null;
if (tags != null && !tags.isEmpty()) {
tagsMap = new HashMap<String, String>();
Collection<?> servicesCollection = tags.values();
Iterator<?> iter = servicesCollection.iterator();
while (iter.hasNext()) {
HashMap<String, String> services = (HashMap<String, String>) iter.next();
String key = services.get("key");
String value = services.get("value");
if (value == null) {
throw new InvalidParameterValueException("No value is passed in for key " + key);
}
tagsMap.put(key, value);
}
}
return tagsMap;
}
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
/////////////// API Implementation/////////////////// /////////////// API Implementation///////////////////

View File

@ -107,7 +107,7 @@ public class ServiceOfferingResponse extends BaseResponse {
@Param(description = "additional key/value details tied with this service offering", since = "4.2.0") @Param(description = "additional key/value details tied with this service offering", since = "4.2.0")
private Map<String, String> details; private Map<String, String> details;
@SerializedName(ApiConstants.RESOURCE_TAG) @Param(description="the list of resource tags associated with service offering." + @SerializedName(ApiConstants.RESOURCE_TAGS) @Param(description="the list of resource tags associated with service offering." +
" The resource tags are not used for Volume/VM placement on the specific host.", " The resource tags are not used for Volume/VM placement on the specific host.",
responseObject = ResourceTagResponse.class, since="4.3") responseObject = ResourceTagResponse.class, since="4.3")
private Set<ResourceTagResponse> resourceTags; private Set<ResourceTagResponse> resourceTags;

View File

@ -16,7 +16,9 @@
// under the License. // under the License.
package org.apache.cloudstack.api.response; package org.apache.cloudstack.api.response;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse; import org.apache.cloudstack.api.BaseResponse;
@ -98,6 +100,15 @@ public class ZoneResponse extends BaseResponse {
@SerializedName(ApiConstants.LOCAL_STORAGE_ENABLED) @Param(description="true if local storage offering enabled, false otherwise") @SerializedName(ApiConstants.LOCAL_STORAGE_ENABLED) @Param(description="true if local storage offering enabled, false otherwise")
private boolean localStorageEnabled; private boolean localStorageEnabled;
@SerializedName(ApiConstants.TAGS) @Param(description="the list of resource tags associated with zone.",
responseObject = ResourceTagResponse.class, since="4.3")
private Set<ResourceTagResponse> tags;
public ZoneResponse(){
tags = new LinkedHashSet<ResourceTagResponse>();
}
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
@ -198,4 +209,8 @@ public class ZoneResponse extends BaseResponse {
public void setIp6Dns2(String ip6Dns2) { public void setIp6Dns2(String ip6Dns2) {
this.ip6Dns2 = ip6Dns2; this.ip6Dns2 = ip6Dns2;
} }
public void addTag(ResourceTagResponse tag){
this.tags.add(tag);
}
} }

View File

@ -2536,9 +2536,23 @@ public class QueryManagerImpl extends ManagerBase implements QueryService {
String keyword = cmd.getKeyword(); String keyword = cmd.getKeyword();
String name = cmd.getName(); String name = cmd.getName();
String networkType = cmd.getNetworkType(); String networkType = cmd.getNetworkType();
Map<String, String> resourceTags = cmd.getTags();
SearchBuilder<DataCenterJoinVO> sb = _dcJoinDao.createSearchBuilder();
if (resourceTags != null && !resourceTags.isEmpty()) {
SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
for (int count=0; count < resourceTags.size(); count++) {
tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
tagSearch.cp();
}
tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
sb.groupBy(sb.entity().getId());
sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
}
Filter searchFilter = new Filter(DataCenterJoinVO.class, null, false, cmd.getStartIndex(), cmd.getPageSizeVal()); Filter searchFilter = new Filter(DataCenterJoinVO.class, null, false, cmd.getStartIndex(), cmd.getPageSizeVal());
SearchCriteria<DataCenterJoinVO> sc = _dcJoinDao.createSearchCriteria(); SearchCriteria<DataCenterJoinVO> sc = sb.create();
if (networkType != null) { if (networkType != null) {
sc.addAnd("networkType", SearchCriteria.Op.EQ, networkType); sc.addAnd("networkType", SearchCriteria.Op.EQ, networkType);
@ -2687,6 +2701,16 @@ public class QueryManagerImpl extends ManagerBase implements QueryService {
} }
} }
} }
if (resourceTags != null && !resourceTags.isEmpty()) {
int count = 0;
sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.Zone.toString());
for (String key : resourceTags.keySet()) {
sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), resourceTags.get(key));
count++;
}
}
return _dcJoinDao.searchAndCount(sc, searchFilter); return _dcJoinDao.searchAndCount(sc, searchFilter);
} }

View File

@ -20,23 +20,23 @@ import java.util.List;
import javax.ejb.Local; import javax.ejb.Local;
import org.apache.cloudstack.api.response.ResourceTagResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.api.ApiDBUtils; import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseHelper; import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.vo.DataCenterJoinVO; import com.cloud.api.query.vo.DataCenterJoinVO;
import com.cloud.api.query.vo.ResourceTagJoinVO;
import com.cloud.dc.DataCenter; import com.cloud.dc.DataCenter;
import com.cloud.server.ResourceTag.TaggedResourceType;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.context.CallContext;
import com.cloud.user.Account; import com.cloud.user.Account;
import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria;
import org.springframework.stereotype.Component;
@Component @Component
@Local(value={DataCenterJoinDao.class}) @Local(value={DataCenterJoinDao.class})
public class DataCenterJoinDaoImpl extends GenericDaoBase<DataCenterJoinVO, Long> implements DataCenterJoinDao { public class DataCenterJoinDaoImpl extends GenericDaoBase<DataCenterJoinVO, Long> implements DataCenterJoinDao {
@ -97,6 +97,14 @@ public class DataCenterJoinDaoImpl extends GenericDaoBase<DataCenterJoinVO, Long
zoneResponse.setAllocationState(dataCenter.getAllocationState().toString()); zoneResponse.setAllocationState(dataCenter.getAllocationState().toString());
zoneResponse.setZoneToken(dataCenter.getZoneToken()); zoneResponse.setZoneToken(dataCenter.getZoneToken());
zoneResponse.setDhcpProvider(dataCenter.getDhcpProvider()); zoneResponse.setDhcpProvider(dataCenter.getDhcpProvider());
// update tag information
List<ResourceTagJoinVO> resourceTags = ApiDBUtils.listResourceTagViewByResourceUUID(dataCenter.getUuid(), TaggedResourceType.Zone);
for (ResourceTagJoinVO resourceTag : resourceTags) {
ResourceTagResponse tagResponse = ApiDBUtils.newResourceTagResponse(resourceTag, false);
zoneResponse.addTag(tagResponse);
}
zoneResponse.setObjectName("zone"); zoneResponse.setObjectName("zone");
return zoneResponse; return zoneResponse;
} }

View File

@ -89,7 +89,8 @@ public class ServiceOfferingJoinDaoImpl extends GenericDaoBase<ServiceOfferingJo
List<ResourceTagJoinVO> resourceTags = ApiDBUtils.listResourceTagViewByResourceUUID(offering.getUuid(), TaggedResourceType.ServiceOffering); List<ResourceTagJoinVO> resourceTags = ApiDBUtils.listResourceTagViewByResourceUUID(offering.getUuid(), TaggedResourceType.ServiceOffering);
for (ResourceTagJoinVO resourceTag : resourceTags) { for (ResourceTagJoinVO resourceTag : resourceTags) {
ResourceTagResponse tagResponse = ApiDBUtils.newResourceTagResponse(resourceTag, false); ResourceTagResponse tagResponse = ApiDBUtils.newResourceTagResponse(resourceTag, false);
offeringResponse.addTag(tagResponse); } offeringResponse.addTag(tagResponse);
}
return offeringResponse; return offeringResponse;
} }