From bdebd4bfa30a4a917660d71dd03fcd9a13d92127 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Wed, 8 Sep 2010 15:42:51 -0700 Subject: [PATCH] Refactor listUsers to new API framework. --- .../com/cloud/api/commands/ListUsersCmd.java | 139 +++++------------- .../response/TemplatePermissionsResponse.java | 17 +++ .../com/cloud/api/response/UserResponse.java | 22 ++- .../com/cloud/server/ManagementServer.java | 5 +- .../cloud/server/ManagementServerImpl.java | 35 +++-- 5 files changed, 99 insertions(+), 119 deletions(-) diff --git a/server/src/com/cloud/api/commands/ListUsersCmd.java b/server/src/com/cloud/api/commands/ListUsersCmd.java index d88964f2647..130cede6e27 100644 --- a/server/src/com/cloud/api/commands/ListUsersCmd.java +++ b/server/src/com/cloud/api/commands/ListUsersCmd.java @@ -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> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_TYPE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.STATE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USERNAME, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(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> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map 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 users = getManagementServer().searchForUsers(c); - - List> userTags = new ArrayList>(); - Object[] uTag = new Object[users.size()]; - int i = 0; - for (UserAccountVO user: users) { - if ((user.getRemoved() == null)&&(user.getId() != 1)) { - List> userData = new ArrayList>(); - userData.add(new Pair(BaseCmd.Properties.ID.getName(), Long.valueOf(user.getId()).toString())); - userData.add(new Pair(BaseCmd.Properties.USERNAME.getName(), user.getUsername())); - userData.add(new Pair(BaseCmd.Properties.FIRSTNAME.getName(), user.getFirstname())); - userData.add(new Pair(BaseCmd.Properties.LASTNAME.getName(), user.getLastname())); - userData.add(new Pair(BaseCmd.Properties.EMAIL.getName(), user.getEmail())); - userData.add(new Pair(BaseCmd.Properties.CREATED.getName(), getDateString(user.getCreated()))); - userData.add(new Pair(BaseCmd.Properties.STATE.getName(), user.getState())); - userData.add(new Pair(BaseCmd.Properties.ACCOUNT.getName(), getManagementServer().findAccountById(user.getAccountId()).getAccountName())); - userData.add(new Pair(BaseCmd.Properties.ACCOUNT_TYPE.getName(), getManagementServer().findAccountById(user.getAccountId()).getType())); - userData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), user.getDomainId().toString())); - userData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(user.getDomainId()).getName())); - userData.add(new Pair(BaseCmd.Properties.TIMEZONE.getName(),user.getTimezone())); - if(user.getApiKey()!=null) - userData.add(new Pair(BaseCmd.Properties.API_KEY.getName(), user.getApiKey())); - if(user.getSecretKey()!=null) - userData.add(new Pair(BaseCmd.Properties.SECRET_KEY.getName(), user.getSecretKey())); - uTag[i++] = userData; - } - } - Pair userTag = new Pair("user", uTag); - userTags.add(userTag); - return userTags; - + + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List users = (List)getResponseObject(); + + List response = new ArrayList(); + 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); } } diff --git a/server/src/com/cloud/api/response/TemplatePermissionsResponse.java b/server/src/com/cloud/api/response/TemplatePermissionsResponse.java index cd955d075af..66b17a7a810 100644 --- a/server/src/com/cloud/api/response/TemplatePermissionsResponse.java +++ b/server/src/com/cloud/api/response/TemplatePermissionsResponse.java @@ -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 . + * + */ package com.cloud.api.response; import java.util.List; diff --git a/server/src/com/cloud/api/response/UserResponse.java b/server/src/com/cloud/api/response/UserResponse.java index 7405c188329..86eacdac46c 100644 --- a/server/src/com/cloud/api/response/UserResponse.java +++ b/server/src/com/cloud/api/response/UserResponse.java @@ -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; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index abbc681c33b..a77ed6bb8a9 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -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 searchForUsers(Criteria c); + List searchForUsers(ListUsersCmd cmd) throws PermissionDeniedException; /** * Searches for Service Offerings by the specified search criteria diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 72497fa31d8..34ebf8c313c 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -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 searchForUsers(Criteria c) { - Filter searchFilter = new Filter(UserAccountVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit()); + public List 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 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 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); }