Skip to content

Commit d98130c

Browse files
author
prachi
committed
Bug 8750 - update/disable/enableAccount api commands should accept "id" request parameter to be consistent with deleteAcccount command
These commands now accept 'id' parameter along with the 'accountname' and 'domainid'.
1 parent 2f884dd commit d98130c

5 files changed

Lines changed: 95 additions & 34 deletions

File tree

‎api/src/com/cloud/api/commands/DisableAccountCmd.java‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ public class DisableAccountCmd extends BaseAsyncCmd {
4141
/////////////////////////////////////////////////////
4242
//////////////// API parameters /////////////////////
4343
/////////////////////////////////////////////////////
44-
45-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, required=true, description="Disables specified account.")
44+
@IdentityMapper(entityTableName="account")
45+
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="Account id")
46+
private Long id;
47+
48+
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="Disables specified account.")
4649
private String accountName;
4750

4851
@IdentityMapper(entityTableName="domain")
49-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, required=true, description="Disables specified account in this domain.")
52+
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="Disables specified account in this domain.")
5053
private Long domainId;
5154

5255
@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 {
5659
/////////////////// Accessors ///////////////////////
5760
/////////////////////////////////////////////////////
5861

62+
public Long getId() {
63+
return id;
64+
}
65+
5966
public String getAccountName() {
6067
return accountName;
6168
}
@@ -80,7 +87,12 @@ public String getEventType() {
8087

8188
@Override
8289
public long getEntityOwnerId() {
83-
Account account = _accountService.getActiveAccountByName(getAccountName(), getDomainId());
90+
Account account = _entityMgr.findById(Account.class, getId());
91+
if (account != null) {
92+
return account.getAccountId();
93+
}
94+
95+
account = _accountService.getActiveAccountByName(getAccountName(), getDomainId());
8496
if (account != null) {
8597
return account.getAccountId();
8698
}
@@ -98,9 +110,9 @@ public void execute() throws ConcurrentOperationException, ResourceUnavailableEx
98110
UserContext.current().setEventDetails("Account Name: "+getAccountName()+", Domain Id:"+getDomainId());
99111
Account result = null;
100112
if(lockRequested)
101-
result = _accountService.lockAccount(getAccountName(), getDomainId());
113+
result = _accountService.lockAccount(getAccountName(), getDomainId(), getId());
102114
else
103-
result = _accountService.disableAccount(getAccountName(), getDomainId());
115+
result = _accountService.disableAccount(getAccountName(), getDomainId(), getId());
104116
if (result != null){
105117
AccountResponse response = _responseGenerator.createAccountResponse(result);
106118
response.setResponseName(getCommandName());

‎api/src/com/cloud/api/commands/EnableAccountCmd.java‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,25 @@ public class EnableAccountCmd extends BaseCmd {
3636
/////////////////////////////////////////////////////
3737
//////////////// API parameters /////////////////////
3838
/////////////////////////////////////////////////////
39+
@IdentityMapper(entityTableName="account")
40+
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="Account id")
41+
private Long id;
3942

40-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, required=true, description="Enables specified account.")
43+
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="Enables specified account.")
4144
private String accountName;
4245

4346
@IdentityMapper(entityTableName="domain")
44-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, required=true, description="Enables specified account in this domain.")
47+
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="Enables specified account in this domain.")
4548
private Long domainId;
4649

4750
/////////////////////////////////////////////////////
4851
/////////////////// Accessors ///////////////////////
4952
/////////////////////////////////////////////////////
5053

54+
public Long getId() {
55+
return id;
56+
}
57+
5158
public String getAccountName() {
5259
return accountName;
5360
}
@@ -67,7 +74,12 @@ public String getCommandName() {
6774

6875
@Override
6976
public long getEntityOwnerId() {
70-
Account account = _accountService.getActiveAccountByName(getAccountName(), getDomainId());
77+
Account account = _entityMgr.findById(Account.class, getId());
78+
if (account != null) {
79+
return account.getAccountId();
80+
}
81+
82+
account = _accountService.getActiveAccountByName(getAccountName(), getDomainId());
7183
if (account != null) {
7284
return account.getAccountId();
7385
}
@@ -77,7 +89,7 @@ public long getEntityOwnerId() {
7789

7890
@Override
7991
public void execute(){
80-
Account result = _accountService.enableAccount(getAccountName(), getDomainId());
92+
Account result = _accountService.enableAccount(getAccountName(), getDomainId(), getId());
8193
if (result != null){
8294
AccountResponse response = _responseGenerator.createAccountResponse(result);
8395
response.setResponseName(getCommandName());

‎api/src/com/cloud/api/commands/UpdateAccountCmd.java‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.cloud.api.commands;
1919

2020
import java.util.Collection;
21-
import java.util.HashMap;
2221
import java.util.Map;
2322

2423
import org.apache.log4j.Logger;
@@ -29,7 +28,6 @@
2928
import com.cloud.api.Implementation;
3029
import com.cloud.api.Parameter;
3130
import com.cloud.api.ServerApiException;
32-
import com.cloud.api.BaseCmd.CommandType;
3331
import com.cloud.api.response.AccountResponse;
3432
import com.cloud.user.Account;
3533

@@ -42,11 +40,15 @@ public class UpdateAccountCmd extends BaseCmd{
4240
//////////////// API parameters /////////////////////
4341
/////////////////////////////////////////////////////
4442

45-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, required=true, description="the current account name")
43+
@IdentityMapper(entityTableName="account")
44+
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="Account id")
45+
private Long id;
46+
47+
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="the current account name")
4648
private String accountName;
4749

4850
@IdentityMapper(entityTableName="domain")
49-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, required=true, description="the ID of the domain where the account exists")
51+
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="the ID of the domain where the account exists")
5052
private Long domainId;
5153

5254
@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{
6264
/////////////////// Accessors ///////////////////////
6365
/////////////////////////////////////////////////////
6466

67+
public Long getId() {
68+
return id;
69+
}
70+
6571
public String getAccountName() {
6672
return accountName;
6773
}
@@ -99,7 +105,11 @@ public String getCommandName() {
99105

100106
@Override
101107
public long getEntityOwnerId() {
102-
Account account = _accountService.getActiveAccountByName(getAccountName(), getDomainId());
108+
Account account = _entityMgr.findById(Account.class, getId());
109+
if (account != null) {
110+
return account.getAccountId();
111+
}
112+
account = _accountService.getActiveAccountByName(getAccountName(), getDomainId());
103113
if (account != null) {
104114
return account.getAccountId();
105115
}

‎api/src/com/cloud/user/AccountService.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,23 @@ public interface AccountService {
9494
* Disables an account by accountName and domainId
9595
* @param accountName TODO
9696
* @param domainId TODO
97+
* @param accountId
9798
* @param disabled
9899
* account if success
99100
* @return true if disable was successful, false otherwise
100101
*/
101-
Account disableAccount(String accountName, Long domainId) throws ConcurrentOperationException, ResourceUnavailableException;
102+
Account disableAccount(String accountName, Long domainId, Long accountId) throws ConcurrentOperationException, ResourceUnavailableException;
102103

103104
/**
104105
* Enables an account by accountId
105106
*
106107
* @param accountName
107108
* - the enableAccount command defining the accountId to be deleted.
108109
* @param domainId TODO
110+
* @param accountId
109111
* @return account object
110112
*/
111-
Account enableAccount(String accountName, long domainId);
113+
Account enableAccount(String accountName, Long domainId, Long accountId);
112114

113115
/**
114116
* 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 {
117119
* @param accountName
118120
* - the LockAccount command defining the accountId to be locked.
119121
* @param domainId TODO
122+
* @param accountId
120123
* @return account object
121124
*/
122-
Account lockAccount(String accountName, Long domainId);
125+
Account lockAccount(String accountName, Long domainId, Long accountId);
123126

124127
/**
125128
* Updates an account name

‎server/src/com/cloud/user/AccountManagerImpl.java‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -966,12 +966,18 @@ public boolean deleteUserAccount(long accountId) {
966966
}
967967

968968
@Override
969-
public AccountVO enableAccount(String accountName, long domainId) {
969+
public AccountVO enableAccount(String accountName, Long domainId, Long accountId) {
970970

971971
// Check if account exists
972-
Account account = _accountDao.findActiveAccount(accountName, domainId);
972+
Account account = null;
973+
if(accountId != null){
974+
account = _accountDao.findById(accountId);
975+
}else{
976+
account = _accountDao.findActiveAccount(accountName, domainId);
977+
}
978+
973979
if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
974-
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
980+
throw new InvalidParameterValueException("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
975981
}
976982

977983
// Don't allow to modify system account
@@ -987,18 +993,24 @@ public AccountVO enableAccount(String accountName, long domainId) {
987993
if (success) {
988994
return _accountDao.findById(account.getId());
989995
} else {
990-
throw new CloudRuntimeException("Unable to enable account " + accountName + " in domain " + domainId);
996+
throw new CloudRuntimeException("Unable to enable account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
991997
}
992998
}
993999

9941000
@Override
9951001
@ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_DISABLE, eventDescription = "locking account", async = true)
996-
public AccountVO lockAccount(String accountName, Long domainId) {
1002+
public AccountVO lockAccount(String accountName, Long domainId, Long accountId) {
9971003
Account caller = UserContext.current().getCaller();
9981004

999-
Account account = _accountDao.findActiveAccount(accountName, domainId);
1005+
Account account = null;
1006+
if(accountId != null){
1007+
account = _accountDao.findById(accountId);
1008+
}else{
1009+
account = _accountDao.findActiveAccount(accountName, domainId);
1010+
}
1011+
10001012
if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
1001-
throw new InvalidParameterValueException("Unable to find active account with name " + accountName + " in domain " + domainId);
1013+
throw new InvalidParameterValueException("Unable to find active account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
10021014
}
10031015

10041016
checkAccess(caller, null, account);
@@ -1011,44 +1023,56 @@ public AccountVO lockAccount(String accountName, Long domainId) {
10111023
if (lockAccount(account.getId())) {
10121024
return _accountDao.findById(account.getId());
10131025
} else {
1014-
throw new CloudRuntimeException("Unable to lock account " + accountName + " in domain " + domainId);
1026+
throw new CloudRuntimeException("Unable to lock account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
10151027
}
10161028
}
10171029

10181030
@Override
10191031
@ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_DISABLE, eventDescription = "disabling account", async = true)
1020-
public AccountVO disableAccount(String accountName, Long domainId) throws ConcurrentOperationException, ResourceUnavailableException {
1032+
public AccountVO disableAccount(String accountName, Long domainId, Long accountId) throws ConcurrentOperationException, ResourceUnavailableException {
10211033
Account caller = UserContext.current().getCaller();
10221034

1023-
Account account = _accountDao.findActiveAccount(accountName, domainId);
1035+
Account account = null;
1036+
if(accountId != null){
1037+
account = _accountDao.findById(accountId);
1038+
}else{
1039+
account = _accountDao.findActiveAccount(accountName, domainId);
1040+
}
1041+
10241042
if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
1025-
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
1043+
throw new InvalidParameterValueException("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
10261044
}
10271045

10281046
checkAccess(caller, null, account);
10291047

10301048
if (disableAccount(account.getId())) {
10311049
return _accountDao.findById(account.getId());
10321050
} else {
1033-
throw new CloudRuntimeException("Unable to update account " + accountName + " in domain " + domainId);
1051+
throw new CloudRuntimeException("Unable to update account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
10341052
}
10351053
}
10361054

10371055
@Override
10381056
public AccountVO updateAccount(UpdateAccountCmd cmd) {
1057+
Long accountId = cmd.getId();
10391058
Long domainId = cmd.getDomainId();
10401059
String accountName = cmd.getAccountName();
10411060
String newAccountName = cmd.getNewName();
10421061
String networkDomain = cmd.getNetworkDomain();
10431062
Map<String, String>details = cmd.getDetails();
10441063

10451064
boolean success = false;
1046-
Account account = _accountDao.findAccount(accountName, domainId);
1065+
Account account = null;
1066+
if(accountId != null){
1067+
account = _accountDao.findById(accountId);
1068+
}else{
1069+
account = _accountDao.findAccount(accountName, domainId);
1070+
}
10471071

10481072
// Check if account exists
10491073
if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
1050-
s_logger.error("Unable to find account " + accountName + " in domain " + domainId);
1051-
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
1074+
s_logger.error("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
1075+
throw new InvalidParameterValueException("Unable to find account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
10521076
}
10531077

10541078
// Don't allow to modify system account
@@ -1095,7 +1119,7 @@ public AccountVO updateAccount(UpdateAccountCmd cmd) {
10951119
if (success) {
10961120
return _accountDao.findById(account.getId());
10971121
} else {
1098-
throw new CloudRuntimeException("Unable to update account " + accountName + " in domain " + domainId);
1122+
throw new CloudRuntimeException("Unable to update account by accountId: "+accountId+" OR by name: "+ accountName + " in domain " + domainId);
10991123
}
11001124
}
11011125

0 commit comments

Comments
 (0)