Skip to content

Commit aa57078

Browse files
author
Alena Prokharchyk
committed
Added resource tag support for Zone
1 parent 1218907 commit aa57078

8 files changed

Lines changed: 87 additions & 19 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public class ApiConstants {
518518
public static final String ROUTING = "isrouting";
519519
public static final String MAX_CONNECTIONS = "maxconnections";
520520
public static final String SERVICE_STATE = "servicestate";
521-
public static final String RESOURCE_TAG = "resourcetag";
521+
public static final String RESOURCE_TAGS = "resourcetags";
522522
public enum HostDetails {
523523
all, capacity, events, stats, min;
524524
}

‎api/src/org/apache/cloudstack/api/command/user/offering/ListServiceOfferingsCmd.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
import org.apache.cloudstack.api.ApiConstants;
2626
import org.apache.cloudstack.api.BaseListCmd;
2727
import org.apache.cloudstack.api.Parameter;
28-
import org.apache.cloudstack.api.BaseCmd.CommandType;
2928
import org.apache.cloudstack.api.response.DomainResponse;
3029
import org.apache.cloudstack.api.response.ListResponse;
3130
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
3231
import org.apache.cloudstack.api.response.UserVmResponse;
33-
3432
import org.apache.log4j.Logger;
3533

3634
import com.cloud.exception.InvalidParameterValueException;
@@ -66,7 +64,7 @@ public class ListServiceOfferingsCmd extends BaseListCmd {
6664
@Parameter(name=ApiConstants.SYSTEM_VM_TYPE, type=CommandType.STRING, description="the system VM type. Possible types are \"consoleproxy\", \"secondarystoragevm\" or \"domainrouter\".")
6765
private String systemVmType;
6866

69-
@Parameter(name = ApiConstants.RESOURCE_TAG, type = CommandType.MAP, description = "List service offerings by resource tags (key/value pairs)", since="4.3")
67+
@Parameter(name = ApiConstants.RESOURCE_TAGS, type = CommandType.MAP, description = "List service offerings by resource tags (key/value pairs)", since="4.3")
7068
private Map resourceTag;
7169

7270

‎api/src/org/apache/cloudstack/api/command/user/zone/ListZonesByCmd.java‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.zone;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.Collection;
20+
import java.util.HashMap;
21+
import java.util.Iterator;
22+
import java.util.Map;
2123

2224
import org.apache.cloudstack.api.APICommand;
2325
import org.apache.cloudstack.api.ApiConstants;
2426
import org.apache.cloudstack.api.BaseListCmd;
2527
import org.apache.cloudstack.api.Parameter;
26-
import org.apache.cloudstack.api.BaseCmd.CommandType;
2728
import org.apache.cloudstack.api.response.DomainResponse;
2829
import org.apache.cloudstack.api.response.ListResponse;
29-
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
3030
import org.apache.cloudstack.api.response.ZoneResponse;
3131
import org.apache.log4j.Logger;
3232

33-
import com.cloud.dc.DataCenter;
33+
import com.cloud.exception.InvalidParameterValueException;
3434

3535
@APICommand(name = "listZones", description="Lists zones", responseObject=ZoneResponse.class)
3636
public class ListZonesByCmd extends BaseListCmd {
@@ -62,6 +62,9 @@ public class ListZonesByCmd extends BaseListCmd {
6262

6363
@Parameter(name=ApiConstants.SHOW_CAPACITIES, type=CommandType.BOOLEAN, description="flag to display the capacity of the zones")
6464
private Boolean showCapacities;
65+
66+
@Parameter(name = ApiConstants.TAGS, type = CommandType.MAP, description = "List zones by resource tags (key/value pairs)", since="4.3")
67+
private Map tags;
6568

6669
/////////////////////////////////////////////////////
6770
/////////////////// Accessors ///////////////////////
@@ -90,6 +93,25 @@ public String getNetworkType() {
9093
public Boolean getShowCapacities() {
9194
return showCapacities;
9295
}
96+
97+
public Map<String, String> getTags() {
98+
Map<String, String> tagsMap = null;
99+
if (tags != null && !tags.isEmpty()) {
100+
tagsMap = new HashMap<String, String>();
101+
Collection<?> servicesCollection = tags.values();
102+
Iterator<?> iter = servicesCollection.iterator();
103+
while (iter.hasNext()) {
104+
HashMap<String, String> services = (HashMap<String, String>) iter.next();
105+
String key = services.get("key");
106+
String value = services.get("value");
107+
if (value == null) {
108+
throw new InvalidParameterValueException("No value is passed in for key " + key);
109+
}
110+
tagsMap.put(key, value);
111+
}
112+
}
113+
return tagsMap;
114+
}
93115

94116
/////////////////////////////////////////////////////
95117
/////////////// API Implementation///////////////////

‎api/src/org/apache/cloudstack/api/response/ServiceOfferingResponse.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class ServiceOfferingResponse extends BaseResponse {
107107
@Param(description = "additional key/value details tied with this service offering", since = "4.2.0")
108108
private Map<String, String> details;
109109

110-
@SerializedName(ApiConstants.RESOURCE_TAG) @Param(description="the list of resource tags associated with service offering." +
110+
@SerializedName(ApiConstants.RESOURCE_TAGS) @Param(description="the list of resource tags associated with service offering." +
111111
" The resource tags are not used for Volume/VM placement on the specific host.",
112112
responseObject = ResourceTagResponse.class, since="4.3")
113113
private Set<ResourceTagResponse> resourceTags;

‎api/src/org/apache/cloudstack/api/response/ZoneResponse.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.response;
1818

19+
import java.util.LinkedHashSet;
1920
import java.util.List;
21+
import java.util.Set;
2022

2123
import org.apache.cloudstack.api.ApiConstants;
2224
import org.apache.cloudstack.api.BaseResponse;
@@ -98,6 +100,15 @@ public class ZoneResponse extends BaseResponse {
98100

99101
@SerializedName(ApiConstants.LOCAL_STORAGE_ENABLED) @Param(description="true if local storage offering enabled, false otherwise")
100102
private boolean localStorageEnabled;
103+
104+
@SerializedName(ApiConstants.TAGS) @Param(description="the list of resource tags associated with zone.",
105+
responseObject = ResourceTagResponse.class, since="4.3")
106+
private Set<ResourceTagResponse> tags;
107+
108+
109+
public ZoneResponse(){
110+
tags = new LinkedHashSet<ResourceTagResponse>();
111+
}
101112

102113
public void setId(String id) {
103114
this.id = id;
@@ -198,4 +209,8 @@ public String getIp6Dns2() {
198209
public void setIp6Dns2(String ip6Dns2) {
199210
this.ip6Dns2 = ip6Dns2;
200211
}
212+
213+
public void addTag(ResourceTagResponse tag){
214+
this.tags.add(tag);
215+
}
201216
}

‎server/src/com/cloud/api/query/QueryManagerImpl.java‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,9 +2536,23 @@ private Pair<List<DataCenterJoinVO>, Integer> listDataCentersInternal(ListZonesB
25362536
String keyword = cmd.getKeyword();
25372537
String name = cmd.getName();
25382538
String networkType = cmd.getNetworkType();
2539-
2539+
Map<String, String> resourceTags = cmd.getTags();
2540+
2541+
SearchBuilder<DataCenterJoinVO> sb = _dcJoinDao.createSearchBuilder();
2542+
if (resourceTags != null && !resourceTags.isEmpty()) {
2543+
SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
2544+
for (int count=0; count < resourceTags.size(); count++) {
2545+
tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
2546+
tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
2547+
tagSearch.cp();
2548+
}
2549+
tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
2550+
sb.groupBy(sb.entity().getId());
2551+
sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
2552+
}
2553+
25402554
Filter searchFilter = new Filter(DataCenterJoinVO.class, null, false, cmd.getStartIndex(), cmd.getPageSizeVal());
2541-
SearchCriteria<DataCenterJoinVO> sc = _dcJoinDao.createSearchCriteria();
2555+
SearchCriteria<DataCenterJoinVO> sc = sb.create();
25422556

25432557
if (networkType != null) {
25442558
sc.addAnd("networkType", SearchCriteria.Op.EQ, networkType);
@@ -2687,6 +2701,16 @@ private Pair<List<DataCenterJoinVO>, Integer> listDataCentersInternal(ListZonesB
26872701
}
26882702
}
26892703
}
2704+
2705+
if (resourceTags != null && !resourceTags.isEmpty()) {
2706+
int count = 0;
2707+
sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.Zone.toString());
2708+
for (String key : resourceTags.keySet()) {
2709+
sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
2710+
sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), resourceTags.get(key));
2711+
count++;
2712+
}
2713+
}
26902714

26912715
return _dcJoinDao.searchAndCount(sc, searchFilter);
26922716
}

‎server/src/com/cloud/api/query/dao/DataCenterJoinDaoImpl.java‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@
2020

2121
import javax.ejb.Local;
2222

23+
import org.apache.cloudstack.api.response.ResourceTagResponse;
24+
import org.apache.cloudstack.api.response.ZoneResponse;
25+
import org.apache.cloudstack.context.CallContext;
2326
import org.apache.log4j.Logger;
27+
import org.springframework.stereotype.Component;
2428

2529
import com.cloud.api.ApiDBUtils;
2630
import com.cloud.api.ApiResponseHelper;
2731
import com.cloud.api.query.vo.DataCenterJoinVO;
32+
import com.cloud.api.query.vo.ResourceTagJoinVO;
2833
import com.cloud.dc.DataCenter;
29-
30-
import org.apache.cloudstack.api.response.ZoneResponse;
31-
import org.apache.cloudstack.context.CallContext;
32-
34+
import com.cloud.server.ResourceTag.TaggedResourceType;
3335
import com.cloud.user.Account;
3436
import com.cloud.utils.db.GenericDaoBase;
3537
import com.cloud.utils.db.SearchBuilder;
3638
import com.cloud.utils.db.SearchCriteria;
3739

38-
import org.springframework.stereotype.Component;
39-
4040
@Component
4141
@Local(value={DataCenterJoinDao.class})
4242
public class DataCenterJoinDaoImpl extends GenericDaoBase<DataCenterJoinVO, Long> implements DataCenterJoinDao {
@@ -97,6 +97,14 @@ public ZoneResponse newDataCenterResponse(DataCenterJoinVO dataCenter, Boolean s
9797
zoneResponse.setAllocationState(dataCenter.getAllocationState().toString());
9898
zoneResponse.setZoneToken(dataCenter.getZoneToken());
9999
zoneResponse.setDhcpProvider(dataCenter.getDhcpProvider());
100+
101+
// update tag information
102+
List<ResourceTagJoinVO> resourceTags = ApiDBUtils.listResourceTagViewByResourceUUID(dataCenter.getUuid(), TaggedResourceType.Zone);
103+
for (ResourceTagJoinVO resourceTag : resourceTags) {
104+
ResourceTagResponse tagResponse = ApiDBUtils.newResourceTagResponse(resourceTag, false);
105+
zoneResponse.addTag(tagResponse);
106+
}
107+
100108
zoneResponse.setObjectName("zone");
101109
return zoneResponse;
102110
}

‎server/src/com/cloud/api/query/dao/ServiceOfferingJoinDaoImpl.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public ServiceOfferingResponse newServiceOfferingResponse(ServiceOfferingJoinVO
8989
List<ResourceTagJoinVO> resourceTags = ApiDBUtils.listResourceTagViewByResourceUUID(offering.getUuid(), TaggedResourceType.ServiceOffering);
9090
for (ResourceTagJoinVO resourceTag : resourceTags) {
9191
ResourceTagResponse tagResponse = ApiDBUtils.newResourceTagResponse(resourceTag, false);
92-
offeringResponse.addTag(tagResponse); }
92+
offeringResponse.addTag(tagResponse);
93+
}
9394

9495
return offeringResponse;
9596
}

0 commit comments

Comments
 (0)