Skip to content

Commit 6417629

Browse files
committed
CLOUDSTACK-3314: ListTemplates only show one entry for cross-zone
templates.
1 parent deaf10c commit 6417629

6 files changed

Lines changed: 142 additions & 121 deletions

File tree

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Date;
2020
import java.util.LinkedHashSet;
21-
import java.util.List;
2221
import java.util.Map;
2322
import java.util.Set;
2423

@@ -137,8 +136,10 @@ public class TemplateResponse extends BaseResponse implements ControlledViewEnti
137136
@SerializedName(ApiConstants.DETAILS) @Param(description="additional key/value details tied with template")
138137
private Map details;
139138

140-
@SerializedName("zones") @Param(description="list of zones associated with tempate", responseObject = TemplateZoneResponse.class)
141-
private Set<TemplateZoneResponse> zones;
139+
// To avoid breaking backwards compatibility, we still treat a template at different zones as different templates, so not embedding
140+
// template_zone information in this TemplateZoneResponse set.
141+
// @SerializedName("zones") @Param(description="list of zones associated with tempate", responseObject = TemplateZoneResponse.class)
142+
// private Set<TemplateZoneResponse> zones;
142143

143144
@SerializedName(ApiConstants.TAGS) @Param(description="the list of resource tags associated with tempate", responseObject = ResourceTagResponse.class)
144145
private Set<ResourceTagResponse> tags;
@@ -150,7 +151,7 @@ public class TemplateResponse extends BaseResponse implements ControlledViewEnti
150151
private Boolean isDynamicallyScalable;
151152

152153
public TemplateResponse(){
153-
zones = new LinkedHashSet<TemplateZoneResponse>();
154+
// zones = new LinkedHashSet<TemplateZoneResponse>();
154155
tags = new LinkedHashSet<ResourceTagResponse>();
155156
}
156157

@@ -176,6 +177,7 @@ public void setAccountId(String accountId) {
176177
this.accountId = accountId;
177178
}
178179

180+
@Override
179181
public void setAccountName(String account) {
180182
this.account = account;
181183
}
@@ -312,13 +314,6 @@ public void addTag(ResourceTagResponse tag){
312314
this.tags.add(tag);
313315
}
314316

315-
public void setZones(Set<TemplateZoneResponse> zones){
316-
this.zones = zones;
317-
}
318-
319-
public void addZone(TemplateZoneResponse zone){
320-
this.zones.add(zone);
321-
}
322317

323318
public void setSshKeyEnabled(boolean sshKeyEnabled) {
324319
this.sshKeyEnabled = sshKeyEnabled;

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

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import javax.ejb.Local;
2828
import javax.inject.Inject;
2929

30-
import com.cloud.storage.*;
3130
import org.apache.cloudstack.affinity.AffinityGroupResponse;
3231
import org.apache.cloudstack.affinity.AffinityGroupVMMapVO;
3332
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
@@ -56,7 +55,28 @@
5655
import org.apache.cloudstack.api.command.user.volume.ListResourceDetailsCmd;
5756
import org.apache.cloudstack.api.command.user.volume.ListVolumesCmd;
5857
import org.apache.cloudstack.api.command.user.zone.ListZonesByCmd;
59-
import org.apache.cloudstack.api.response.*;
58+
import org.apache.cloudstack.api.response.AccountResponse;
59+
import org.apache.cloudstack.api.response.AsyncJobResponse;
60+
import org.apache.cloudstack.api.response.DiskOfferingResponse;
61+
import org.apache.cloudstack.api.response.DomainRouterResponse;
62+
import org.apache.cloudstack.api.response.EventResponse;
63+
import org.apache.cloudstack.api.response.HostResponse;
64+
import org.apache.cloudstack.api.response.ImageStoreResponse;
65+
import org.apache.cloudstack.api.response.InstanceGroupResponse;
66+
import org.apache.cloudstack.api.response.ListResponse;
67+
import org.apache.cloudstack.api.response.ProjectAccountResponse;
68+
import org.apache.cloudstack.api.response.ProjectInvitationResponse;
69+
import org.apache.cloudstack.api.response.ProjectResponse;
70+
import org.apache.cloudstack.api.response.ResourceDetailResponse;
71+
import org.apache.cloudstack.api.response.ResourceTagResponse;
72+
import org.apache.cloudstack.api.response.SecurityGroupResponse;
73+
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
74+
import org.apache.cloudstack.api.response.StoragePoolResponse;
75+
import org.apache.cloudstack.api.response.TemplateResponse;
76+
import org.apache.cloudstack.api.response.UserResponse;
77+
import org.apache.cloudstack.api.response.UserVmResponse;
78+
import org.apache.cloudstack.api.response.VolumeResponse;
79+
import org.apache.cloudstack.api.response.ZoneResponse;
6080
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateState;
6181
import org.apache.cloudstack.query.QueryService;
6282
import org.apache.log4j.Logger;
@@ -132,8 +152,14 @@
132152
import com.cloud.server.TaggedResourceService;
133153
import com.cloud.service.ServiceOfferingVO;
134154
import com.cloud.service.dao.ServiceOfferingDao;
155+
import com.cloud.storage.DataStoreRole;
156+
import com.cloud.storage.ScopeType;
157+
import com.cloud.storage.Storage;
135158
import com.cloud.storage.Storage.ImageFormat;
136159
import com.cloud.storage.Storage.TemplateType;
160+
import com.cloud.storage.VMTemplateVO;
161+
import com.cloud.storage.Volume;
162+
import com.cloud.storage.VolumeDetailVO;
137163
import com.cloud.storage.dao.VMTemplateDao;
138164
import com.cloud.storage.dao.VolumeDetailsDao;
139165
import com.cloud.template.VirtualMachineTemplate.TemplateFilter;
@@ -729,7 +755,7 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsByCriteria(Criteria c,
729755
// first search distinct vm id by using query criteria and pagination
730756
SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
731757
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct
732-
// ids
758+
// ids
733759
_accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts,
734760
listProjectResourcesCriteria);
735761

@@ -951,7 +977,7 @@ private Pair<List<SecurityGroupJoinVO>, Integer> searchForSecurityGroupsInternal
951977
cmd.getPageSizeVal());
952978
SearchBuilder<SecurityGroupJoinVO> sb = _securityGroupJoinDao.createSearchBuilder();
953979
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct
954-
// ids
980+
// ids
955981
_accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts,
956982
listProjectResourcesCriteria);
957983

@@ -1474,7 +1500,7 @@ public Pair<List<HostJoinVO>, Integer> searchForServersInternal(ListHostsCmd cmd
14741500

14751501
SearchBuilder<HostJoinVO> sb = _hostJoinDao.createSearchBuilder();
14761502
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct
1477-
// ids
1503+
// ids
14781504
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
14791505
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
14801506
sb.and("type", sb.entity().getType(), SearchCriteria.Op.LIKE);
@@ -1935,7 +1961,7 @@ private Pair<List<StoragePoolJoinVO>, Integer> searchForStoragePoolsInternal(Lis
19351961

19361962
SearchBuilder<StoragePoolJoinVO> sb = _poolJoinDao.createSearchBuilder();
19371963
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct
1938-
// ids
1964+
// ids
19391965
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
19401966
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
19411967
sb.and("path", sb.entity().getPath(), SearchCriteria.Op.EQ);
@@ -2025,7 +2051,7 @@ private Pair<List<ImageStoreJoinVO>, Integer> searchForImageStoresInternal(ListI
20252051

20262052
SearchBuilder<ImageStoreJoinVO> sb = _imageStoreJoinDao.createSearchBuilder();
20272053
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct
2028-
// ids
2054+
// ids
20292055
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
20302056
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
20312057
sb.and("dataCenterId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
@@ -2105,7 +2131,7 @@ private Pair<List<ImageStoreJoinVO>, Integer> searchForCacheStoresInternal(ListC
21052131

21062132
SearchBuilder<ImageStoreJoinVO> sb = _imageStoreJoinDao.createSearchBuilder();
21072133
sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct
2108-
// ids
2134+
// ids
21092135
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
21102136
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
21112137
sb.and("dataCenterId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
@@ -2229,14 +2255,14 @@ private Pair<List<DiskOfferingJoinVO>, Integer> searchForDiskOfferingsInternal(L
22292255

22302256
spc.addOr("domainId", SearchCriteria.Op.IN, domainIds.toArray());
22312257
spc.addOr("domainId", SearchCriteria.Op.NULL); // include public
2232-
// offering as where
2258+
// offering as where
22332259
sc.addAnd("domainId", SearchCriteria.Op.SC, spc);
22342260
sc.addAnd("systemUse", SearchCriteria.Op.EQ, false); // non-root
2235-
// users should
2236-
// not see
2237-
// system
2238-
// offering at
2239-
// all
2261+
// users should
2262+
// not see
2263+
// system
2264+
// offering at
2265+
// all
22402266

22412267
}
22422268

@@ -2356,7 +2382,7 @@ private Pair<List<ServiceOfferingJoinVO>, Integer> searchForServiceOfferingsInte
23562382

23572383
spc.addOr("domainId", SearchCriteria.Op.IN, domainIds.toArray());
23582384
spc.addOr("domainId", SearchCriteria.Op.NULL); // include public
2359-
// offering as where
2385+
// offering as where
23602386
sc.addAnd("domainId", SearchCriteria.Op.SC, spc);
23612387

23622388
} else {
@@ -2439,8 +2465,9 @@ private Pair<List<DataCenterJoinVO>, Integer> listDataCentersInternal(ListZonesB
24392465
Filter searchFilter = new Filter(DataCenterJoinVO.class, null, false, cmd.getStartIndex(), cmd.getPageSizeVal());
24402466
SearchCriteria<DataCenterJoinVO> sc = _dcJoinDao.createSearchCriteria();
24412467

2442-
if (networkType != null)
2468+
if (networkType != null) {
24432469
sc.addAnd("networkType", SearchCriteria.Op.EQ, networkType);
2470+
}
24442471

24452472
if (id != null) {
24462473
sc.addAnd("id", SearchCriteria.Op.EQ, id);
@@ -2550,8 +2577,8 @@ private Pair<List<DataCenterJoinVO>, Integer> listDataCentersInternal(ListZonesB
25502577
if (account != null) {
25512578
if ((available != null) && Boolean.FALSE.equals(available)) {
25522579
Set<Long> dcIds = new HashSet<Long>(); // data centers with
2553-
// at least one VM
2554-
// running
2580+
// at least one VM
2581+
// running
25552582
List<DomainRouterVO> routers = _routerDao.listBy(account.getId());
25562583
for (DomainRouterVO router : routers) {
25572584
dcIds.add(router.getDataCenterId());
@@ -2664,6 +2691,9 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long temp
26642691
Boolean isAscending = Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
26652692
isAscending = (isAscending == null ? true : isAscending);
26662693
Filter searchFilter = new Filter(TemplateJoinVO.class, "sortKey", isAscending, startIndex, pageSize);
2694+
2695+
SearchBuilder<TemplateJoinVO> sb = _templateJoinDao.createSearchBuilder();
2696+
sb.select(null, Func.DISTINCT, sb.entity().getTempZonePair()); // select distinct (templateId, zoneId) pair
26672697
SearchCriteria<TemplateJoinVO> sc = _templateJoinDao.createSearchCriteria();
26682698

26692699
// verify templateId parameter and specially handle it
@@ -2846,7 +2876,14 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long temp
28462876
}
28472877

28482878
if (zoneId != null) {
2849-
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
2879+
SearchCriteria<TemplateJoinVO> zoneSc = _templateJoinDao.createSearchCriteria();
2880+
zoneSc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
2881+
// handle the case where xs-tools.iso and vmware-tools.iso do not have data_center information in template_view
2882+
SearchCriteria<TemplateJoinVO> isoPerhostSc = _templateJoinDao.createSearchCriteria();
2883+
isoPerhostSc.addAnd("format", SearchCriteria.Op.EQ, ImageFormat.ISO);
2884+
isoPerhostSc.addAnd("templateType", SearchCriteria.Op.EQ, TemplateType.PERHOST);
2885+
zoneSc.addOr("templateType", SearchCriteria.Op.SC, isoPerhostSc);
2886+
sc.addAnd("dataCenterId", SearchCriteria.Op.SC, zoneSc);
28502887
}
28512888

28522889
if (!showDomr) {
@@ -2867,12 +2904,12 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long temp
28672904
return uniqueTmplPair;
28682905
}
28692906
List<TemplateJoinVO> uniqueTmpls = uniqueTmplPair.first();
2870-
Long[] vrIds = new Long[uniqueTmpls.size()];
2907+
String[] tzIds = new String[uniqueTmpls.size()];
28712908
int i = 0;
28722909
for (TemplateJoinVO v : uniqueTmpls) {
2873-
vrIds[i++] = v.getId();
2910+
tzIds[i++] = v.getTempZonePair();
28742911
}
2875-
List<TemplateJoinVO> vrs = _templateJoinDao.searchByIds(vrIds);
2912+
List<TemplateJoinVO> vrs = _templateJoinDao.searchByTemplateZonePair(tzIds);
28762913
return new Pair<List<TemplateJoinVO>, Integer>(vrs, count);
28772914

28782915
// TODO: revisit the special logic for iso search in
@@ -2926,6 +2963,7 @@ private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(ListIsosCmd cm
29262963
cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags);
29272964
}
29282965

2966+
@Override
29292967
public ListResponse<AffinityGroupResponse> listAffinityGroups(Long affinityGroupId, String affinityGroupName,
29302968
String affinityGroupType, Long vmId, String accountName, Long domainId, boolean isRecursive,
29312969
boolean listAll, Long startIndex, Long pageSize) {
@@ -2971,7 +3009,7 @@ public Pair<List<AffinityGroupJoinVO>, Integer> listAffinityGroupsInternal(Long
29713009
listProjectResourcesCriteria);
29723010

29733011
groupSearch.select(null, Func.DISTINCT, groupSearch.entity().getId()); // select
2974-
// distinct
3012+
// distinct
29753013

29763014
SearchCriteria<AffinityGroupJoinVO> sc = groupSearch.create();
29773015
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts,
@@ -3026,6 +3064,7 @@ private Pair<List<AffinityGroupJoinVO>, Integer> listAffinityGroupsByVM(long vmI
30263064
return new Pair<List<AffinityGroupJoinVO>, Integer>(ags, count);
30273065
}
30283066

3067+
@Override
30293068
public List<ResourceDetailResponse> listResource(ListResourceDetailsCmd cmd) {
30303069

30313070
String key = cmd.getKey();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
import org.apache.cloudstack.api.response.TemplateResponse;
22+
2223
import com.cloud.api.query.vo.TemplateJoinVO;
2324
import com.cloud.template.VirtualMachineTemplate;
2425
import com.cloud.utils.db.GenericDao;
@@ -37,8 +38,8 @@ public interface TemplateJoinDao extends GenericDao<TemplateJoinVO, Long> {
3738

3839
List<TemplateJoinVO> newTemplateView(VirtualMachineTemplate tmpl, long zoneId, boolean readyOnly);
3940

40-
List<TemplateJoinVO> searchByIds(Long... ids);
41+
List<TemplateJoinVO> searchByTemplateZonePair(String... pairs);
4142

4243
List<TemplateJoinVO> listActiveTemplates(long storeId);
4344

44-
}
45+
}

0 commit comments

Comments
 (0)