Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to delete all the resources belongs to a given resource type in a tenant. #5030

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,15 @@ void deleteFileById(String resourceType, String resourceName, String fileId)
* @throws ConfigurationManagementException Configuration management exception.
*/
void replaceResource(Resource resource) throws ConfigurationManagementException;

/**
* This function is used to delete all the resources belongs to given resource type in the current tenant.
*
* @param resourceType Request to delete the resources for the given {@link ResourceType}.
* @throws ConfigurationManagementException Configuration management exception.
*/
default void deleteResourcesByType(String resourceType) throws ConfigurationManagementException {

throw new NotImplementedException("This functionality is not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ private void validateSearchRequest(Condition condition) throws ConfigurationMana
}
}

@Override
public void deleteResourcesByType(String resourceTypeName) throws ConfigurationManagementException {

ResourceType resourceType = getResourceType(resourceTypeName);
this.getConfigurationDAO().deleteResourcesByType(getTenantId(), resourceType.getId());
if (log.isDebugEnabled()) {
log.debug("Resources belongs to Resource Type : " + resourceTypeName + " is deleted successfully.");
}
}

private void validateResourceRetrieveRequest(String resourceTypeName, String resourceName)
throws ConfigurationManagementException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,6 @@ public class SQLConstants {
+ "MODIFIED, IDN_CONFIG_RESOURCE.CREATED_TIME, IDN_CONFIG_RESOURCE.HAS_FILE, IDN_CONFIG_RESOURCE"
+ ".HAS_ATTRIBUTE FROM IDN_CONFIG_RESOURCE WHERE IDN_CONFIG_RESOURCE."
+ "TYPE_ID = ? and IDN_CONFIG_RESOURCE.TENANT_ID = ?";
public static final String DELETE_RESOURCES_BY_RESOURCE_TYPE_ID_SQL =
"DELETE FROM IDN_CONFIG_RESOURCE WHERE TYPE_ID = ? and TENANT_ID = ?";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.wso2.carbon.identity.configuration.mgt.core.dao;

import org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException;
import org.wso2.carbon.identity.configuration.mgt.core.exception.NotImplementedException;
import org.wso2.carbon.identity.configuration.mgt.core.model.Attribute;
import org.wso2.carbon.identity.configuration.mgt.core.model.Resource;
import org.wso2.carbon.identity.configuration.mgt.core.model.ResourceFile;
Expand Down Expand Up @@ -312,4 +313,16 @@ List getResourcesByType(int tenantId, String resourceTypeId)
* @throws ConfigurationManagementException if an error occurs while validating the resourceId.
*/
boolean isExistingResource(int tenantId, String resourceId) throws ConfigurationManagementException;

/**
* Delete all the resources by the given resource type in the given tenant.
*
* @param tenantId Id of the tenant
* @param resourceTypeId Id of the {@link ResourceType}.
* @throws ConfigurationManagementException Configuration Management Exception.
*/
default void deleteResourcesByType(int tenantId, String resourceTypeId) throws ConfigurationManagementException {

throw new NotImplementedException("This functionality is not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import java.io.InputStream;
import java.util.List;

import static org.wso2.carbon.identity.configuration.mgt.core.util.ConfigurationUtils.handleClientException;

/**
* This is a wrapper data access object to the default data access object to provide caching functionalities.
*/
Expand Down Expand Up @@ -273,6 +275,22 @@ public boolean isExistingResource(int tenantId, String resourceId) throws Config
return configurationDAO.isExistingResource(tenantId, resourceId);
}

@Override
public void deleteResourcesByType(int tenantId, String resourceTypeId) throws ConfigurationManagementException {

List<Resource> resourceList = configurationDAO.getResourcesByType(tenantId, resourceTypeId);
if (resourceList.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("No resource found for the resourceTypeId: " + resourceTypeId + " in tenant: " + tenantId);
}
throw handleClientException(ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCES_DOES_NOT_EXISTS);
}
configurationDAO.deleteResourcesByType(tenantId, resourceTypeId);
for (Resource resource : resourceList) {
deleteResourceFromCache(resource);
}
}

private Resource getResourceFromCacheById(String resourceId, int tenantId)
throws ConfigurationManagementException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,34 @@ public List<Resource> getResourcesByType(int tenantId, String resourceTypeId)
}
}

@Override
public void deleteResourcesByType(int tenantId, String resourceTypeId) throws ConfigurationManagementException {

JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
if (isMySQLDB()) {
/**
* Need to first delete files since in MySql Clustering scripts for NDB engine `ON DELETE CASCADE`is no
* longer supported for foreign keys on NDB tables when the child table contains a column that uses any
* of the BLOB or TEXT types. Issue: (https://github.com/wso2/product-is/issues/12167).
*/
List<Resource> resourceList = getResourcesByType(tenantId, resourceTypeId);
if (!resourceList.isEmpty()) {
for (Resource resource : resourceList) {
deleteFiles(resource.getResourceId());
}
}
}
jdbcTemplate.executeUpdate(SQLConstants.DELETE_RESOURCES_BY_RESOURCE_TYPE_ID_SQL, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resourceTypeId);
preparedStatement.setInt(++initialParameterIndex, tenantId);
});
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_RESOURCES_DOES_NOT_EXISTS, e);
}
}

/**
* Get Attributes for the {@link Resource}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public class ConfigurationUtils {

private static final Log log = LogFactory.getLog(ConfigurationUtils.class);

/**
* This method can be used to generate a ConfigurationManagementClientException from
* ConfigurationConstants.ErrorMessages object when no exception is thrown.
*
* @param error ConfigurationConstants.ErrorMessages.
* @return ConfigurationManagementClientException.
*/
public static ConfigurationManagementClientException handleClientException(
ConfigurationConstants.ErrorMessages error) {

return new ConfigurationManagementClientException(error.getMessage(), error.getCode());
}

/**
* This method can be used to generate a ConfigurationManagementClientException from
* ConfigurationConstants.ErrorMessages object when no exception is thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,27 @@ public void testSearchTenantSpecificResources() throws Exception {
assertTrue(isTenantSearchConditionMatch(resources));
}

@Test(priority = 33)
public void testDeleteResourcesByType() throws Exception {

ResourceType resourceType = configurationManager.addResourceType(getSampleResourceType2Add());
Resource resource1 = configurationManager.addResource(resourceType.getName(), getSampleResource1Add());
Resource resource2 = configurationManager.addResource(resourceType.getName(), getSampleResource2Add());

Resources resourcesByType = configurationManager.getResourcesByType(resourceType.getName());
Assert.assertTrue("Retrieved resource count should be equal to the added value",
resourcesByType.getResources().size() == 2);
Assert.assertEquals("Created resource name should be equal to the retrieved resource name",
resource1.getResourceName(), resourcesByType.getResources().get(0).getResourceName());
Assert.assertEquals("Created resource name should be equal to the retrieved resource name",
resource2.getResourceName(), resourcesByType.getResources().get(1).getResourceName());

configurationManager.deleteResourcesByType(resourceType.getName());
resourcesByType = configurationManager.getResourcesByType(resourceType.getName());
Assert.assertTrue("Retrieved resource count should be equal to the 0",
resourcesByType.getResources().size() == 0);
}

private void removeCreatedTimeColumn() throws DataAccessException {

JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
Expand Down
Loading