Refactor listUsers to new API framework.

This commit is contained in:
Kris McQueen 2010-09-08 15:42:51 -07:00
parent b40f496f80
commit bdebd4bfa3
5 changed files with 99 additions and 119 deletions

View File

@ -20,38 +20,21 @@ package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseListCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.domain.DomainVO;
import com.cloud.server.Criteria;
import com.cloud.user.Account;
import com.cloud.api.response.UserResponse;
import com.cloud.serializer.SerializerHelper;
import com.cloud.user.UserAccountVO;
import com.cloud.utils.Pair;
public class ListUsersCmd extends BaseCmd {
@Implementation(method="searchForUsers")
public class ListUsersCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListUsersCmd.class.getName());
private static final String s_name = "listusersresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_TYPE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.STATE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USERNAME, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.KEYWORD, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -107,88 +90,36 @@ public class ListUsersCmd extends BaseCmd {
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getName() {
return s_name;
}
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Long id = (Long)params.get(BaseCmd.Properties.ID.getName());
String userName = (String)params.get(BaseCmd.Properties.USERNAME.getName());
String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName());
Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
Long type = (Long)params.get(BaseCmd.Properties.ACCOUNT_TYPE.getName());
String state = (String)params.get(BaseCmd.Properties.STATE.getName());
String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName());
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
if (domainId != null) {
if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list users.");
}
} else {
// default domainId to the admin's domain
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
}
Long startIndex = Long.valueOf(0);
int pageSizeNum = 50;
if (pageSize != null) {
pageSizeNum = pageSize.intValue();
}
if (page != null) {
int pageNum = page.intValue();
if (pageNum > 0) {
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
}
}
Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum));
if (keyword != null) {
c.addCriteria(Criteria.KEYWORD, keyword);
}
else {
c.addCriteria(Criteria.ID, id);
c.addCriteria(Criteria.ACCOUNTNAME, accountName);
c.addCriteria(Criteria.DOMAINID, domainId);
c.addCriteria(Criteria.USERNAME, userName);
c.addCriteria(Criteria.TYPE, type);
c.addCriteria(Criteria.STATE, state);
}
List<UserAccountVO> users = getManagementServer().searchForUsers(c);
List<Pair<String, Object>> userTags = new ArrayList<Pair<String, Object>>();
Object[] uTag = new Object[users.size()];
int i = 0;
for (UserAccountVO user: users) {
if ((user.getRemoved() == null)&&(user.getId() != 1)) {
List<Pair<String, Object>> userData = new ArrayList<Pair<String, Object>>();
userData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), Long.valueOf(user.getId()).toString()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.USERNAME.getName(), user.getUsername()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.FIRSTNAME.getName(), user.getFirstname()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.LASTNAME.getName(), user.getLastname()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.EMAIL.getName(), user.getEmail()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(user.getCreated())));
userData.add(new Pair<String, Object>(BaseCmd.Properties.STATE.getName(), user.getState()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), getManagementServer().findAccountById(user.getAccountId()).getAccountName()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT_TYPE.getName(), getManagementServer().findAccountById(user.getAccountId()).getType()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), user.getDomainId().toString()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(user.getDomainId()).getName()));
userData.add(new Pair<String, Object>(BaseCmd.Properties.TIMEZONE.getName(),user.getTimezone()));
if(user.getApiKey()!=null)
userData.add(new Pair<String, Object>(BaseCmd.Properties.API_KEY.getName(), user.getApiKey()));
if(user.getSecretKey()!=null)
userData.add(new Pair<String, Object>(BaseCmd.Properties.SECRET_KEY.getName(), user.getSecretKey()));
uTag[i++] = userData;
}
}
Pair<String, Object> userTag = new Pair<String, Object>("user", uTag);
userTags.add(userTag);
return userTags;
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<UserAccountVO> users = (List<UserAccountVO>)getResponseObject();
List<UserResponse> response = new ArrayList<UserResponse>();
for (UserAccountVO user : users) {
UserResponse userResponse = new UserResponse();
userResponse.setId(user.getId());
userResponse.setUsername(user.getUsername());
userResponse.setFirstname(user.getFirstname());
userResponse.setLastname(user.getLastname());
userResponse.setEmail(user.getEmail());
userResponse.setCreated(user.getCreated());
userResponse.setState(user.getState());
userResponse.setAccountName(user.getAccountName());
userResponse.setAccountType(user.getType());
userResponse.setDomainId(user.getDomainId());
userResponse.setDomainName(getManagementServer().findDomainIdById(user.getDomainId()).getName());
userResponse.setTimezone(user.getTimezone());
userResponse.setApiKey(user.getApiKey());
userResponse.setSecretKey(user.getSecretKey());
response.add(userResponse);
}
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -1,3 +1,20 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.api.response;
import java.util.List;

View File

@ -59,7 +59,11 @@ public class UserResponse implements ResponseObject {
@Param(name="timezone")
private String timezone;
// TODO: user keys?
@Param(name="apikey")
private String apiKey;
@Param(name="secretkey")
private String secretKey;
public Long getId() {
return id;
@ -156,4 +160,20 @@ public class UserResponse implements ResponseObject {
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}

View File

@ -59,6 +59,7 @@ import com.cloud.api.commands.ListStoragePoolsCmd;
import com.cloud.api.commands.ListSystemVMsCmd;
import com.cloud.api.commands.ListTemplateOrIsoPermissionsCmd;
import com.cloud.api.commands.ListTemplatesCmd;
import com.cloud.api.commands.ListUsersCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.RebootSystemVmCmd;
@ -870,10 +871,10 @@ public interface ManagementServer {
/** revisit
* Searches for users by the specified search criteria
* Can search by: "id", "username", "account", "domainId", "type"
* @param c
* @param cmd
* @return List of UserAccounts
*/
List<UserAccountVO> searchForUsers(Criteria c);
List<UserAccountVO> searchForUsers(ListUsersCmd cmd) throws PermissionDeniedException;
/**
* Searches for Service Offerings by the specified search criteria

View File

@ -95,6 +95,7 @@ import com.cloud.api.commands.ListStoragePoolsCmd;
import com.cloud.api.commands.ListSystemVMsCmd;
import com.cloud.api.commands.ListTemplateOrIsoPermissionsCmd;
import com.cloud.api.commands.ListTemplatesCmd;
import com.cloud.api.commands.ListUsersCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
@ -3762,16 +3763,26 @@ public class ManagementServerImpl implements ManagementServer {
}
@Override
public List<UserAccountVO> searchForUsers(Criteria c) {
Filter searchFilter = new Filter(UserAccountVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
public List<UserAccountVO> searchForUsers(ListUsersCmd cmd) throws PermissionDeniedException {
Account account = (Account)UserContext.current().getAccountObject();
Long domainId = cmd.getDomainId();
if (domainId != null) {
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
throw new PermissionDeniedException("Invalid domain id (" + domainId + ") given, unable to list users.");
}
} else {
// default domainId to the admin's domain
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
}
Object id = c.getCriteria(Criteria.ID);
Object username = c.getCriteria(Criteria.USERNAME);
Object type = c.getCriteria(Criteria.TYPE);
Object domainId = c.getCriteria(Criteria.DOMAINID);
Object account = c.getCriteria(Criteria.ACCOUNTNAME);
Object state = c.getCriteria(Criteria.STATE);
Object keyword = c.getCriteria(Criteria.KEYWORD);
Filter searchFilter = new Filter(UserAccountVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
Object id = cmd.getId();
Object username = cmd.getUsername();
Object type = cmd.getAccountType();
Object accountName = cmd.getAccountName();
Object state = cmd.getState();
Object keyword = cmd.getKeyword();
SearchBuilder<UserAccountVO> sb = _userAccountDao.createSearchBuilder();
sb.and("username", sb.entity().getUsername(), SearchCriteria.Op.LIKE);
@ -3781,7 +3792,7 @@ public class ManagementServerImpl implements ManagementServer {
sb.and("accountName", sb.entity().getAccountName(), SearchCriteria.Op.LIKE);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
if ((account == null) && (domainId != null)) {
if ((accountName == null) && (domainId != null)) {
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId());
@ -3814,8 +3825,8 @@ public class ManagementServerImpl implements ManagementServer {
sc.setParameters("type", type);
}
if (account != null) {
sc.setParameters("accountName", "%" + account + "%");
if (accountName != null) {
sc.setParameters("accountName", "%" + accountName + "%");
if (domainId != null) {
sc.setParameters("domainId", domainId);
}