Skip to content

Commit 784d1e1

Browse files
continue implementing the listIacTemplates API
1 parent 9b7f77c commit 784d1e1

8 files changed

Lines changed: 96 additions & 8 deletions

File tree

plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/command/ListIacTemplatesCmd.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.cloudstack.api.response.IacTemplateResponse;
1313
import org.apache.cloudstack.api.response.ListResponse;
1414
import org.apache.cloudstack.api.response.ProjectResponse;
15-
import org.apache.cloudstack.context.CallContext;
1615
import org.apache.cloudstack.persistence.iactemplates.IacTemplate;
1716
import org.apache.cloudstack.service.NimbleService;
1817

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplates/IacTemplateAccountMapDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
public interface IacTemplateAccountMapDao extends GenericDao<IacTemplateAccountMapVO, Long> {
2424

2525
List<IacTemplateAccountMapVO> listByIacTemplateId(long iacTemplateId);
26+
List<IacTemplateAccountMapVO> listByAccountId(long accountId);
2627
void removeByIacTemplateId(long iacTemplateId);
2728
void removeByAccountId(long accountId);
2829
void removeUserAccountMappingsByIacTemplateId(long iacTemplateId);

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplates/IacTemplateAccountMapDaoImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public List<IacTemplateAccountMapVO> listByIacTemplateId(long iacTemplateId) {
4646
return listBy(searchCriteria);
4747
}
4848

49+
@Override
50+
public List<IacTemplateAccountMapVO> listByAccountId(long accountId) {
51+
SearchCriteria<IacTemplateAccountMapVO> searchCriteria = accountMappingSearch.create();
52+
searchCriteria.setParameters(ACCOUNT_ID, accountId);
53+
return listBy(searchCriteria);
54+
}
55+
4956
@Override
5057
public void removeByIacTemplateId(long iacTemplateId) {
5158
SearchCriteria<IacTemplateAccountMapVO> searchCriteria = accountMappingSearch.create();

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplates/IacTemplateDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
// under the License.
1717
package org.apache.cloudstack.persistence.iactemplates;
1818

19+
import com.cloud.utils.Pair;
1920
import com.cloud.utils.db.GenericDao;
2021

2122
import java.util.List;
2223

2324
public interface IacTemplateDao extends GenericDao<IacTemplateVO, Long> {
2425
void removeByAccountId(long accountId);
2526
List<IacTemplateVO> listByAccountId(long accountId);
27+
Pair<List<IacTemplateVO>, Integer> listIacTemplates(Long id, String name, List<Long> domainIds, Long accountId,
28+
boolean showIacTemplateContent, boolean showSharedIacTemplates,
29+
String keyword, Long pageSizeVal, Long startIndex);
2630
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplates/IacTemplateDaoImpl.java

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

19+
import com.cloud.utils.Pair;
1920
import com.cloud.utils.db.DB;
21+
import com.cloud.utils.db.Filter;
2022
import com.cloud.utils.db.GenericDaoBase;
2123
import com.cloud.utils.db.SearchBuilder;
2224
import com.cloud.utils.db.SearchCriteria;
@@ -27,7 +29,12 @@
2729

2830
@Component
2931
public class IacTemplateDaoImpl extends GenericDaoBase<IacTemplateVO, Long> implements IacTemplateDao {
32+
private static final String ID = "id";
33+
private static final String NAME = "name";
34+
private static final String DOMAIN_IDS = "domainIds";
3035
private static final String ACCOUNT_ID = "accountId";
36+
private static final String CREATED = "created";
37+
private static final String NAME_LIKE_KEYWORD = "nameLikeKeyword";
3138

3239
@Inject
3340
private IacTemplateAccountMapDao iacTemplateAccountMapDao;
@@ -39,6 +46,9 @@ public class IacTemplateDaoImpl extends GenericDaoBase<IacTemplateVO, Long> impl
3946

4047
public IacTemplateDaoImpl() {
4148
iacTemplatesSearch = createSearchBuilder();
49+
iacTemplatesSearch.and(ID, iacTemplatesSearch.entity().getId(), SearchCriteria.Op.EQ);
50+
iacTemplatesSearch.and(NAME, iacTemplatesSearch.entity().getName(), SearchCriteria.Op.EQ);
51+
iacTemplatesSearch.and(DOMAIN_IDS, iacTemplatesSearch.entity().getDomainId(), SearchCriteria.Op.IN);
4252
iacTemplatesSearch.and(ACCOUNT_ID, iacTemplatesSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
4353
iacTemplatesSearch.done();
4454
}
@@ -80,4 +90,21 @@ public List<IacTemplateVO> listByAccountId(long accountId) {
8090
searchCriteria.setParameters(ACCOUNT_ID, accountId);
8191
return listBy(searchCriteria);
8292
}
93+
94+
@Override
95+
public Pair<List<IacTemplateVO>, Integer> listIacTemplates(Long id, String name, List<Long> domainIds, Long accountId,
96+
boolean showIacTemplateContent, boolean showSharedIacTemplates,
97+
String keyword, Long pageSizeVal, Long startIndex) {
98+
SearchCriteria<IacTemplateVO> searchCriteria = iacTemplatesSearch.create();
99+
searchCriteria.setParametersIfNotNull(ID, id);
100+
searchCriteria.setParametersIfNotNull(NAME, name);
101+
searchCriteria.setParameters(DOMAIN_IDS, domainIds.toArray());
102+
searchCriteria.setParametersIfNotNull(ACCOUNT_ID, accountId);
103+
if (keyword != null) {
104+
searchCriteria.setParameters(NAME_LIKE_KEYWORD, "%" + keyword + "%");
105+
}
106+
107+
Filter filter = new Filter(IacTemplateVO.class, CREATED, false, startIndex, pageSizeVal);
108+
return null;
109+
}
83110
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplates/IacTemplateDomainMapDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
public interface IacTemplateDomainMapDao extends GenericDao<IacTemplateDomainMapVO, Long> {
2424
List<IacTemplateDomainMapVO> listByIacTemplateId(long iacTemplateId);
25+
List<IacTemplateDomainMapVO> listByDomainId(long domainId);
2526
void removeByIacTemplateId(long iacTemplateId);
2627
void removeByDomainId(long domainId);
2728
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/persistence/iactemplates/IacTemplateDomainMapDaoImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public List<IacTemplateDomainMapVO> listByIacTemplateId(long iacTemplateId) {
4444
return listBy(searchCriteria);
4545
}
4646

47+
@Override
48+
public List<IacTemplateDomainMapVO> listByDomainId(long domainId) {
49+
SearchCriteria<IacTemplateDomainMapVO> searchCriteria = domainMappingSearch.create();
50+
searchCriteria.setParameters(DOMAIN_ID, domainId);
51+
return listBy(searchCriteria);
52+
}
53+
4754
@Override
4855
public void removeByIacTemplateId(long iacTemplateId) {
4956
SearchCriteria<IacTemplateDomainMapVO> searchCriteria = domainMappingSearch.create();

plugins/iac/nimble/src/main/java/org/apache/cloudstack/service/NimbleManagerImpl.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,36 @@ public ListResponse<IacResourceTypeResponse> listIacResourceTypes(ListIacResourc
136136
public ListResponse<IacTemplateResponse> listIacTemplates(ListIacTemplatesCmd cmd) {
137137
Account caller = CallContext.current().getCallingAccount();
138138
long domainId = getBaseDomainIdToListIacTemplatesFrom(cmd.getDomainId(), caller);
139-
Project projectId = getProjectToListIacTemplatesFor(cmd.getProjectId(), caller);
140-
List<Long> domainIds = cmd.isRecursive() ? domainDao.getDomainAndChildrenIds(cmd.getDomainId()) : List.of(cmd.getDomainId());
141-
// ACL validations are ready -> need to understand how to grab those shared templates and that's basically it :)
139+
List<Long> domainIds = cmd.isRecursive() ? domainDao.getDomainAndChildrenIds(domainId) : List.of(domainId);
140+
Long accountId = getAccountIdToListIacTemplatesFor(cmd.getAccountId(), cmd.getProjectId(), caller);
141+
142+
List<Long> sharedIacTemplateIds = new ArrayList<>();
143+
// WHERE id = blabla AND name = blabla AND name LIKE %blabla%
144+
// AND domain_id IN (domain1, domain2, domain3) OR id IN (shareddomainid1, shareddomainid2)
145+
if (cmd.isShowSharedIacTemplates()) {
146+
sharedIacTemplateIds = getListOfSharedIacTemplatesIds(domainId, ObjectUtils.defaultIfNull(accountId, caller.getId()));
147+
}
148+
149+
iacTemplateDao.listIacTemplates(cmd.getId(), cmd.getName(), domainIds, accountId,
150+
cmd.isShowIacTemplateContent(), cmd.isShowSharedIacTemplates(), cmd.getKeyword(),
151+
cmd.getPageSizeVal(), cmd.getStartIndex());
142152

143153
return null;
144154
}
145155

156+
List<Long> getListOfSharedIacTemplatesIds(long domainId, Long accountId) {
157+
List<Long> sharedIacTemplateIds = new ArrayList<>();
158+
159+
iacTemplateAccountMapDao.listByAccountId(accountId).stream()
160+
.map(IacTemplateAccountMapVO::getIacTemplateId)
161+
.forEach(sharedIacTemplateIds::add);
162+
iacTemplateDomainMapDao.listByDomainId(domainId).stream()
163+
.map(IacTemplateDomainMapVO::getIacTemplateId)
164+
.forEach(sharedIacTemplateIds::add);
165+
166+
return sharedIacTemplateIds;
167+
}
168+
146169
private long getBaseDomainIdToListIacTemplatesFrom(Long domainId, Account caller) {
147170
if (domainId == null) {
148171
return caller.getDomainId();
@@ -157,11 +180,27 @@ private long getBaseDomainIdToListIacTemplatesFrom(Long domainId, Account caller
157180
return domainId;
158181
}
159182

160-
private Project getProjectToListIacTemplatesFor(Long projectId, Account caller) {
161-
if (projectId == null) {
162-
return null;
183+
private Long getAccountIdToListIacTemplatesFor(Long accountId, Long projectId, Account caller) {
184+
if (ObjectUtils.allNotNull(accountId, projectId)) {
185+
throw new InvalidParameterValueException("Parameters [accountid, projectid] cannot be specified together. Please specify only one of them.");
186+
}
187+
188+
if (projectId != null) {
189+
return getProjectAccountIdToListIacTemplatesFor(projectId, caller);
190+
}
191+
192+
if (accountId != null) {
193+
return accountId;
163194
}
164195

196+
if (accountService.isNormalUser(caller.getId())) {
197+
return caller.getId();
198+
}
199+
200+
return null;
201+
}
202+
203+
private long getProjectAccountIdToListIacTemplatesFor(long projectId, Account caller) {
165204
Project project = projectManager.getProject(projectId);
166205
if (project == null) {
167206
throw new InvalidParameterValueException("Unable to find the specified project.");
@@ -176,7 +215,7 @@ private Project getProjectToListIacTemplatesFor(Long projectId, Account caller)
176215
throw new InvalidParameterValueException(exceptionMessage);
177216
}
178217

179-
return project;
218+
return project.getProjectAccountId();
180219
}
181220

182221
private boolean doesUserHaveAccessToNodeTypeApis(Pair<String, String> nodeTypeApis) {
@@ -290,6 +329,9 @@ private IacTemplate persistIacTemplate(BaseIacTemplateRegistrationCmd cmd, Accou
290329
}
291330

292331
private void persistDomainMappings(Set<Long> sharedDomainIds, long iacTemplateId, Account iacTemplateOwner) {
332+
333+
334+
// alterar aqui para ja fazer o spread sobre os dominios recursivamente
293335
for (Long domainId : sharedDomainIds) {
294336
Domain domain = domainManager.getDomain(domainId);
295337
if (domain == null) {

0 commit comments

Comments
 (0)