Commit 2d9ece83 authored by nobodyiam's avatar nobodyiam

refactor a little bit

parent e25fe0ce
...@@ -6,7 +6,6 @@ import com.ctrip.framework.apollo.common.dto.AppDTO; ...@@ -6,7 +6,6 @@ import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.entity.App; import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.exception.BadRequestException; import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException; import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.exception.ServiceException;
import com.ctrip.framework.apollo.common.utils.BeanUtils; import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.InputValidator; import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.core.utils.StringUtils;
...@@ -55,7 +54,7 @@ public class AppController { ...@@ -55,7 +54,7 @@ public class AppController {
if (entity == null) { if (entity == null) {
throw new NotFoundException("app not found for appId " + appId); throw new NotFoundException("app not found for appId " + appId);
} }
appService.delete(entity.getId(), operator); adminService.deleteApp(entity, operator);
} }
@RequestMapping(value = "/apps/{appId:.+}", method = RequestMethod.PUT) @RequestMapping(value = "/apps/{appId:.+}", method = RequestMethod.PUT)
...@@ -92,21 +91,4 @@ public class AppController { ...@@ -92,21 +91,4 @@ public class AppController {
public boolean isAppIdUnique(@PathVariable("appId") String appId) { public boolean isAppIdUnique(@PathVariable("appId") String appId) {
return appService.isAppIdUnique(appId); return appService.isAppIdUnique(appId);
} }
@RequestMapping(value = "/apps", method = RequestMethod.DELETE)
public void deleteApp(@RequestParam("appId") String appId,
@RequestParam("operator") String operator) {
App app = appService.findOne(appId);
if (Objects.isNull(app)) {
throw new NotFoundException("app not found for appId " + appId);
}
try {
adminService.deleteApp(appId, operator);
} catch (Exception e) {
String exc = String
.format("user:%s deleting app:%s,failure:%s", operator, app, e.getMessage());
throw new ServiceException(exc);
}
}
} }
...@@ -28,7 +28,5 @@ public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNa ...@@ -28,7 +28,5 @@ public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNa
@Modifying @Modifying
@Query("UPDATE AppNamespace SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1") @Query("UPDATE AppNamespace SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId, String operator); int batchDeleteByAppId(String appId, String operator);
int countByAppId(String appId);
} }
...@@ -2,7 +2,6 @@ package com.ctrip.framework.apollo.biz.repository; ...@@ -2,7 +2,6 @@ package com.ctrip.framework.apollo.biz.repository;
import com.ctrip.framework.apollo.common.entity.App; import com.ctrip.framework.apollo.common.entity.App;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
...@@ -15,10 +14,4 @@ public interface AppRepository extends PagingAndSortingRepository<App, Long> { ...@@ -15,10 +14,4 @@ public interface AppRepository extends PagingAndSortingRepository<App, Long> {
List<App> findByName(@Param("name") String name); List<App> findByName(@Param("name") String name);
App findByAppId(String appId); App findByAppId(String appId);
@Modifying
@Query("UPDATE App SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int deleteApp(String appId, String operator);
int countByAppId(String appId);
} }
...@@ -3,8 +3,6 @@ package com.ctrip.framework.apollo.biz.repository; ...@@ -3,8 +3,6 @@ package com.ctrip.framework.apollo.biz.repository;
import com.ctrip.framework.apollo.biz.entity.Cluster; import com.ctrip.framework.apollo.biz.entity.Cluster;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List; import java.util.List;
...@@ -18,10 +16,4 @@ public interface ClusterRepository extends PagingAndSortingRepository<Cluster, L ...@@ -18,10 +16,4 @@ public interface ClusterRepository extends PagingAndSortingRepository<Cluster, L
Cluster findByAppIdAndName(String appId, String name); Cluster findByAppIdAndName(String appId, String name);
List<Cluster> findByParentClusterId(Long parentClusterId); List<Cluster> findByParentClusterId(Long parentClusterId);
@Modifying
@Query("UPDATE Cluster SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId, String operator);
int countByAppId(String appId);
} }
...@@ -18,9 +18,4 @@ public interface CommitRepository extends PagingAndSortingRepository<Commit, Lon ...@@ -18,9 +18,4 @@ public interface CommitRepository extends PagingAndSortingRepository<Commit, Lon
@Query("update Commit set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3") @Query("update Commit set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator); int batchDelete(String appId, String clusterName, String namespaceName, String operator);
@Modifying
@Query("UPDATE Commit SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId,String operator);
int countByAppId(String appId);
} }
...@@ -20,8 +20,6 @@ public interface GrayReleaseRuleRepository extends PagingAndSortingRepository<Gr ...@@ -20,8 +20,6 @@ public interface GrayReleaseRuleRepository extends PagingAndSortingRepository<Gr
List<GrayReleaseRule> findFirst500ByIdGreaterThanOrderByIdAsc(Long id); List<GrayReleaseRule> findFirst500ByIdGreaterThanOrderByIdAsc(Long id);
@Modifying @Modifying
@Query("UPDATE GrayReleaseRule SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1") @Query("UPDATE GrayReleaseRule SET IsDeleted=1, DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDeleteByDeleteApp(String appId, String operator); int batchDelete(String appId, String clusterName, String namespaceName, String operator);
int countByAppId(String appId);
} }
...@@ -25,9 +25,4 @@ public interface NamespaceRepository extends PagingAndSortingRepository<Namespac ...@@ -25,9 +25,4 @@ public interface NamespaceRepository extends PagingAndSortingRepository<Namespac
int countByNamespaceNameAndAppIdNot(String namespaceName, String appId); int countByNamespaceNameAndAppIdNot(String namespaceName, String appId);
@Modifying
@Query("UPDATE Namespace SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId,String operator);
int countByAppId(String appId);
} }
...@@ -23,9 +23,4 @@ public interface ReleaseHistoryRepository extends PagingAndSortingRepository<Rel ...@@ -23,9 +23,4 @@ public interface ReleaseHistoryRepository extends PagingAndSortingRepository<Rel
@Query("update ReleaseHistory set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3") @Query("update ReleaseHistory set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator); int batchDelete(String appId, String clusterName, String namespaceName, String operator);
@Modifying
@Query("UPDATE ReleaseHistory SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId, String operator);
int countByAppId(String appId);
} }
...@@ -35,10 +35,4 @@ public interface ReleaseRepository extends PagingAndSortingRepository<Release, L ...@@ -35,10 +35,4 @@ public interface ReleaseRepository extends PagingAndSortingRepository<Release, L
// For release history conversion program, need to delete after conversion it done // For release history conversion program, need to delete after conversion it done
List<Release> findByAppIdAndClusterNameAndNamespaceNameOrderByIdAsc(String appId, String clusterName, String namespaceName); List<Release> findByAppIdAndClusterNameAndNamespaceNameOrderByIdAsc(String appId, String clusterName, String namespaceName);
@Modifying
@Query("UPDATE Release SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String oldAppId, String operator);
int countByAppId(String appId);
} }
package com.ctrip.framework.apollo.biz.service; package com.ctrip.framework.apollo.biz.service;
import com.ctrip.framework.apollo.biz.entity.Cluster; import com.ctrip.framework.apollo.biz.entity.Cluster;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.common.entity.App; import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import com.google.common.collect.Sets;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -14,7 +13,6 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -14,7 +13,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
@Service @Service
public class AdminService { public class AdminService {
...@@ -27,18 +25,8 @@ public class AdminService { ...@@ -27,18 +25,8 @@ public class AdminService {
private ClusterService clusterService; private ClusterService clusterService;
@Autowired @Autowired
private NamespaceService namespaceService; private NamespaceService namespaceService;
@Autowired
private ReleaseHistoryService releaseHistoryService;
@Autowired
private ReleaseService releaseService;
@Autowired
private GrayReleaseRuleService grayReleaseRuleService;
@Autowired
private CommitService commitService;
@Autowired
private ItemService itemService;
final static Logger logger = LoggerFactory.getLogger(AdminService.class); private final static Logger logger = LoggerFactory.getLogger(AdminService.class);
@Transactional @Transactional
public App createNewApp(App app) { public App createNewApp(App app) {
...@@ -57,46 +45,24 @@ public class AdminService { ...@@ -57,46 +45,24 @@ public class AdminService {
} }
@Transactional @Transactional
public void deleteApp(String appId, String operator) { public void deleteApp(App app, String operator) {
String appId = app.getAppId();
logger.info("{} is deleting App:{}", operator, appId); logger.info("{} is deleting App:{}", operator, appId);
List<Cluster> managedClusters = clusterService.findClusters(appId); List<Cluster> managedClusters = clusterService.findClusters(appId);
Set<Namespace> managedNamespaces = Sets.newLinkedHashSet(); // 1. delete clusters
if (Objects.nonNull(managedClusters)) { if (Objects.nonNull(managedClusters)) {
for (Cluster cluster : managedClusters) { for (Cluster cluster : managedClusters) {
managedNamespaces.addAll(namespaceService.findNamespaces(appId, cluster.getName())); clusterService.delete(cluster.getId(), operator);
} }
} }
//1.delete release history. // 2. delete appNamespace
releaseHistoryService.batchDeleteByDeleteApp(appId, operator); appNamespaceService.batchDelete(appId, operator);
//2.delete release.
releaseService.deleteApp(appId, operator);
//3.delete config items.
for (Namespace namespace : managedNamespaces) {
itemService.batchDelete(namespace.getId(), operator);
}
//4.delete Namespaces
namespaceService.deleteApp(managedNamespaces, operator);
//5.delete GrayReleaseRule
grayReleaseRuleService.deleteApp(appId, operator);
//6.delete history.
commitService.deleteApp(appId, operator);
//7.delete cluster
clusterService.deleteApp(appId, operator);
//8.delete appNamespace
appNamespaceService.deleteApp(appId, operator);
//9.delete app // 3. delete app
appService.deleteApp(appId, operator); appService.delete(app.getId(), operator);
} }
} }
...@@ -139,9 +139,7 @@ public class AppNamespaceService { ...@@ -139,9 +139,7 @@ public class AppNamespaceService {
} }
@Transactional @Transactional
public void deleteApp(String appId,String operator) { public void batchDelete(String appId, String operator) {
if (appNamespaceRepository.countByAppId(appId) > 0) { appNamespaceRepository.batchDeleteByAppId(appId, operator);
appNamespaceRepository.batchDeleteByDeleteApp(appId, operator);
}
} }
} }
...@@ -92,11 +92,4 @@ public class AppService { ...@@ -92,11 +92,4 @@ public class AppService {
managedApp.getDataChangeLastModifiedBy()); managedApp.getDataChangeLastModifiedBy());
} }
@Transactional
public void deleteApp(String appId, String operator) {
if (appRepository.countByAppId(appId) > 0) {
appRepository.deleteApp(appId, operator);
}
}
} }
...@@ -137,13 +137,6 @@ public class ClusterService { ...@@ -137,13 +137,6 @@ public class ClusterService {
return clusterRepository.findByParentClusterId(parentCluster.getId()); return clusterRepository.findByParentClusterId(parentCluster.getId());
} }
@Transactional
public void deleteApp(String appId, String operator) {
if (clusterRepository.countByAppId(appId) > 0) {
clusterRepository.batchDeleteByDeleteApp(appId, operator);
}
}
public List<Cluster> findClusters(String appId) { public List<Cluster> findClusters(String appId) {
return clusterRepository.findByAppId(appId); return clusterRepository.findByAppId(appId);
} }
......
...@@ -31,10 +31,4 @@ public class CommitService { ...@@ -31,10 +31,4 @@ public class CommitService {
return commitRepository.batchDelete(appId, clusterName, namespaceName, operator); return commitRepository.batchDelete(appId, clusterName, namespaceName, operator);
} }
@Transactional
public void deleteApp(String appId,String operator) {
if (commitRepository.countByAppId(appId) > 0) {
commitRepository.batchDeleteByDeleteApp(appId, operator);
}
}
} }
...@@ -12,9 +12,7 @@ public class GrayReleaseRuleService { ...@@ -12,9 +12,7 @@ public class GrayReleaseRuleService {
private GrayReleaseRuleRepository grayReleaseRuleRepository; private GrayReleaseRuleRepository grayReleaseRuleRepository;
@Transactional @Transactional
public void deleteApp(String appId, String operator) { public void batchDelete(String appId, String clusterName, String namespaceName, String operator) {
if (grayReleaseRuleRepository.countByAppId(appId) > 0) { grayReleaseRuleRepository.batchDelete(appId, clusterName, namespaceName, operator);
grayReleaseRuleRepository.batchDeleteByDeleteApp(appId, operator);
}
} }
} }
...@@ -59,6 +59,8 @@ public class NamespaceService { ...@@ -59,6 +59,8 @@ public class NamespaceService {
@Autowired @Autowired
private ReleaseHistoryService releaseHistoryService; private ReleaseHistoryService releaseHistoryService;
@Autowired @Autowired
private GrayReleaseRuleService grayReleaseRuleService;
@Autowired
private NamespaceLockService namespaceLockService; private NamespaceLockService namespaceLockService;
@Autowired @Autowired
private InstanceService instanceService; private InstanceService instanceService;
...@@ -263,10 +265,10 @@ public class NamespaceService { ...@@ -263,10 +265,10 @@ public class NamespaceService {
itemService.batchDelete(namespace.getId(), operator); itemService.batchDelete(namespace.getId(), operator);
commitService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator); commitService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);
if (!isChildNamespace(namespace)) {
releaseService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator); releaseService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);
} grayReleaseRuleService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);
if (!isChildNamespace(namespace)) {
//delete child namespace //delete child namespace
Namespace childNamespace = findChildNamespace(namespace); Namespace childNamespace = findChildNamespace(namespace);
if (childNamespace != null) { if (childNamespace != null) {
...@@ -275,6 +277,7 @@ public class NamespaceService { ...@@ -275,6 +277,7 @@ public class NamespaceService {
//delete child namespace's releases. Notice: delete child namespace will not delete child namespace's releases //delete child namespace's releases. Notice: delete child namespace will not delete child namespace's releases
releaseService.batchDelete(appId, childNamespace.getClusterName(), namespaceName, operator); releaseService.batchDelete(appId, childNamespace.getClusterName(), namespaceName, operator);
} }
}
releaseHistoryService.batchDelete(appId, clusterName, namespaceName, operator); releaseHistoryService.batchDelete(appId, clusterName, namespaceName, operator);
...@@ -394,20 +397,4 @@ public class NamespaceService { ...@@ -394,20 +397,4 @@ public class NamespaceService {
return false; return false;
} }
@Transactional
public void deleteApp(Set<Namespace> namespaces, String operator) {
if (Objects.nonNull(namespaces)) {
String appId = namespaces.iterator().next().getAppId();
namespaceRepository.batchDeleteByDeleteApp(appId, operator);
//Publish release message
for (Namespace namespace : namespaces) {
messageSender.sendMessage(ReleaseMessageKeyGenerator
.generate(appId, namespace.getClusterName(), namespace.getNamespaceName()),
Topics.APOLLO_RELEASE_TOPIC);
}
}
}
} }
...@@ -76,11 +76,4 @@ public class ReleaseHistoryService { ...@@ -76,11 +76,4 @@ public class ReleaseHistoryService {
public int batchDelete(String appId, String clusterName, String namespaceName, String operator) { public int batchDelete(String appId, String clusterName, String namespaceName, String operator) {
return releaseHistoryRepository.batchDelete(appId, clusterName, namespaceName, operator); return releaseHistoryRepository.batchDelete(appId, clusterName, namespaceName, operator);
} }
@Transactional
public void batchDeleteByDeleteApp(String appId, String operator) {
if (releaseHistoryRepository.countByAppId(appId) > 0) {
releaseHistoryRepository.batchDeleteByDeleteApp(appId,operator);
}
}
} }
...@@ -467,10 +467,4 @@ public class ReleaseService { ...@@ -467,10 +467,4 @@ public class ReleaseService {
return releaseRepository.batchDelete(appId, clusterName, namespaceName, operator); return releaseRepository.batchDelete(appId, clusterName, namespaceName, operator);
} }
@Transactional
public void deleteApp(String appId, String operator) {
if (releaseRepository.countByAppId(appId) > 0) {
releaseRepository.batchDeleteByDeleteApp(appId, operator);
}
}
} }
...@@ -27,11 +27,4 @@ public class AppNamespaceRepositoryTest extends AbstractIntegrationTest{ ...@@ -27,11 +27,4 @@ public class AppNamespaceRepositoryTest extends AbstractIntegrationTest{
assertNull(appNamespace); assertNull(appNamespace);
} }
@Test
public void testDeleteAppNamespaceWhenDeleteApp() throws Exception{
String appId = "100003171";
int count = repository.countByAppId(appId);
assertEquals(count,repository.batchDeleteByDeleteApp(appId,null));
}
} }
...@@ -56,29 +56,4 @@ public class AppRepositoryTest extends AbstractIntegrationTest{ ...@@ -56,29 +56,4 @@ public class AppRepositoryTest extends AbstractIntegrationTest{
Assert.assertEquals(0, appRepository.count()); Assert.assertEquals(0, appRepository.count());
} }
@Test
public void testDelete() {
String appId = "someAppId";
String appName = "someAppName";
String ownerName = "someOwnerName";
String ownerEmail = "someOwnerName@ctrip.com";
String operator = "someOwnerName";
App app = new App();
app.setAppId(appId);
app.setName(appName);
app.setOwnerName(ownerName);
app.setOwnerEmail(ownerEmail);
Assert.assertEquals(0, appRepository.count());
appRepository.save(app);
Assert.assertEquals(1, appRepository.count());
Assert.assertEquals(1, appRepository.deleteApp(app.getAppId(), operator));
Assert.assertEquals(0, appRepository.count());
}
} }
...@@ -8,13 +8,12 @@ import com.ctrip.framework.apollo.biz.repository.AppRepository; ...@@ -8,13 +8,12 @@ import com.ctrip.framework.apollo.biz.repository.AppRepository;
import com.ctrip.framework.apollo.common.entity.App; import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.exception.ServiceException; import com.ctrip.framework.apollo.common.exception.ServiceException;
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.ConfigConsts;
import java.util.Date;
import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
public class AdminServiceTest extends AbstractIntegrationTest { public class AdminServiceTest extends AbstractIntegrationTest {
@Autowired @Autowired
...@@ -104,13 +103,14 @@ public class AdminServiceTest extends AbstractIntegrationTest { ...@@ -104,13 +103,14 @@ public class AdminServiceTest extends AbstractIntegrationTest {
Assert.assertEquals(1, namespaceService.findNamespaces(appId, ConfigConsts.CLUSTER_NAME_DEFAULT).size()); Assert.assertEquals(1, namespaceService.findNamespaces(appId, ConfigConsts.CLUSTER_NAME_DEFAULT).size());
adminService.deleteApp(appId, owner); adminService.deleteApp(app, owner);
Assert.assertEquals(0, appNamespaceService.findByAppId(appId).size()); Assert.assertEquals(0, appNamespaceService.findByAppId(appId).size());
Assert.assertEquals(0, clusterService.findClusters(appId).size()); Assert.assertEquals(0, clusterService.findClusters(appId).size());
Assert.assertEquals(0, namespaceService.findByAppIdAndNamespaceName(appId, ConfigConsts.CLUSTER_NAME_DEFAULT).size()); Assert
.assertEquals(0, namespaceService.findByAppIdAndNamespaceName(appId, ConfigConsts.CLUSTER_NAME_DEFAULT).size());
} }
} }
...@@ -2,6 +2,8 @@ package com.ctrip.framework.apollo.openapi.repository; ...@@ -2,6 +2,8 @@ package com.ctrip.framework.apollo.openapi.repository;
import com.ctrip.framework.apollo.openapi.entity.ConsumerRole; import com.ctrip.framework.apollo.openapi.entity.ConsumerRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List; import java.util.List;
...@@ -23,4 +25,8 @@ public interface ConsumerRoleRepository extends PagingAndSortingRepository<Consu ...@@ -23,4 +25,8 @@ public interface ConsumerRoleRepository extends PagingAndSortingRepository<Consu
List<ConsumerRole> findByRoleId(long roleId); List<ConsumerRole> findByRoleId(long roleId);
ConsumerRole findByConsumerIdAndRoleId(long consumerId, long roleId); ConsumerRole findByConsumerIdAndRoleId(long consumerId, long roleId);
@Modifying
@Query("UPDATE ConsumerRole SET IsDeleted=1, DataChange_LastModifiedBy = ?2 WHERE RoleId in ?1")
Integer batchDeleteByRoleIds(List<Long> roleIds, String operator);
} }
package com.ctrip.framework.apollo.portal.api; package com.ctrip.framework.apollo.portal.api;
import com.ctrip.framework.apollo.common.dto.*; import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.common.dto.CommitDTO;
import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleDTO;
import com.ctrip.framework.apollo.common.dto.InstanceDTO;
import com.ctrip.framework.apollo.common.dto.ItemChangeSets;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseHistoryDTO;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Health;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
...@@ -15,8 +32,6 @@ import org.springframework.util.CollectionUtils; ...@@ -15,8 +32,6 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import java.util.*;
@Service @Service
public class AdminServiceAPI { public class AdminServiceAPI {
...@@ -45,7 +60,7 @@ public class AdminServiceAPI { ...@@ -45,7 +60,7 @@ public class AdminServiceAPI {
} }
public void deleteApp(Env env, String appId, String operator) { public void deleteApp(Env env, String appId, String operator) {
restTemplate.delete(env, "/apps?appId={appId}&operator={operator}", appId, operator); restTemplate.delete(env, "/apps/{appId}?operator={operator}", appId, operator);
} }
} }
......
...@@ -142,14 +142,12 @@ public class AppController { ...@@ -142,14 +142,12 @@ public class AppController {
} }
@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@RequestMapping(value = "/{appId:.+}", method = RequestMethod.DELETE) @RequestMapping(value = "/{appId:.+}", method = RequestMethod.DELETE)
public void deleteApp(@PathVariable String appId) { public void deleteApp(@PathVariable String appId) {
App app = appService.deleteAppInLocal(appId);
App app = appService.load(appId);
publisher.publishEvent(new AppDeletionEvent(app)); publisher.publishEvent(new AppDeletionEvent(app));
appService.deleteAppInLocal(appId);
} }
@RequestMapping(value = "/{appId}/miss_envs", method = RequestMethod.GET) @RequestMapping(value = "/{appId}/miss_envs", method = RequestMethod.GET)
......
...@@ -8,7 +8,6 @@ public class AppDeletionEvent extends ApplicationEvent { ...@@ -8,7 +8,6 @@ public class AppDeletionEvent extends ApplicationEvent {
public AppDeletionEvent(Object source) { public AppDeletionEvent(Object source) {
super(source); super(source);
} }
public App getApp() { public App getApp() {
......
...@@ -24,8 +24,6 @@ public class AppInfoChangedListener { ...@@ -24,8 +24,6 @@ public class AppInfoChangedListener {
private AdminServiceAPI.AppAPI appAPI; private AdminServiceAPI.AppAPI appAPI;
@Autowired @Autowired
private PortalSettings portalSettings; private PortalSettings portalSettings;
@Autowired
private UserInfoHolder userInfoHolder;
@EventListener @EventListener
public void onAppInfoChange(AppInfoChangedEvent event) { public void onAppInfoChange(AppInfoChangedEvent event) {
...@@ -47,7 +45,7 @@ public class AppInfoChangedListener { ...@@ -47,7 +45,7 @@ public class AppInfoChangedListener {
public void onAppDelete(AppDeletionEvent event) { public void onAppDelete(AppDeletionEvent event) {
AppDTO appDTO = BeanUtils.transfrom(AppDTO.class, event.getApp()); AppDTO appDTO = BeanUtils.transfrom(AppDTO.class, event.getApp());
String appId = appDTO.getAppId(); String appId = appDTO.getAppId();
String operator = userInfoHolder.getUser().getName(); String operator = appDTO.getDataChangeLastModifiedBy();
List<Env> envs = portalSettings.getActiveEnvs(); List<Env> envs = portalSettings.getActiveEnvs();
for (Env env : envs) { for (Env env : envs) {
......
...@@ -18,7 +18,5 @@ public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNa ...@@ -18,7 +18,5 @@ public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNa
@Modifying @Modifying
@Query("UPDATE AppNamespace SET IsDeleted=1,DataChange_LastModifiedBy=?2 WHERE AppId=?1") @Query("UPDATE AppNamespace SET IsDeleted=1,DataChange_LastModifiedBy=?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId, String operator); int batchDeleteByAppId(String appId, String operator);
int countByAppId(String appId);
} }
...@@ -21,7 +21,5 @@ public interface FavoriteRepository extends PagingAndSortingRepository<Favorite, ...@@ -21,7 +21,5 @@ public interface FavoriteRepository extends PagingAndSortingRepository<Favorite,
@Modifying @Modifying
@Query("UPDATE Favorite SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1") @Query("UPDATE Favorite SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByDeleteApp(String appId, String operator); int batchDeleteByAppId(String appId, String operator);
int countByAppId(String appId);
} }
...@@ -8,6 +8,7 @@ import org.springframework.data.repository.PagingAndSortingRepository; ...@@ -8,6 +8,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.springframework.data.repository.query.Param;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -24,12 +25,10 @@ public interface PermissionRepository extends PagingAndSortingRepository<Permiss ...@@ -24,12 +25,10 @@ public interface PermissionRepository extends PagingAndSortingRepository<Permiss
List<Permission> findByPermissionTypeInAndTargetId(Collection<String> permissionTypes, List<Permission> findByPermissionTypeInAndTargetId(Collection<String> permissionTypes,
String targetId); String targetId);
/** @Query("SELECT p.id from Permission p where p.targetId = ?1 or p.targetId like CONCAT(?1, '+%'))")
* delete Permission when delete app. List<Long> findPermissionIdsByAppId(String appId);
*/
@Modifying @Modifying
@Query("UPDATE Permission SET IsDeleted=1," + @Query("UPDATE Permission SET IsDeleted=1, DataChange_LastModifiedBy = ?2 WHERE Id in ?1")
"TargetId=CONCAT('DELETED_',TargetId,'_',CURRENT_TIMESTAMP)," + Integer batchDelete(List<Long> permissionIds, String operator);
"DataChange_LastModifiedBy = ?2 WHERE TargetId LIKE ?1 OR TargetId LIKE CONCAT(?1,'+%')")
Integer batchDeleteByDeleteApp(String appId, String operator);
} }
...@@ -2,6 +2,8 @@ package com.ctrip.framework.apollo.portal.repository; ...@@ -2,6 +2,8 @@ package com.ctrip.framework.apollo.portal.repository;
import com.ctrip.framework.apollo.portal.entity.po.RolePermission; import com.ctrip.framework.apollo.portal.entity.po.RolePermission;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.Collection; import java.util.Collection;
...@@ -17,4 +19,7 @@ public interface RolePermissionRepository extends PagingAndSortingRepository<Rol ...@@ -17,4 +19,7 @@ public interface RolePermissionRepository extends PagingAndSortingRepository<Rol
*/ */
List<RolePermission> findByRoleIdIn(Collection<Long> roleId); List<RolePermission> findByRoleIdIn(Collection<Long> roleId);
@Modifying
@Query("UPDATE RolePermission SET IsDeleted=1, DataChange_LastModifiedBy = ?2 WHERE PermissionId in ?1")
Integer batchDeleteByPermissionIds(List<Long> permissionIds, String operator);
} }
package com.ctrip.framework.apollo.portal.repository; package com.ctrip.framework.apollo.portal.repository;
import com.ctrip.framework.apollo.portal.entity.po.Role; import com.ctrip.framework.apollo.portal.entity.po.Role;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public interface RoleRepository extends PagingAndSortingRepository<Role, Long> { public interface RoleRepository extends PagingAndSortingRepository<Role, Long> {
/** /**
* find role by role name * find role by role name
*/ */
Role findTopByRoleName(String roleName); Role findTopByRoleName(String roleName);
@Query("SELECT r.id from Role r where (r.roleName = CONCAT('Master+', ?1) "
+ "OR r.roleName like CONCAT('ModifyNamespace+', ?1, '+%') "
+ "OR r.roleName like CONCAT('ReleaseNamespace+', ?1, '+%'))")
List<Long> findRoleIdsByAppId(String appId);
@Modifying @Modifying
@Query("UPDATE Role SET IsDeleted=1," + @Query("UPDATE Role SET IsDeleted=1, DataChange_LastModifiedBy = ?2 WHERE Id in ?1")
"RoleName=CONCAT('DELETED_',RoleName,'_',CURRENT_TIMESTAMP)," + Integer batchDelete(List<Long> roleIds, String operator);
"DataChange_LastModifiedBy = ?2 WHERE RoleName LIKE CONCAT('Master+',?1) " +
"OR RoleName LIKE CONCAT('ModifyNamespace+',?1,'+%') " +
"OR RoleName LIKE CONCAT('ReleaseNamespace+',?1,'+%')")
Integer batchDeleteByDeleteApp(String appId, String operator);
} }
...@@ -2,6 +2,8 @@ package com.ctrip.framework.apollo.portal.repository; ...@@ -2,6 +2,8 @@ package com.ctrip.framework.apollo.portal.repository;
import com.ctrip.framework.apollo.portal.entity.po.UserRole; import com.ctrip.framework.apollo.portal.entity.po.UserRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.Collection; import java.util.Collection;
...@@ -26,4 +28,8 @@ public interface UserRoleRepository extends PagingAndSortingRepository<UserRole, ...@@ -26,4 +28,8 @@ public interface UserRoleRepository extends PagingAndSortingRepository<UserRole,
*/ */
List<UserRole> findByUserIdInAndRoleId(Collection<String> userId, long roleId); List<UserRole> findByUserIdInAndRoleId(Collection<String> userId, long roleId);
@Modifying
@Query("UPDATE UserRole SET IsDeleted=1, DataChange_LastModifiedBy = ?2 WHERE RoleId in ?1")
Integer batchDeleteByRoleIds(List<Long> roleIds, String operator);
} }
...@@ -118,9 +118,7 @@ public class AppNamespaceService { ...@@ -118,9 +118,7 @@ public class AppNamespaceService {
return createdAppNamespace; return createdAppNamespace;
} }
public void deleteApp(String appId, String operator) { public void batchDeleteByAppId(String appId, String operator) {
if (appNamespaceRepository.countByAppId(appId) > 0) { appNamespaceRepository.batchDeleteByAppId(appId, operator);
appNamespaceRepository.batchDeleteByDeleteApp(appId, operator);
}
} }
} }
...@@ -144,22 +144,28 @@ public class AppService { ...@@ -144,22 +144,28 @@ public class AppService {
} }
@Transactional @Transactional
public void deleteAppInLocal(String appId) { public App deleteAppInLocal(String appId) {
App managedApp = appRepository.findByAppId(appId); App managedApp = appRepository.findByAppId(appId);
if (managedApp == null) { if (managedApp == null) {
throw new BadRequestException(String.format("App not exists. AppId = %s", appId)); throw new BadRequestException(String.format("App not exists. AppId = %s", appId));
} }
String operator = userInfoHolder.getUser().getUserId(); String operator = userInfoHolder.getUser().getUserId();
//this operator is passed to com.ctrip.framework.apollo.portal.listener.AppInfoChangedListener.onAppDelete
managedApp.setDataChangeLastModifiedBy(operator);
//删除portal数据库中的app //删除portal数据库中的app
appRepository.deleteApp(appId, operator); appRepository.deleteApp(appId, operator);
//删除portal数据库中的appNamespace //删除portal数据库中的appNamespace
appNamespaceService.deleteApp(appId, operator); appNamespaceService.batchDeleteByAppId(appId, operator);
//删除portal数据库中的收藏表 //删除portal数据库中的收藏表
favoriteService.deleteApp(appId, operator); favoriteService.batchDeleteByAppId(appId, operator);
//删除portal数据库中Perimission、Role相关数据 //删除portal数据库中Permission、Role相关数据
rolePermissionService.deleteRolePermissionsByAppId(appId, operator); rolePermissionService.deleteRolePermissionsByAppId(appId, operator);
return managedApp;
} }
} }
...@@ -108,9 +108,7 @@ public class FavoriteService { ...@@ -108,9 +108,7 @@ public class FavoriteService {
} }
} }
public void deleteApp(String appId, String operator) { public void batchDeleteByAppId(String appId, String operator) {
if (favoriteRepository.countByAppId(appId) > 0) { favoriteRepository.batchDeleteByAppId(appId, operator);
favoriteRepository.batchDeleteByDeleteApp(appId, operator);
}
} }
} }
package com.ctrip.framework.apollo.portal.spi.defaultimpl; package com.ctrip.framework.apollo.portal.spi.defaultimpl;
import com.ctrip.framework.apollo.common.entity.BaseEntity;
import com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig; import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo; import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.entity.po.Permission; import com.ctrip.framework.apollo.portal.entity.po.Permission;
...@@ -16,6 +18,8 @@ import com.google.common.collect.FluentIterable; ...@@ -16,6 +18,8 @@ import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
...@@ -36,7 +40,8 @@ public class DefaultRolePermissionService implements RolePermissionService { ...@@ -36,7 +40,8 @@ public class DefaultRolePermissionService implements RolePermissionService {
private PermissionRepository permissionRepository; private PermissionRepository permissionRepository;
@Autowired @Autowired
private PortalConfig portalConfig; private PortalConfig portalConfig;
@Autowired
private ConsumerRoleRepository consumerRoleRepository;
/** /**
* Create role with permissions, note that role name should be unique * Create role with permissions, note that role name should be unique
...@@ -223,7 +228,27 @@ public class DefaultRolePermissionService implements RolePermissionService { ...@@ -223,7 +228,27 @@ public class DefaultRolePermissionService implements RolePermissionService {
@Transactional @Transactional
@Override @Override
public void deleteRolePermissionsByAppId(String appId, String operator) { public void deleteRolePermissionsByAppId(String appId, String operator) {
permissionRepository.batchDeleteByDeleteApp(appId, operator); List<Long> permissionIds = permissionRepository.findPermissionIdsByAppId(appId);
roleRepository.batchDeleteByDeleteApp(appId, operator);
if (!permissionIds.isEmpty()) {
// 1. delete Permission
permissionRepository.batchDelete(permissionIds, operator);
// 2. delete Role Permission
rolePermissionRepository.batchDeleteByPermissionIds(permissionIds, operator);
}
List<Long> roleIds = roleRepository.findRoleIdsByAppId(appId);
if (!roleIds.isEmpty()) {
// 3. delete Role
roleRepository.batchDelete(roleIds, operator);
// 4. delete User Role
userRoleRepository.batchDeleteByRoleIds(roleIds, operator);
// 5. delete Consumer Role
consumerRoleRepository.batchDeleteByRoleIds(roleIds, operator);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment