Skip to content

Commit 5f07dda

Browse files
JoaoJandreJoão ParaquettiJoao
authored
Refactor account type (#6048)
* Refactor account type * Added license. * Address reviews * Address review. Co-authored-by: João Paraquetti <joao@scclouds.com.br> Co-authored-by: Joao <JoaoJandre@gitlab.com>
1 parent d258da5 commit 5f07dda

File tree

139 files changed

+671
-531
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+671
-531
lines changed

api/src/main/java/com/cloud/user/Account.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,65 @@
1616
// under the License.
1717
package com.cloud.user;
1818

19-
import java.util.Date;
20-
2119
import org.apache.cloudstack.acl.ControlledEntity;
2220
import org.apache.cloudstack.api.Identity;
2321
import org.apache.cloudstack.api.InternalIdentity;
2422

23+
import java.util.Date;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
2527
public interface Account extends ControlledEntity, InternalIdentity, Identity {
2628

29+
/**
30+
* Account states.
31+
* */
32+
enum State {
33+
DISABLED, ENABLED, LOCKED;
34+
35+
/**
36+
* The toString method was overridden to maintain consistency in the DB, as the GenericDaoBase uses toString in the enum value to make the sql statements
37+
* and previously the enum was in lowercase.
38+
* */
39+
@Override
40+
public String toString(){
41+
return super.toString().toLowerCase();
42+
}
43+
44+
/**
45+
* This method was created to maintain backwards compatibility to the DB schema. Unfortunately we can't override the valueOf method.
46+
* */
47+
public static State getValueOf(String name){
48+
return State.valueOf(name.toUpperCase());
49+
}
2750

28-
public enum State {
29-
disabled, enabled, locked
3051
}
3152

32-
public static final short ACCOUNT_TYPE_NORMAL = 0;
33-
public static final short ACCOUNT_TYPE_ADMIN = 1;
34-
public static final short ACCOUNT_TYPE_DOMAIN_ADMIN = 2;
35-
public static final short ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN = 3;
36-
public static final short ACCOUNT_TYPE_READ_ONLY_ADMIN = 4;
37-
public static final short ACCOUNT_TYPE_PROJECT = 5;
53+
/**
54+
* Account types.
55+
* */
56+
enum Type {
57+
NORMAL, ADMIN, DOMAIN_ADMIN, RESOURCE_DOMAIN_ADMIN, READ_ONLY_ADMIN, PROJECT, UNKNOWN;
3858

39-
public static final String ACCOUNT_STATE_DISABLED = "disabled";
40-
public static final String ACCOUNT_STATE_ENABLED = "enabled";
41-
public static final String ACCOUNT_STATE_LOCKED = "locked";
59+
private static Map<Integer,Type> ACCOUNT_TYPE_MAP = new HashMap<>();
60+
61+
static {
62+
for (Type t: Type.values()) {
63+
ACCOUNT_TYPE_MAP.put(t.ordinal(),t);
64+
}
65+
ACCOUNT_TYPE_MAP.remove(6);
66+
}
67+
68+
public static Type getFromValue(Integer type){
69+
return ACCOUNT_TYPE_MAP.get(type);
70+
}
71+
}
4272

4373
public static final long ACCOUNT_ID_SYSTEM = 1;
4474

4575
public String getAccountName();
4676

47-
public short getType();
77+
public Type getType();
4878

4979
public Long getRoleId();
5080

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public interface AccountService {
4242
*/
4343
UserAccount createUserAccount(CreateAccountCmd accountCmd);
4444

45-
UserAccount createUserAccount(String userName, String password, String firstName, String lastName, String email, String timezone, String accountName, short accountType, Long roleId, Long domainId,
46-
String networkDomain, Map<String, String> details, String accountUUID, String userUUID, User.Source source);
45+
UserAccount createUserAccount(String userName, String password, String firstName, String lastName, String email, String timezone, String accountName, Account.Type accountType,
46+
Long roleId, Long domainId, String networkDomain, Map<String, String> details, String accountUUID, String userUUID, User.Source source);
4747

4848
/**
4949
* Locks a user by userId. A locked user cannot access the API, but will still have running VMs/IP addresses
@@ -57,7 +57,8 @@ UserAccount createUserAccount(String userName, String password, String firstName
5757

5858
User createUser(String userName, String password, String firstName, String lastName, String email, String timeZone, String accountName, Long domainId, String userUUID);
5959

60-
User createUser(String userName, String password, String firstName, String lastName, String email, String timeZone, String accountName, Long domainId, String userUUID, User.Source source);
60+
User createUser(String userName, String password, String firstName, String lastName, String email, String timeZone, String accountName, Long domainId, String userUUID,
61+
User.Source source);
6162

6263
boolean isAdmin(Long accountId);
6364

api/src/main/java/com/cloud/vm/InstanceGroup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Date;
2020

21+
import com.cloud.user.Account;
2122
import org.apache.cloudstack.acl.ControlledEntity;
2223
import org.apache.cloudstack.api.Identity;
2324
import org.apache.cloudstack.api.InternalIdentity;
@@ -28,6 +29,6 @@ public interface InstanceGroup extends ControlledEntity, Identity, InternalIdent
2829

2930
Date getCreated();
3031

31-
Short getAccountType();
32+
Account.Type getAccountType();
3233

3334
}

api/src/main/java/org/apache/cloudstack/acl/RoleType.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,33 @@
2020

2121
import com.cloud.user.Account;
2222
import com.google.common.base.Enums;
23+
import org.apache.log4j.Logger;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
2327

2428
// Enum for default roles in CloudStack
2529
public enum RoleType {
26-
Admin(1L, Account.ACCOUNT_TYPE_ADMIN, 1),
27-
ResourceAdmin(2L, Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN, 2),
28-
DomainAdmin(3L, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, 4),
29-
User(4L, Account.ACCOUNT_TYPE_NORMAL, 8),
30-
Unknown(-1L, (short) -1, 0);
30+
Admin(1L, Account.Type.ADMIN, 1),
31+
ResourceAdmin(2L, Account.Type.RESOURCE_DOMAIN_ADMIN, 2),
32+
DomainAdmin(3L, Account.Type.DOMAIN_ADMIN, 4),
33+
User(4L, Account.Type.NORMAL, 8),
34+
Unknown(-1L, Account.Type.UNKNOWN, 0);
3135

3236
private long id;
33-
private short accountType;
37+
private Account.Type accountType;
3438
private int mask;
3539

36-
RoleType(final long id, final short accountType, final int mask) {
40+
private static Logger logger = Logger.getLogger(RoleType.class.getName());
41+
private static Map<Account.Type, RoleType> ACCOUNT_TYPE_MAP = new HashMap<>();
42+
43+
static {
44+
for (RoleType t: RoleType.values()) {
45+
ACCOUNT_TYPE_MAP.put(t.getAccountType(),t);
46+
}
47+
}
48+
49+
RoleType(final long id, final Account.Type accountType, final int mask) {
3750
this.id = id;
3851
this.accountType = accountType;
3952
this.mask = mask;
@@ -43,7 +56,7 @@ public long getId() {
4356
return id;
4457
}
4558

46-
public short getAccountType() {
59+
public Account.Type getAccountType() {
4760
return accountType;
4861
}
4962

@@ -68,26 +81,15 @@ public static RoleType fromMask(int mask) {
6881
return Unknown;
6982
}
7083

71-
public static RoleType getByAccountType(final short accountType) {
72-
RoleType roleType = RoleType.Unknown;
73-
switch (accountType) {
74-
case Account.ACCOUNT_TYPE_ADMIN:
75-
roleType = RoleType.Admin;
76-
break;
77-
case Account.ACCOUNT_TYPE_DOMAIN_ADMIN:
78-
roleType = RoleType.DomainAdmin;
79-
break;
80-
case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN:
81-
roleType = RoleType.ResourceAdmin;
82-
break;
83-
case Account.ACCOUNT_TYPE_NORMAL:
84-
roleType = RoleType.User;
85-
break;
84+
public static RoleType getByAccountType(final Account.Type accountType) {
85+
RoleType t = ACCOUNT_TYPE_MAP.get(accountType);
86+
if (t == null) {
87+
return RoleType.Unknown;
8688
}
87-
return roleType;
89+
return t;
8890
}
8991

90-
public static Long getRoleByAccountType(final Long roleId, final Short accountType) {
92+
public static Long getRoleByAccountType(final Long roleId, final Account.Type accountType) {
9193
if (roleId == null && accountType != null) {
9294
RoleType defaultRoleType = RoleType.getByAccountType(accountType);
9395
if (defaultRoleType != null && defaultRoleType != RoleType.Unknown) {
@@ -97,10 +99,15 @@ public static Long getRoleByAccountType(final Long roleId, final Short accountTy
9799
return roleId;
98100
}
99101

100-
public static Short getAccountTypeByRole(final Role role, final Short accountType) {
101-
if (role != null && role.getId() > 0L) {
102+
/**
103+
* This method returns the role account type if the role isn't null, else it returns the default account type.
104+
* */
105+
public static Account.Type getAccountTypeByRole(final Role role, final Account.Type defautAccountType) {
106+
if (role != null) {
107+
logger.debug(String.format("Role [%s] is not null; therefore, we use its account type [%s].", role, defautAccountType));
102108
return role.getRoleType().getAccountType();
103109
}
104-
return accountType;
110+
logger.debug(String.format("Role is null; therefore, we use the default account type [%s] value.", defautAccountType));
111+
return defautAccountType;
105112
}
106113
}

api/src/main/java/org/apache/cloudstack/api/command/admin/account/CreateAccountCmd.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public class CreateAccountCmd extends BaseCmd {
5656
private String accountName;
5757

5858
@Parameter(name = ApiConstants.ACCOUNT_TYPE,
59-
type = CommandType.SHORT,
59+
type = CommandType.INTEGER,
6060
description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin")
61-
private Short accountType;
61+
private Integer accountType;
6262

6363
@Parameter(name = ApiConstants.ROLE_ID, type = CommandType.UUID, entityType = RoleResponse.class, description = "Creates the account under the specified role.")
6464
private Long roleId;
@@ -109,12 +109,12 @@ public String getAccountName() {
109109
return accountName;
110110
}
111111

112-
public Short getAccountType() {
113-
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), accountType);
112+
public Account.Type getAccountType() {
113+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType));
114114
}
115115

116116
public Long getRoleId() {
117-
return RoleType.getRoleByAccountType(roleId, accountType);
117+
return RoleType.getRoleByAccountType(roleId, getAccountType());
118118
}
119119

120120
public Long getDomainId() {

api/src/main/java/org/apache/cloudstack/api/command/admin/ca/ListCaCertificateCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ public String getCommandName() {
8585

8686
@Override
8787
public long getEntityOwnerId() {
88-
return Account.ACCOUNT_TYPE_NORMAL;
88+
return Account.Type.NORMAL.ordinal();
8989
}
9090
}

api/src/main/java/org/apache/cloudstack/api/command/admin/user/ListUsersCmd.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.cloud.server.ResourceIcon;
2020
import com.cloud.server.ResourceTag;
21+
import com.cloud.user.Account;
2122
import org.apache.cloudstack.api.response.ResourceIconResponse;
2223
import org.apache.log4j.Logger;
2324

@@ -42,9 +43,9 @@ public class ListUsersCmd extends BaseListAccountResourcesCmd {
4243
/////////////////////////////////////////////////////
4344

4445
@Parameter(name = ApiConstants.ACCOUNT_TYPE,
45-
type = CommandType.LONG,
46+
type = CommandType.INTEGER,
4647
description = "List users by account type. Valid types include admin, domain-admin, read-only-admin, or user.")
47-
private Long accountType;
48+
private Integer accountType;
4849

4950
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = UserResponse.class, description = "List user by ID.")
5051
private Long id;
@@ -63,8 +64,8 @@ public class ListUsersCmd extends BaseListAccountResourcesCmd {
6364
/////////////////// Accessors ///////////////////////
6465
/////////////////////////////////////////////////////
6566

66-
public Long getAccountType() {
67-
return accountType;
67+
public Account.Type getAccountType() {
68+
return Account.Type.getFromValue(accountType);
6869
}
6970

7071
public Long getId() {

api/src/main/java/org/apache/cloudstack/api/command/user/account/ListAccountsCmd.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public class ListAccountsCmd extends BaseListDomainResourcesCmd implements UserC
4949
/////////////////////////////////////////////////////
5050

5151
@Parameter(name = ApiConstants.ACCOUNT_TYPE,
52-
type = CommandType.LONG,
52+
type = CommandType.INTEGER,
5353
description = "list accounts by account type. Valid account types are 1 (admin), 2 (domain-admin), and 0 (user).")
54-
private Long accountType;
54+
private Integer accountType;
5555

5656
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = AccountResponse.class, description = "list account by account ID")
5757
private Long id;
@@ -79,8 +79,8 @@ public class ListAccountsCmd extends BaseListDomainResourcesCmd implements UserC
7979
/////////////////// Accessors ///////////////////////
8080
/////////////////////////////////////////////////////
8181

82-
public Long getAccountType() {
83-
return accountType;
82+
public Account.Type getAccountType() {
83+
return Account.Type.getFromValue(accountType);
8484
}
8585

8686
public Long getId() {

api/src/main/java/org/apache/cloudstack/api/command/user/snapshot/CreateSnapshotCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ public long getEntityOwnerId() {
172172

173173
Account account = _accountService.getAccount(volume.getAccountId());
174174
//Can create templates for enabled projects/accounts only
175-
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
175+
if (account.getType() == Account.Type.PROJECT) {
176176
Project project = _projectService.findByProjectAccountId(volume.getAccountId());
177177
if (project.getState() != Project.State.Active) {
178178
throw new PermissionDeniedException("Can't add resources to the project id=" + project.getId() + " in state=" + project.getState() +
179179
" as it's no longer active");
180180
}
181-
} else if (account.getState() == Account.State.disabled) {
181+
} else if (account.getState() == Account.State.DISABLED) {
182182
throw new PermissionDeniedException("The owner of template is disabled: " + account);
183183
}
184184

api/src/main/java/org/apache/cloudstack/api/command/user/snapshot/CreateSnapshotFromVMSnapshotCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ public long getEntityOwnerId() {
129129

130130
Account account = _accountService.getAccount(vmsnapshot.getAccountId());
131131
//Can create templates for enabled projects/accounts only
132-
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
132+
if (account.getType() == Account.Type.PROJECT) {
133133
Project project = _projectService.findByProjectAccountId(vmsnapshot.getAccountId());
134134
if (project == null) {
135135
throw new InvalidParameterValueException("Unable to find project by account id=" + account.getUuid());
136136
}
137137
if (project.getState() != Project.State.Active) {
138138
throw new PermissionDeniedException("Can't add resources to the project id=" + project.getUuid() + " in state=" + project.getState() + " as it's no longer active");
139139
}
140-
} else if (account.getState() == Account.State.disabled) {
140+
} else if (account.getState() == Account.State.DISABLED) {
141141
throw new PermissionDeniedException("The owner of template is disabled: " + account);
142142
}
143143

0 commit comments

Comments
 (0)