From f625900b8bdbf7e86ba82d4acc6307b418994804 Mon Sep 17 00:00:00 2001 From: abhishek Date: Fri, 20 Aug 2010 09:59:06 -0700 Subject: [PATCH] Refactoring the register command --- .../com/cloud/api/commands/RegisterCmd.java | 63 +++++++++---------- .../com/cloud/server/ManagementServer.java | 5 +- .../cloud/server/ManagementServerImpl.java | 23 +++++-- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/server/src/com/cloud/api/commands/RegisterCmd.java b/server/src/com/cloud/api/commands/RegisterCmd.java index f290ba2eabc..006fdd5805e 100644 --- a/server/src/com/cloud/api/commands/RegisterCmd.java +++ b/server/src/com/cloud/api/commands/RegisterCmd.java @@ -18,27 +18,19 @@ 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.user.User; -import com.cloud.utils.Pair; - +import com.cloud.api.BaseCmd.Manager; + +@Implementation(method="createApiKeyAndSecretKey", manager=Manager.ManagementServer) public class RegisterCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(RegisterCmd.class.getName()); private static final String s_name = "registeruserkeysresponse"; - private static final List> s_properties = new ArrayList>(); - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -62,28 +54,31 @@ public class RegisterCmd extends BaseCmd { public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Long userId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// @Override +// public List> execute(Map params) { +// Long userId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// +// User user = getManagementServer().findUserById(userId); +// +// if (user == null) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "unable to find user for id : " + userId); +// } +// +// // generate both an api key and a secret key, update the user table with the keys, return the keys to the user +// String apiKey = getManagementServer().createApiKey(user.getId()); +// String secretKey = getManagementServer().createSecretKey(user.getId()); +// +// List> returnValues = new ArrayList>(); +// +// returnValues.add(new Pair(BaseCmd.Properties.API_KEY.getName(), apiKey)); +// returnValues.add(new Pair(BaseCmd.Properties.SECRET_KEY.getName(), secretKey)); +// return returnValues; +// } - User user = getManagementServer().findUserById(userId); - - if (user == null) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "unable to find user for id : " + userId); - } - - // generate both an api key and a secret key, update the user table with the keys, return the keys to the user - String apiKey = getManagementServer().createApiKey(user.getId()); - String secretKey = getManagementServer().createSecretKey(user.getId()); - - List> returnValues = new ArrayList>(); - - returnValues.add(new Pair(BaseCmd.Properties.API_KEY.getName(), apiKey)); - returnValues.add(new Pair(BaseCmd.Properties.SECRET_KEY.getName(), secretKey)); - return returnValues; - } + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 42dd438d05b..cc723d1019c 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -325,7 +325,7 @@ public interface ManagementServer { * @param userId * @return the new API key */ - String createApiKey(Long userId); +// String createApiKey(Long userId); /** * Create a secret key for a user, this key is used to sign requests made by the user @@ -334,7 +334,7 @@ public interface ManagementServer { * @param userId * @return the new secret key */ - String createSecretKey(Long userId); +// String createSecretKey(Long userId); /** * Gets Storage statistics for a given host @@ -2009,4 +2009,5 @@ public interface ManagementServer { boolean validateCustomVolumeSizeRange(long size) throws InvalidParameterValueException; boolean updateUser(UpdateUserCmd cmd) throws InvalidParameterValueException; boolean updateTemplatePermissions(UpdateTemplateOrIsoPermissionsCmd cmd)throws InvalidParameterValueException, PermissionDeniedException,InternalErrorException; + String[] createApiKeyAndSecretKey(Long userId); } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index d07874aedb7..079281420d6 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -1396,9 +1396,25 @@ public class ManagementServerImpl implements ManagementServer { return account; } - + @Override - public String createApiKey(Long userId) { + public String[] createApiKeyAndSecretKey(Long userId) + { + User user = _userDao.findById(userId); + + if (user == null) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "unable to find user for id : " + userId); + } + + // generate both an api key and a secret key, update the user table with the keys, return the keys to the user + String[] keys = new String[2]; + keys[0] = createApiKey(userId); + keys[1] = createSecretKey(userId); + + return keys; + } + + private String createApiKey(Long userId) { User user = findUserById(userId); try { UserVO updatedUser = _userDao.createForUpdate(); @@ -1427,8 +1443,7 @@ public class ManagementServerImpl implements ManagementServer { return null; } - @Override - public String createSecretKey(Long userId) { + private String createSecretKey(Long userId) { User user = findUserById(userId); try { UserVO updatedUser = _userDao.createForUpdate();