From 5e812291819eeff3a986f1f9a4ab58b5c09cd507 Mon Sep 17 00:00:00 2001 From: dewniMW Date: Thu, 12 May 2022 06:58:35 +0530 Subject: [PATCH] Add tenant activate and deactivate by tenant id --- .../tenant/mgt/services/TenantMgtImpl.java | 53 +++++++++++++++++++ .../tenant/mgt/services/TenantMgtService.java | 22 +++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtImpl.java b/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtImpl.java index 465e4272..bc2c195b 100644 --- a/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtImpl.java +++ b/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtImpl.java @@ -209,6 +209,32 @@ public void activateTenant(String tenantUniqueID) throws TenantMgtException { PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername() + "'"); } + @Override + public void activateTenant(int tenantId) throws TenantMgtException { + + TenantManager tenantManager = TenantMgtServiceComponent.getTenantManager(); + String tenantDomain = null; + try { + Tenant tenant = (Tenant) tenantManager.getTenant(tenantId); + if (tenant == null) { + throw new TenantManagementClientException(ERROR_CODE_RESOURCE_NOT_FOUND.getCode(), + String.format(ERROR_CODE_RESOURCE_NOT_FOUND.getMessage(), tenantId)); + } + tenantManager.activateTenant(tenantId); + tenantDomain = tenant.getDomain(); + // Notify tenant activation to all listeners. + TenantMgtUtil.triggerTenantActivation(tenantId); + log.info(String.format("Tenant with id: %s and domain: %s activated by %s", tenantId, + tenantDomain, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername())); + } catch (org.wso2.carbon.user.api.UserStoreException e) { + throw new TenantManagementServerException(String.format("Error while retrieving or deactivating the " + + "tenant with id: %s", tenantId), e); + } catch (StratosException e) { + throw new TenantManagementServerException(String.format("Error in notifying tenant activation of tenant " + + "with id: %s and domain: %s", tenantId, tenantDomain), e); + } + } + public void deactivateTenant(String tenantUniqueID) throws TenantMgtException { TenantManager tenantManager = TenantMgtServiceComponent.getTenantManager(); @@ -239,6 +265,33 @@ public void deactivateTenant(String tenantUniqueID) throws TenantMgtException { } } + @Override + public void deactivateTenant(int tenantId) throws TenantMgtException { + + TenantManager tenantManager = TenantMgtServiceComponent.getTenantManager(); + String tenantDomain = null; + try { + Tenant tenant = (Tenant) tenantManager.getTenant(tenantId); + if (tenant == null) { + throw new TenantManagementClientException(ERROR_CODE_RESOURCE_NOT_FOUND.getCode(), + String.format(ERROR_CODE_RESOURCE_NOT_FOUND.getMessage(), tenantId)); + } + tenantDomain = tenant.getDomain(); + // Notify tenant deactivation to all listeners. + TenantMgtUtil.triggerTenantDeactivation(tenantId); + tenantManager.deactivateTenant(tenantId); + TenantMgtUtil.unloadTenantConfigurations(tenantDomain, tenantId); + log.info(String.format("Tenant with id: %s and domain: %s deactivated by %s", tenantId, + tenantDomain, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername())); + } catch (org.wso2.carbon.user.api.UserStoreException e) { + throw new TenantManagementServerException(String.format("Error while retrieving or deactivating the " + + "tenant with id: %s", tenantId), e); + } catch (StratosException e) { + throw new TenantManagementServerException(String.format("Error while triggering tenant deactivation for " + + "the tenant with id: %s and domain: %s", tenantId, tenantDomain), e); + } + } + @Override public void deleteTenantMetaData(String tenantUniqueIdentifier) throws TenantMgtException { diff --git a/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtService.java b/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtService.java index e8385803..4f45c3f8 100644 --- a/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtService.java +++ b/components/tenant-mgt/org.wso2.carbon.tenant.mgt/src/main/java/org/wso2/carbon/tenant/mgt/services/TenantMgtService.java @@ -93,13 +93,33 @@ TenantSearchResult listTenants(Integer limit, Integer offset, String sortOrder, void activateTenant(String tenantUniqueIdentifier) throws TenantMgtException; /** - * Deactivate the given tenant. + * Activate the given tenant by the tenant id. + * + * @param tenantId tenant id. + * @throws TenantMgtException if tenant activation fails. + */ + default void activateTenant(int tenantId) throws TenantMgtException { + + } + + /** + * Deactivate the given tenant by the tenant uuid. * * @param tenantUniqueIdentifier tenant uuid. * @throws TenantMgtException if tenant deactivation fails. */ void deactivateTenant(String tenantUniqueIdentifier) throws TenantMgtException; + /** + * Deactivate the given tenant by the tenant id. + * + * @param tenantId tenant id. + * @throws TenantMgtException if tenant deactivation fails. + */ + default void deactivateTenant(int tenantId) throws TenantMgtException { + + } + /** * Delete tenant metadata. *