Refactor createUser to new API framework. Consolidate the old createUser (GWT UI used this API) and createUserAPI into one createUser method since it's only called through integration API now. Return a UserAccount instead of a User in order to generate a response with all the information required. Also clean up some of the unused private template code from ManagementServer that was mistakenly left in from a previous refactor.

This commit is contained in:
Kris McQueen 2010-08-23 17:33:33 -07:00
parent defae59126
commit c085283c2e
4 changed files with 185 additions and 127 deletions

View File

@ -18,38 +18,20 @@
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.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.domain.DomainVO;
import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.api.response.UserResponse;
import com.cloud.serializer.SerializerHelper;
import com.cloud.user.UserAccount;
@Implementation(method="createUser")
public class CreateUserCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(CreateUserCmd.class.getName());
private static final String s_name = "createuserresponse";
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.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.EMAIL, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.FIRSTNAME, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.LASTNAME, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PASSWORD, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.TIMEZONE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USERNAME, Boolean.TRUE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -128,75 +110,31 @@ public class CreateUserCmd 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) {
String username = (String)params.get(BaseCmd.Properties.USERNAME.getName());
String password = (String)params.get(BaseCmd.Properties.PASSWORD.getName());
String firstname = (String)params.get(BaseCmd.Properties.FIRSTNAME.getName());
String lastname = (String)params.get(BaseCmd.Properties.LASTNAME.getName());
String email = (String)params.get(BaseCmd.Properties.EMAIL.getName());
Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName());
Long accountType = (Long)params.get(BaseCmd.Properties.ACCOUNT_TYPE.getName());
String timezone = (String)params.get(BaseCmd.Properties.TIMEZONE.getName());
public String getResponse() {
UserAccount user = (UserAccount)getResponseObject();
// Check the domainId
if (domainId == null) {
domainId = DomainVO.ROOT_DOMAIN;
}
// TODO: user keys?
UserResponse response = new UserResponse();
response.setAccountName(user.getAccountName());
response.setAccountType(user.getType());
response.setCreated(user.getCreated());
response.setDomainId(user.getDomainId());
// TODO: implement
// response.setDomainName(user.getDomainName());
response.setEmail(user.getEmail());
response.setFirstname(user.getFirstname());
response.setId(user.getId());
response.setLastname(user.getLastname());
response.setState(user.getState());
response.setTimezone(user.getTimezone());
response.setUsername(user.getUsername());
//Verify if the account exists
if (accountName != null) {
Account account = getManagementServer().findActiveAccount(accountName, domainId);
if (account !=null) {
accountType = Long.valueOf((long)account.getType());
}
}
else {
accountName = username;
}
User createdUser = null;
try {
createdUser = getManagementServer().createUserAPI(username, password, firstname, lastname, domainId, accountName, accountType.shortValue(), email, timezone);
} catch (CloudRuntimeException ex) {
if (s_logger.isInfoEnabled()) {
s_logger.info("exception creating user: " + ex);
}
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, ex.getMessage());
}
if (createdUser == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "failed to create user");
} else {
Account userAccount = getManagementServer().findAccountById(Long.valueOf(createdUser.getAccountId()));
if (userAccount != null) {
domainId = userAccount.getDomainId();
accountName = userAccount.getAccountName();
}
}
List<Pair<String, Object>> embeddedObject = new ArrayList<Pair<String, Object>>();
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), createdUser.getId().toString()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.USERNAME.getName(), createdUser.getUsername()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.FIRSTNAME.getName(), createdUser.getFirstname()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.LASTNAME.getName(), createdUser.getLastname()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.EMAIL.getName(), createdUser.getEmail()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(createdUser.getCreated())));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.STATE.getName(), createdUser.getState()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountName));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT_TYPE.getName(), accountType));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), domainId.toString()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(domainId).getName()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.TIMEZONE.getName(),createdUser.getTimezone()));
embeddedObject.add(new Pair<String, Object>("user", new Object[] { returnValues } ));
return embeddedObject;
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -0,0 +1,142 @@
package com.cloud.api.response;
import java.util.Date;
import com.cloud.api.ResponseObject;
import com.cloud.serializer.Param;
public class UserResponse implements ResponseObject {
@Param(name="id")
private Long id;
@Param(name="username")
private String username;
@Param(name="firstname")
private String firstname;
@Param(name="lastname")
private String lastname;
@Param(name="email")
private String email;
@Param(name="created")
private Date created;
@Param(name="state")
private String state;
@Param(name="account")
private String accountName;
@Param(name="accounttype")
private Short accountType;
@Param(name="domainid")
private Long domainId;
@Param(name="domain")
private String domainName;
@Param(name="timezone")
private String timezone;
// TODO: user keys?
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Short getAccountType() {
return accountType;
}
public void setAccountType(Short accountType) {
this.accountType = accountType;
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
}

View File

@ -26,6 +26,7 @@ import com.cloud.alert.AlertVO;
import com.cloud.api.commands.CreateDomainCmd;
import com.cloud.api.commands.CreatePortForwardingServiceCmd;
import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd;
import com.cloud.api.commands.CreateUserCmd;
import com.cloud.api.commands.DisassociateIPAddrCmd;
import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd;
@ -39,7 +40,6 @@ import com.cloud.api.commands.StartSystemVMCmd;
import com.cloud.api.commands.StopSystemVmCmd;
import com.cloud.api.commands.UpdateAccountCmd;
import com.cloud.api.commands.UpdateDomainCmd;
import com.cloud.api.commands.UpdateTemplateCmd;
import com.cloud.api.commands.UpdateTemplateOrIsoCmd;
import com.cloud.api.commands.UpdateTemplateOrIsoPermissionsCmd;
import com.cloud.api.commands.UpdateUserCmd;
@ -58,7 +58,6 @@ import com.cloud.dc.VlanVO;
import com.cloud.domain.DomainVO;
import com.cloud.event.EventVO;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.DiscoveryException;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.InsufficientStorageCapacityException;
import com.cloud.exception.InternalErrorException;
@ -68,7 +67,6 @@ import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceInUseException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.Host;
import com.cloud.host.HostStats;
import com.cloud.host.HostVO;
import com.cloud.info.ConsoleProxyInfo;
@ -126,39 +124,14 @@ public interface ManagementServer {
static final String Name = "management-server";
/**
* Creates a new user, encrypts the password on behalf of the caller
*
* @param username username
* @param password the user's password
* @param firstName the user's first name
* @param lastName the user's last name
* @param domain the id of the domain that this user belongs to
* @param accountName the name(a.k.a. id) of the account that this user belongs to
* @param timezone the user's current timezone (default: PST)
* @return a user object
* Creates a new user, stores the password as is so encrypted passwords are recommended.
* @param cmd the create command that has the username, email, password, account name, domain, timezone, etc. for creating the user.
* @return the user if created successfully, null otherwise
*/
User createUser(String username, String password, String firstName, String lastName, Long domain, String accountName, short userType, String email, String timezone);
// boolean reconnect(long hostId);
// long reconnectAsync(long hostId);
ClusterVO findClusterById(long clusterId);
List<ClusterVO> listClusterByPodId(long podId);
// ClusterVO createCluster(long dcId, long podId, String name);
UserAccount createUser(CreateUserCmd cmd);
/**
* Creates a new user, does not encrypt the password
*
* @param username username
* @param password the user's password
* @param firstName the user's first name
* @param lastName the user's last name
* @param domain the id of the domain that this user belongs to FIXME: if we have account, do we also need domain?
* @param accountName the name(a.k.a. id) of the account that this user belongs to
* @param timezone the user's current timezone (default: PST)
* @return a user object
*/
User createUserAPI(String username, String password, String firstName, String lastName, Long domain, String accountName, short userType, String email, String timezone);
ClusterVO findClusterById(long clusterId);
List<ClusterVO> listClusterByPodId(long podId);
/**
* Gets a user by userId

View File

@ -63,6 +63,7 @@ import com.cloud.api.commands.CreateDomainCmd;
import com.cloud.api.commands.CreatePortForwardingServiceCmd;
import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd;
import com.cloud.api.commands.CreateTemplateCmd;
import com.cloud.api.commands.CreateUserCmd;
import com.cloud.api.commands.CreateVolumeCmd;
import com.cloud.api.commands.DeleteIsoCmd;
import com.cloud.api.commands.DeleteTemplateCmd;
@ -572,8 +573,17 @@ public class ManagementServerImpl implements ManagementServer {
}
@Override
public User createUserAPI(String username, String password, String firstName, String lastName, Long domainId, String accountName, short userType, String email, String timezone) {
public UserAccount createUser(CreateUserCmd cmd) {
Long accountId = null;
String username = cmd.getUsername();
String password = cmd.getPassword();
String firstName = cmd.getFirstname();
String lastName = cmd.getLastname();
Long domainId = cmd.getDomainId();
String email = cmd.getEmail();
String timezone = cmd.getTimezone();
String accountName = cmd.getAccountName();
short userType = cmd.getAccountType().shortValue();
try {
if (accountName == null) {
accountName = username;
@ -715,11 +725,6 @@ public class ManagementServerImpl implements ManagementServer {
return _asyncMgr.submitAsyncJob(job);
}
@Override
public User createUser(String username, String password, String firstName, String lastName, Long domain, String accountName, short userType, String email, String timezone) {
return createUserAPI(username, StringToMD5(password), firstName, lastName, domain, accountName, userType, email, timezone);
}
@Override
public String updateAdminPassword(long userId, String oldPassword, String newPassword) {
// String old = StringToMD5(oldPassword);