diff --git a/api/src/com/cloud/api/commands/DisableAccountCmd.java b/api/src/com/cloud/api/commands/DisableAccountCmd.java index aab1e12e054..a9f663c4928 100644 --- a/api/src/com/cloud/api/commands/DisableAccountCmd.java +++ b/api/src/com/cloud/api/commands/DisableAccountCmd.java @@ -41,12 +41,15 @@ public class DisableAccountCmd extends BaseAsyncCmd { ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// - - @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, required=true, description="Disables specified account.") + @IdentityMapper(entityTableName="account") + @Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="Account id") + private Long id; + + @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="Disables specified account.") private String accountName; @IdentityMapper(entityTableName="domain") - @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, required=true, description="Disables specified account in this domain.") + @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="Disables specified account in this domain.") private Long domainId; @Parameter(name=ApiConstants.LOCK, type=CommandType.BOOLEAN, required=true, description="If true, only lock the account; else disable the account") @@ -56,6 +59,10 @@ public class DisableAccountCmd extends BaseAsyncCmd { /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// + public Long getId() { + return id; + } + public String getAccountName() { return accountName; } @@ -80,7 +87,12 @@ public class DisableAccountCmd extends BaseAsyncCmd { @Override public long getEntityOwnerId() { - Account account = _accountService.getActiveAccountByName(getAccountName(), getDomainId()); + Account account = _entityMgr.findById(Account.class, getId()); + if (account != null) { + return account.getAccountId(); + } + + account = _accountService.getActiveAccountByName(getAccountName(), getDomainId()); if (account != null) { return account.getAccountId(); } @@ -98,9 +110,9 @@ public class DisableAccountCmd extends BaseAsyncCmd { UserContext.current().setEventDetails("Account Name: "+getAccountName()+", Domain Id:"+getDomainId()); Account result = null; if(lockRequested) - result = _accountService.lockAccount(getAccountName(), getDomainId()); + result = _accountService.lockAccount(getAccountName(), getDomainId(), getId()); else - result = _accountService.disableAccount(getAccountName(), getDomainId()); + result = _accountService.disableAccount(getAccountName(), getDomainId(), getId()); if (result != null){ AccountResponse response = _responseGenerator.createAccountResponse(result); response.setResponseName(getCommandName()); diff --git a/api/src/com/cloud/api/commands/EnableAccountCmd.java b/api/src/com/cloud/api/commands/EnableAccountCmd.java index 54e89d02a34..9deb4713833 100644 --- a/api/src/com/cloud/api/commands/EnableAccountCmd.java +++ b/api/src/com/cloud/api/commands/EnableAccountCmd.java @@ -36,18 +36,25 @@ public class EnableAccountCmd extends BaseCmd { ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="account") + @Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="Account id") + private Long id; - @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, required=true, description="Enables specified account.") + @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="Enables specified account.") private String accountName; @IdentityMapper(entityTableName="domain") - @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, required=true, description="Enables specified account in this domain.") + @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="Enables specified account in this domain.") private Long domainId; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// + public Long getId() { + return id; + } + public String getAccountName() { return accountName; } @@ -67,7 +74,12 @@ public class EnableAccountCmd extends BaseCmd { @Override public long getEntityOwnerId() { - Account account = _accountService.getActiveAccountByName(getAccountName(), getDomainId()); + Account account = _entityMgr.findById(Account.class, getId()); + if (account != null) { + return account.getAccountId(); + } + + account = _accountService.getActiveAccountByName(getAccountName(), getDomainId()); if (account != null) { return account.getAccountId(); } @@ -77,7 +89,7 @@ public class EnableAccountCmd extends BaseCmd { @Override public void execute(){ - Account result = _accountService.enableAccount(getAccountName(), getDomainId()); + Account result = _accountService.enableAccount(getAccountName(), getDomainId(), getId()); if (result != null){ AccountResponse response = _responseGenerator.createAccountResponse(result); response.setResponseName(getCommandName()); diff --git a/api/src/com/cloud/api/commands/UpdateAccountCmd.java b/api/src/com/cloud/api/commands/UpdateAccountCmd.java index 1b623289a9d..596194b48e4 100755 --- a/api/src/com/cloud/api/commands/UpdateAccountCmd.java +++ b/api/src/com/cloud/api/commands/UpdateAccountCmd.java @@ -18,7 +18,6 @@ package com.cloud.api.commands; import java.util.Collection; -import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; @@ -29,7 +28,6 @@ import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; -import com.cloud.api.BaseCmd.CommandType; import com.cloud.api.response.AccountResponse; import com.cloud.user.Account; @@ -42,11 +40,15 @@ public class UpdateAccountCmd extends BaseCmd{ //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// - @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, required=true, description="the current account name") + @IdentityMapper(entityTableName="account") + @Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="Account id") + private Long id; + + @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="the current account name") private String accountName; @IdentityMapper(entityTableName="domain") - @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, required=true, description="the ID of the domain where the account exists") + @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="the ID of the domain where the account exists") private Long domainId; @Parameter(name=ApiConstants.NEW_NAME, type=CommandType.STRING, required=true, description="new name for the account") @@ -62,6 +64,10 @@ public class UpdateAccountCmd extends BaseCmd{ /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// + public Long getId() { + return id; + } + public String getAccountName() { return accountName; } @@ -99,7 +105,11 @@ public class UpdateAccountCmd extends BaseCmd{ @Override public long getEntityOwnerId() { - Account account = _accountService.getActiveAccountByName(getAccountName(), getDomainId()); + Account account = _entityMgr.findById(Account.class, getId()); + if (account != null) { + return account.getAccountId(); + } + account = _accountService.getActiveAccountByName(getAccountName(), getDomainId()); if (account != null) { return account.getAccountId(); } diff --git a/api/src/com/cloud/user/AccountService.java b/api/src/com/cloud/user/AccountService.java index 711974d6738..75b4fa49339 100755 --- a/api/src/com/cloud/user/AccountService.java +++ b/api/src/com/cloud/user/AccountService.java @@ -94,11 +94,12 @@ public interface AccountService { * Disables an account by accountName and domainId * @param accountName TODO * @param domainId TODO + * @param accountId * @param disabled * account if success * @return true if disable was successful, false otherwise */ - Account disableAccount(String accountName, Long domainId) throws ConcurrentOperationException, ResourceUnavailableException; + Account disableAccount(String accountName, Long domainId, Long accountId) throws ConcurrentOperationException, ResourceUnavailableException; /** * Enables an account by accountId @@ -106,9 +107,10 @@ public interface AccountService { * @param accountName * - the enableAccount command defining the accountId to be deleted. * @param domainId TODO + * @param accountId * @return account object */ - Account enableAccount(String accountName, long domainId); + Account enableAccount(String accountName, Long domainId, Long accountId); /** * Locks an account by accountId. A locked account cannot access the API, but will still have running VMs/IP addresses @@ -117,9 +119,10 @@ public interface AccountService { * @param accountName * - the LockAccount command defining the accountId to be locked. * @param domainId TODO + * @param accountId * @return account object */ - Account lockAccount(String accountName, Long domainId); + Account lockAccount(String accountName, Long domainId, Long accountId); /** * Updates an account name diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index 490c73a728f..957ab96c66b 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -966,12 +966,18 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag } @Override - public AccountVO enableAccount(String accountName, long domainId) { + public AccountVO enableAccount(String accountName, Long domainId, Long accountId) { // Check if account exists - Account account = _accountDao.findActiveAccount(accountName, domainId); + Account account = null; + if(accountId != null){ + account = _accountDao.findById(accountId); + }else{ + account = _accountDao.findActiveAccount(accountName, domainId); + } + if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) { - throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + throw new InvalidParameterValueException("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } // Don't allow to modify system account @@ -987,18 +993,24 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag if (success) { return _accountDao.findById(account.getId()); } else { - throw new CloudRuntimeException("Unable to enable account " + accountName + " in domain " + domainId); + throw new CloudRuntimeException("Unable to enable account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } } @Override @ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_DISABLE, eventDescription = "locking account", async = true) - public AccountVO lockAccount(String accountName, Long domainId) { + public AccountVO lockAccount(String accountName, Long domainId, Long accountId) { Account caller = UserContext.current().getCaller(); - Account account = _accountDao.findActiveAccount(accountName, domainId); + Account account = null; + if(accountId != null){ + account = _accountDao.findById(accountId); + }else{ + account = _accountDao.findActiveAccount(accountName, domainId); + } + if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) { - throw new InvalidParameterValueException("Unable to find active account with name " + accountName + " in domain " + domainId); + throw new InvalidParameterValueException("Unable to find active account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } checkAccess(caller, null, account); @@ -1011,18 +1023,24 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag if (lockAccount(account.getId())) { return _accountDao.findById(account.getId()); } else { - throw new CloudRuntimeException("Unable to lock account " + accountName + " in domain " + domainId); + throw new CloudRuntimeException("Unable to lock account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } } @Override @ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_DISABLE, eventDescription = "disabling account", async = true) - public AccountVO disableAccount(String accountName, Long domainId) throws ConcurrentOperationException, ResourceUnavailableException { + public AccountVO disableAccount(String accountName, Long domainId, Long accountId) throws ConcurrentOperationException, ResourceUnavailableException { Account caller = UserContext.current().getCaller(); - Account account = _accountDao.findActiveAccount(accountName, domainId); + Account account = null; + if(accountId != null){ + account = _accountDao.findById(accountId); + }else{ + account = _accountDao.findActiveAccount(accountName, domainId); + } + if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) { - throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + throw new InvalidParameterValueException("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } checkAccess(caller, null, account); @@ -1030,12 +1048,13 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag if (disableAccount(account.getId())) { return _accountDao.findById(account.getId()); } else { - throw new CloudRuntimeException("Unable to update account " + accountName + " in domain " + domainId); + throw new CloudRuntimeException("Unable to update account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } } @Override public AccountVO updateAccount(UpdateAccountCmd cmd) { + Long accountId = cmd.getId(); Long domainId = cmd.getDomainId(); String accountName = cmd.getAccountName(); String newAccountName = cmd.getNewName(); @@ -1043,12 +1062,17 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag Mapdetails = cmd.getDetails(); boolean success = false; - Account account = _accountDao.findAccount(accountName, domainId); + Account account = null; + if(accountId != null){ + account = _accountDao.findById(accountId); + }else{ + account = _accountDao.findAccount(accountName, domainId); + } // Check if account exists if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) { - s_logger.error("Unable to find account " + accountName + " in domain " + domainId); - throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + s_logger.error("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); + throw new InvalidParameterValueException("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } // Don't allow to modify system account @@ -1095,7 +1119,7 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag if (success) { return _accountDao.findById(account.getId()); } else { - throw new CloudRuntimeException("Unable to update account " + accountName + " in domain " + domainId); + throw new CloudRuntimeException("Unable to update account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId); } }