From 47fdc1e81e389c46413c8e1c93fe4caf70e32533 Mon Sep 17 00:00:00 2001 From: davidjumani Date: Mon, 22 Jun 2020 18:55:47 +0530 Subject: [PATCH] Adding listall to listLdapConfigurations --- .../api/command/LdapListConfigurationCmd.java | 8 ++++++ .../cloudstack/ldap/LdapManagerImpl.java | 3 +- .../ldap/dao/LdapConfigurationDao.java | 4 +++ .../ldap/dao/LdapConfigurationDaoImpl.java | 28 +++++++++++++++---- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java index db6318e6b2cd..d12ca4ab6c1c 100644 --- a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java +++ b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java @@ -55,6 +55,10 @@ public class LdapListConfigurationCmd extends BaseListCmd { @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain") private Long domainId; + @Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN, description = "If set to true, " + + " and no domainid specified, list all LDAP configurations irrespective of the linked domain", since = "4.13.2") + private Boolean listAll; + public LdapListConfigurationCmd() { super(); } @@ -117,4 +121,8 @@ public void setPort(final int port) { public void setDomainId(final Long domainId) { this.domainId = domainId; } + + public boolean listAll() { + return listAll != null && listAll; + } } diff --git a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java index 547c10b7b1d5..7b1216dbbb60 100644 --- a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java +++ b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java @@ -291,7 +291,8 @@ public Pair, Integer> listConfigurations(fin final String hostname = cmd.getHostname(); final int port = cmd.getPort(); final Long domainId = cmd.getDomainId(); - final Pair, Integer> result = _ldapConfigurationDao.searchConfigurations(hostname, port, domainId); + final boolean listAll = cmd.listAll(); + final Pair, Integer> result = _ldapConfigurationDao.searchConfigurations(hostname, port, domainId, listAll); return new Pair, Integer>(result.first(), result.second()); } diff --git a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java index e99c78be9b78..6e618ca97e26 100644 --- a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java +++ b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java @@ -37,5 +37,9 @@ public interface LdapConfigurationDao extends GenericDao, Integer> searchConfigurations(String hostname, int port, Long domainId); + + Pair, Integer> searchConfigurations(String hostname, int port, Long domainId, boolean listAll); } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java index fa4c0af236f8..78c9bae4d0a7 100644 --- a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java +++ b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java @@ -46,6 +46,7 @@ public LdapConfigurationDaoImpl() { listGlobalConfigurationsSearch.and("port", listGlobalConfigurationsSearch.entity().getPort(), Op.EQ); listGlobalConfigurationsSearch.and("domain_id", listGlobalConfigurationsSearch.entity().getDomainId(),SearchCriteria.Op.NULL); listGlobalConfigurationsSearch.done(); + listDomainConfigurationsSearch = createSearchBuilder(); listDomainConfigurationsSearch.and("hostname", listDomainConfigurationsSearch.entity().getHostname(), Op.EQ); listDomainConfigurationsSearch.and("port", listDomainConfigurationsSearch.entity().getPort(), Op.EQ); @@ -62,23 +63,38 @@ public LdapConfigurationVO findByHostname(final String hostname) { @Override public LdapConfigurationVO find(String hostname, int port, Long domainId) { - SearchCriteria sc = getSearchCriteria(hostname, port, domainId); + SearchCriteria sc = getSearchCriteria(hostname, port, domainId, false); + return findOneBy(sc); + } + + @Override + public LdapConfigurationVO find(String hostname, int port, Long domainId, boolean listAll) { + SearchCriteria sc = getSearchCriteria(hostname, port, domainId, listAll); return findOneBy(sc); } @Override public Pair, Integer> searchConfigurations(final String hostname, final int port, final Long domainId) { - SearchCriteria sc = getSearchCriteria(hostname, port, domainId); + SearchCriteria sc = getSearchCriteria(hostname, port, domainId, false); return searchAndCount(sc, null); } - private SearchCriteria getSearchCriteria(String hostname, int port, Long domainId) { + @Override + public Pair, Integer> searchConfigurations(final String hostname, final int port, final Long domainId, final boolean listAll) { + SearchCriteria sc = getSearchCriteria(hostname, port, domainId, listAll); + return searchAndCount(sc, null); + } + + private SearchCriteria getSearchCriteria(String hostname, int port, Long domainId,boolean listAll) { SearchCriteria sc; - if (domainId == null) { - sc = listDomainConfigurationsSearch.create(); - } else { + if (domainId != null) { + // If domainid is present, ignore listall sc = listDomainConfigurationsSearch.create(); sc.setParameters("domain_id", domainId); + } else if (listAll) { + sc = listDomainConfigurationsSearch.create(); + } else { + sc = listGlobalConfigurationsSearch.create(); } if (hostname != null) { sc.setParameters("hostname", hostname);