Skip to content

Commit de0904b

Browse files
committed
Modify AccountResponse to return groups that account belongs to.
1 parent d4d3c69 commit de0904b

8 files changed

Lines changed: 447 additions & 35 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,9 @@ public void setDefaultZone(String defaultZoneId) {
438438
public void setIsDefault(Boolean isDefault) {
439439
this.isDefault = isDefault;
440440
}
441+
442+
public void setGroups(List<AclGroupResponse> groups) {
443+
this.groups = groups;
444+
}
445+
441446
}

‎client/tomcatconf/applicationContext.xml.in‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@
376376
<bean id="AclGroupAccountMapDaoImpl" class="org.apache.cloudstack.acl.dao.AclGroupAccountMapDaoImpl"/>
377377
<bean id="AclGroupRoleMapDaoImpl" class="org.apache.cloudstack.acl.dao.AclGroupRoleMapDaoImpl"/>
378378
<bean id="AclApiPermissionDaoImpl" class="org.apache.cloudstack.acl.dao.AclApiPermissionDaoImpl"/>
379-
<bean id="AclEntityPermissionDaoImpl" class="org.apache.cloudstack.acl.dao.AclEntityPermissionDaoImpl"/>
379+
<bean id="AclEntityPermissionDaoImpl" class="org.apache.cloudstack.acl.dao.AclEntityPermissionDaoImpl"/>
380+
<bean id="AclServiceImpl" class="org.apache.cloudstack.acl.AclServiceImpl"/>
380381

381382

382383
<!--

‎server/src/com/cloud/api/ApiDBUtils.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ void init() {
641641
_serviceOfferingDetailsDao = serviceOfferingDetailsDao;
642642
_accountService = accountService;
643643
_aclRoleJoinDao = aclRoleJoinDao;
644+
_aclGroupJoinDao = aclGroupJoinDao;
644645
}
645646

646647
// ///////////////////////////////////////////////////////////
@@ -1512,6 +1513,10 @@ public static List<UserAccountJoinVO> findUserViewByAccountId(Long accountId){
15121513
return _userAccountJoinDao.searchByAccountId(accountId);
15131514
}
15141515

1516+
public static List<AclGroupJoinVO> findAclGroupByAccountId(long accountId) {
1517+
return _aclGroupJoinDao.findAclGroupsByAccount(accountId);
1518+
}
1519+
15151520
public static ProjectAccountResponse newProjectAccountResponse(ProjectAccountJoinVO proj) {
15161521
return _projectAccountJoinDao.newProjectAccountResponse(proj);
15171522
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
import javax.ejb.Local;
2222
import javax.inject.Inject;
2323

24+
import org.apache.log4j.Logger;
25+
import org.springframework.stereotype.Component;
26+
2427
import org.apache.cloudstack.api.response.AccountResponse;
28+
import org.apache.cloudstack.api.response.AclGroupResponse;
2529
import org.apache.cloudstack.api.response.ResourceLimitAndCountResponse;
2630
import org.apache.cloudstack.api.response.UserResponse;
27-
import org.apache.log4j.Logger;
28-
import org.springframework.stereotype.Component;
2931

3032
import com.cloud.api.ApiDBUtils;
3133
import com.cloud.api.query.ViewResponseHelper;
3234
import com.cloud.api.query.vo.AccountJoinVO;
35+
import com.cloud.api.query.vo.AclGroupJoinVO;
3336
import com.cloud.api.query.vo.UserAccountJoinVO;
3437
import com.cloud.configuration.Resource.ResourceType;
3538
import com.cloud.user.Account;
@@ -43,7 +46,7 @@
4346
public class AccountJoinDaoImpl extends GenericDaoBase<AccountJoinVO, Long> implements AccountJoinDao {
4447
public static final Logger s_logger = Logger.getLogger(AccountJoinDaoImpl.class);
4548

46-
private SearchBuilder<AccountJoinVO> acctIdSearch;
49+
private final SearchBuilder<AccountJoinVO> acctIdSearch;
4750
@Inject
4851
public AccountManager _accountMgr;
4952

@@ -53,7 +56,7 @@ protected AccountJoinDaoImpl() {
5356
acctIdSearch.and("id", acctIdSearch.entity().getId(), SearchCriteria.Op.EQ);
5457
acctIdSearch.done();
5558

56-
this._count = "select count(distinct id) from account_view WHERE ";
59+
_count = "select count(distinct id) from account_view WHERE ";
5760
}
5861

5962
@Override
@@ -100,6 +103,11 @@ public AccountResponse newAccountResponse(AccountJoinVO account) {
100103
accountResponse.setDetails(ApiDBUtils.getAccountDetails(account.getId()));
101104
accountResponse.setObjectName("account");
102105

106+
// add all the acl groups for an account
107+
List<AclGroupJoinVO> groupsForAccount = ApiDBUtils.findAclGroupByAccountId(account.getId());
108+
List<AclGroupResponse> groupResponses = ViewResponseHelper.createAclGroupResponses(groupsForAccount);
109+
accountResponse.setGroups(groupResponses);
110+
103111
return accountResponse;
104112
}
105113

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api.query.dao;
18+
19+
import java.util.List;
20+
21+
import org.apache.cloudstack.acl.AclGroup;
22+
import org.apache.cloudstack.api.response.AclGroupResponse;
23+
24+
import com.cloud.api.query.vo.AclGroupJoinVO;
25+
import com.cloud.utils.db.GenericDao;
26+
27+
public interface AclGroupJoinDao extends GenericDao<AclGroupJoinVO, Long> {
28+
29+
AclGroupResponse newAclGroupResponse(AclGroupJoinVO group);
30+
31+
AclGroupResponse setAclGroupResponse(AclGroupResponse response, AclGroupJoinVO os);
32+
33+
List<AclGroupJoinVO> newAclGroupView(AclGroup group);
34+
35+
List<AclGroupJoinVO> searchByIds(Long... ids);
36+
37+
List<AclGroupJoinVO> findAclGroupsByAccount(long accountId);
38+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api.query.dao;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashSet;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
import javax.ejb.Local;
25+
import javax.inject.Inject;
26+
27+
import org.apache.log4j.Logger;
28+
import org.springframework.stereotype.Component;
29+
30+
import org.apache.cloudstack.acl.AclGroup;
31+
import org.apache.cloudstack.acl.AclGroupAccountMapVO;
32+
import org.apache.cloudstack.acl.dao.AclGroupAccountMapDao;
33+
import org.apache.cloudstack.api.response.AclGroupResponse;
34+
import org.apache.cloudstack.api.response.AclRoleResponse;
35+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
36+
37+
import com.cloud.api.query.vo.AclGroupJoinVO;
38+
import com.cloud.user.AccountManager;
39+
import com.cloud.utils.db.GenericDaoBase;
40+
import com.cloud.utils.db.SearchBuilder;
41+
import com.cloud.utils.db.SearchCriteria;
42+
43+
@Component
44+
@Local(value = {AclGroupJoinDao.class})
45+
public class AclGroupJoinDaoImpl extends GenericDaoBase<AclGroupJoinVO, Long> implements AclGroupJoinDao {
46+
public static final Logger s_logger = Logger.getLogger(AclGroupJoinDaoImpl.class);
47+
48+
49+
private final SearchBuilder<AclGroupJoinVO> grpIdSearch;
50+
private final SearchBuilder<AclGroupJoinVO> grpSearch;
51+
@Inject
52+
public AccountManager _accountMgr;
53+
@Inject
54+
public ConfigurationDao _configDao;
55+
@Inject
56+
public AclGroupAccountMapDao _grpAccountDao;
57+
58+
protected AclGroupJoinDaoImpl() {
59+
60+
grpSearch = createSearchBuilder();
61+
grpSearch.and("idIN", grpSearch.entity().getId(), SearchCriteria.Op.IN);
62+
grpSearch.done();
63+
64+
grpIdSearch = createSearchBuilder();
65+
grpIdSearch.and("id", grpIdSearch.entity().getId(), SearchCriteria.Op.EQ);
66+
grpIdSearch.done();
67+
68+
_count = "select count(distinct id) from acl_group_view WHERE ";
69+
}
70+
71+
72+
73+
@Override
74+
public AclGroupResponse newAclGroupResponse(AclGroupJoinVO group) {
75+
76+
AclGroupResponse response = new AclGroupResponse();
77+
response.setId(group.getUuid());
78+
response.setName(group.getName());
79+
response.setDescription(group.getDescription());
80+
response.setDomainId(group.getDomainUuid());
81+
response.setDomainName(group.getName());
82+
if (group.getAccountId() > 0) {
83+
response.addAccountId(group.getAccountUuid());
84+
}
85+
if (group.getRoleId() > 0) {
86+
AclRoleResponse roleResp = new AclRoleResponse();
87+
roleResp.setId(group.getRoleUuid());
88+
roleResp.setName(group.getRoleName());
89+
response.addRole(roleResp);
90+
}
91+
response.setObjectName("aclgroup");
92+
93+
94+
return response;
95+
}
96+
97+
@Override
98+
public AclGroupResponse setAclGroupResponse(AclGroupResponse response, AclGroupJoinVO group) {
99+
if (group.getAccountId() > 0) {
100+
response.addAccountId(group.getAccountUuid());
101+
}
102+
if (group.getRoleId() > 0) {
103+
AclRoleResponse roleResp = new AclRoleResponse();
104+
roleResp.setId(group.getRoleUuid());
105+
roleResp.setName(group.getRoleName());
106+
response.addRole(roleResp);
107+
}
108+
return response;
109+
}
110+
111+
@Override
112+
public List<AclGroupJoinVO> newAclGroupView(AclGroup group) {
113+
SearchCriteria<AclGroupJoinVO> sc = grpIdSearch.create();
114+
sc.setParameters("id", group.getId());
115+
return searchIncludingRemoved(sc, null, null, false);
116+
117+
}
118+
119+
@Override
120+
public List<AclGroupJoinVO> searchByIds(Long... grpIds) {
121+
// set detail batch query size
122+
int DETAILS_BATCH_SIZE = 2000;
123+
String batchCfg = _configDao.getValue("detail.batch.query.size");
124+
if (batchCfg != null) {
125+
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
126+
}
127+
// query details by batches
128+
List<AclGroupJoinVO> uvList = new ArrayList<AclGroupJoinVO>();
129+
// query details by batches
130+
int curr_index = 0;
131+
if (grpIds.length > DETAILS_BATCH_SIZE) {
132+
while ((curr_index + DETAILS_BATCH_SIZE) <= grpIds.length) {
133+
Long[] ids = new Long[DETAILS_BATCH_SIZE];
134+
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
135+
ids[k] = grpIds[j];
136+
}
137+
SearchCriteria<AclGroupJoinVO> sc = grpSearch.create();
138+
sc.setParameters("idIN", ids);
139+
List<AclGroupJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
140+
if (vms != null) {
141+
uvList.addAll(vms);
142+
}
143+
curr_index += DETAILS_BATCH_SIZE;
144+
}
145+
}
146+
if (curr_index < grpIds.length) {
147+
int batch_size = (grpIds.length - curr_index);
148+
// set the ids value
149+
Long[] ids = new Long[batch_size];
150+
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
151+
ids[k] = grpIds[j];
152+
}
153+
SearchCriteria<AclGroupJoinVO> sc = grpSearch.create();
154+
sc.setParameters("idIN", ids);
155+
List<AclGroupJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
156+
if (vms != null) {
157+
uvList.addAll(vms);
158+
}
159+
}
160+
return uvList;
161+
}
162+
163+
@Override
164+
public List<AclGroupJoinVO> findAclGroupsByAccount(long accountId) {
165+
List<AclGroupAccountMapVO> grpMap = _grpAccountDao.listByAccountId(accountId);
166+
if (grpMap != null && grpMap.size() > 0) {
167+
Set<Long> grpList = new HashSet<Long>();
168+
for (AclGroupAccountMapVO m : grpMap) {
169+
grpList.add(m.getAclGroupId());
170+
}
171+
return searchByIds(grpList.toArray(new Long[grpList.size()]));
172+
}
173+
else{
174+
return null;
175+
}
176+
}
177+
178+
}

0 commit comments

Comments
 (0)