Skip to content

Commit 6d7c042

Browse files
Accept a role ID on linking an account to LDAP (#8236)
* accept role on link account to ldap * reformat tests * validation * Update plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LinkAccountToLdapCmd.java Co-authored-by: Suresh Kumar Anaparti <sureshkumar.anaparti@gmail.com>
1 parent 7f0d9a0 commit 6d7c042

4 files changed

Lines changed: 26 additions & 21 deletions

File tree

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LinkAccountToLdapCmd.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.cloudstack.api.response.DomainResponse;
3434
import org.apache.cloudstack.api.response.LinkAccountToLdapResponse;
3535
import org.apache.cloudstack.api.response.LinkDomainToLdapResponse;
36+
import org.apache.cloudstack.api.response.RoleResponse;
3637
import org.apache.cloudstack.ldap.LdapManager;
3738
import org.apache.cloudstack.ldap.LdapUser;
3839
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
@@ -63,9 +64,12 @@ public class LinkAccountToLdapCmd extends BaseCmd {
6364
@Parameter(name = ApiConstants.ADMIN, type = CommandType.STRING, required = false, description = "domain admin username in LDAP ")
6465
private String admin;
6566

66-
@Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.INTEGER, required = true, description = "Type of the account to auto import. Specify 0 for user and 2 for "
67+
@Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.INTEGER, required = false, description = "Type of the account to auto import. Specify 0 for user and 2 for "
6768
+ "domain admin")
68-
private int accountType;
69+
private Integer accountType;
70+
71+
@Parameter(name = ApiConstants.ROLE_ID, type = CommandType.UUID, entityType = RoleResponse.class, required = false, description = "Creates the account under the specified role.", since="4.19.1")
72+
private Long roleId;
6973

7074
@Inject
7175
private LdapManager _ldapManager;
@@ -134,7 +138,14 @@ public String getAdmin() {
134138
}
135139

136140
public Account.Type getAccountType() {
137-
return Account.Type.getFromValue(accountType);
141+
if (accountType == null) {
142+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), null);
143+
}
144+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType.intValue()));
145+
}
146+
147+
public Long getRoleId() {
148+
return RoleType.getRoleByAccountType(roleId, getAccountType());
138149
}
139150

140151
@Override

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,12 @@ public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
451451
Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");
452452
Validate.notNull(cmd.getType(), "type cannot be null. It should either be GROUP or OU");
453453
Validate.notEmpty(cmd.getLdapDomain(), "GROUP or OU name cannot be empty");
454+
Validate.isTrue(cmd.getAccountType() != null || cmd.getRoleId() != null, "Either account type or role ID must be given");
454455

455456
LinkType linkType = LdapManager.LinkType.valueOf(cmd.getType().toUpperCase());
456457
Account account = accountDao.findActiveAccount(cmd.getAccountName(),cmd.getDomainId());
457458
if (account == null) {
458-
account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), UUID.randomUUID().toString());
459+
account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), cmd.getRoleId(), UUID.randomUUID().toString());
459460
accountDao.persist((AccountVO)account);
460461
}
461462

test/integration/component/test_ldap.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ def setUpClass(cls):
5252

5353
@classmethod
5454
def tearDownClass(cls):
55-
try:
56-
cleanup_resources(cls.api_client, cls._cleanup)
57-
58-
except Exception as tde:
59-
raise Exception("Warning: Exception during cleanup : %s" % tde)
60-
return
55+
super(TestLdap, cls).tearDownClass()
6156

6257
def setUp(self):
6358

test/integration/plugins/ldap/test_ldap.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ def setUpClass(cls):
101101
def tearDownClass(cls):
102102
cls.logger.info("Tearing Down Class")
103103
try:
104-
cleanup_resources(cls.apiclient, reversed(cls._cleanup))
105-
cls.remove_ldap_configuration_for_domains()
106-
cls.logger.debug("done cleaning up resources in tearDownClass(cls) %s")
107-
except Exception as e:
108-
cls.logger.debug("Exception in tearDownClass(cls): %s" % e)
104+
super(TestLDAP, cls).tearDownClass()
105+
finally:
106+
try:
107+
cls.remove_ldap_configuration_for_domains()
108+
cls.logger.debug("done cleaning up resources in tearDownClass(cls) %s")
109+
except Exception as e:
110+
cls.logger.debug("Exception in tearDownClass(cls): %s" % e)
109111

110112
def setUp(self):
111113
self.cleanup = []
@@ -116,11 +118,7 @@ def setUp(self):
116118
return
117119

118120
def tearDown(self):
119-
try:
120-
cleanup_resources(self.apiclient, self.cleanup)
121-
except Exception as e:
122-
raise Exception("Warning: Exception during cleanup : %s" % e)
123-
return
121+
super(TestLDAP, self).tearDown()
124122

125123
@attr(tags=["smoke", "advanced"], required_hardware="false")
126124
def test_01_manual(self):
@@ -349,8 +347,8 @@ def create_domain(cls, domain_to_create, parent_domain = None):
349347
if parent_domain:
350348
domain_to_create["parentdomainid"] = parent_domain
351349
tmpDomain = Domain.create(cls.apiclient, domain_to_create)
352-
cls.logger.debug("Created domain %s with id %s " % (tmpDomain.name, tmpDomain.id))
353350
cls._cleanup.append(tmpDomain)
351+
cls.logger.debug("Created domain %s with id %s " % (tmpDomain.name, tmpDomain.id))
354352
return tmpDomain
355353

356354
@classmethod

0 commit comments

Comments
 (0)