refactor: 资源池校验优化

This commit is contained in:
CaptainB 2022-10-20 11:37:54 +08:00 committed by 刘瑞斌
parent 07357b4e47
commit baafb0459a
4 changed files with 56 additions and 118 deletions

View File

@ -1,23 +0,0 @@
package io.metersphere.controller;
import io.metersphere.dto.TestResourcePoolDTO;
import io.metersphere.service.BaseTestResourcePoolService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/testresourcepool")
public class BaseResourcePoolController {
@Resource
private BaseTestResourcePoolService baseTestResourcePoolService;
@GetMapping("list/all/valid")
public List<TestResourcePoolDTO> listValidResourcePools() {
return baseTestResourcePoolService.listValidResourcePools();
}
}

View File

@ -6,12 +6,9 @@ import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.base.domain.TestResourcePoolExample; import io.metersphere.base.domain.TestResourcePoolExample;
import io.metersphere.base.mapper.TestResourceMapper; import io.metersphere.base.mapper.TestResourceMapper;
import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.base.mapper.ext.BaseTaskMapper;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.TestResourcePoolDTO; import io.metersphere.dto.TestResourcePoolDTO;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.OperatingLogDetails;
@ -29,11 +26,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static io.metersphere.commons.constants.ResourceStatusEnum.*; import static io.metersphere.commons.constants.ResourceStatusEnum.DELETE;
/**
* @author dongbin
*/
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class BaseTestResourcePoolService { public class BaseTestResourcePoolService {
@ -42,61 +36,6 @@ public class BaseTestResourcePoolService {
private TestResourcePoolMapper testResourcePoolMapper; private TestResourcePoolMapper testResourcePoolMapper;
@Resource @Resource
private TestResourceMapper testResourceMapper; private TestResourceMapper testResourceMapper;
@Resource
private NodeResourcePoolService nodeResourcePoolService;
@Resource
private BaseTaskMapper baseTaskMapper;
public void checkTestResourcePool(TestResourcePoolDTO testResourcePoolDTO) {
String resourcePoolName = testResourcePoolDTO.getName();
if (StringUtils.isBlank(resourcePoolName)) {
MSException.throwException(Translator.get("test_resource_pool_name_is_null"));
}
TestResourcePoolExample example = new TestResourcePoolExample();
TestResourcePoolExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(resourcePoolName);
if (StringUtils.isNotBlank(testResourcePoolDTO.getId())) {
criteria.andIdNotEqualTo(testResourcePoolDTO.getId());
}
criteria.andStatusNotEqualTo(DELETE.name());
if (testResourcePoolMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("test_resource_pool_name_already_exists"));
}
}
public void updateTestResourcePoolStatus(String poolId, String status) {
TestResourcePool testResourcePool = testResourcePoolMapper.selectByPrimaryKey(poolId);
if (testResourcePool == null) {
MSException.throwException("Resource Pool not found.");
}
testResourcePool.setUpdateTime(System.currentTimeMillis());
testResourcePool.setStatus(status);
// 禁用/删除 资源池
if (INVALID.name().equals(status) || DELETE.name().equals(status)) {
testResourcePoolMapper.updateByPrimaryKeySelective(testResourcePool);
return;
}
TestResourcePoolDTO testResourcePoolDTO = new TestResourcePoolDTO();
try {
BeanUtils.copyProperties(testResourcePoolDTO, testResourcePool);
TestResourceExample example2 = new TestResourceExample();
example2.createCriteria().andTestResourcePoolIdEqualTo(poolId);
List<TestResource> testResources = testResourceMapper.selectByExampleWithBLOBs(example2);
testResourcePoolDTO.setResources(testResources);
if (validateTestResourcePool(testResourcePoolDTO)) {
testResourcePoolMapper.updateByPrimaryKeySelective(testResourcePool);
} else {
MSException.throwException("Resource Pool is invalid.");
}
} catch (IllegalAccessException | InvocationTargetException e) {
LogUtil.error(e.getMessage(), e);
}
}
public List<TestResourcePoolDTO> listResourcePools(QueryResourcePoolRequest request) { public List<TestResourcePoolDTO> listResourcePools(QueryResourcePoolRequest request) {
TestResourcePoolExample example = new TestResourcePoolExample(); TestResourcePoolExample example = new TestResourcePoolExample();
@ -127,43 +66,11 @@ public class BaseTestResourcePoolService {
return testResourcePoolDTOS; return testResourcePoolDTOS;
} }
private boolean validateTestResourcePool(TestResourcePoolDTO testResourcePool) {
// todo
// if (StringUtils.equalsIgnoreCase(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.name())) {
// KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class);
// if (resourcePoolService == null) {
// return false;
// }
// return resourcePoolService.validate(testResourcePool);
// }
return nodeResourcePoolService.validate(testResourcePool);
}
public TestResourcePool getResourcePool(String resourcePoolId) { public TestResourcePool getResourcePool(String resourcePoolId) {
return testResourcePoolMapper.selectByPrimaryKey(resourcePoolId); return testResourcePoolMapper.selectByPrimaryKey(resourcePoolId);
} }
public List<TestResourcePoolDTO> listValidResourcePools() {
QueryResourcePoolRequest request = new QueryResourcePoolRequest();
List<TestResourcePoolDTO> testResourcePools = listResourcePools(request);
// 重新校验 pool
for (TestResourcePoolDTO pool : testResourcePools) {
// 手动设置成无效的, 排除
if (INVALID.name().equals(pool.getStatus())) {
continue;
}
try {
validateTestResourcePool(pool);
} catch (Throwable e) {
LogUtil.error(e.getMessage(), e);
pool.setStatus(INVALID.name());
pool.setUpdateTime(System.currentTimeMillis());
testResourcePoolMapper.updateByPrimaryKeySelective(pool);
}
}
request.setStatus(VALID.name());
return listResourcePools(request);
}
public String getLogDetails(String id) { public String getLogDetails(String id) {
TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(id); TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(id);

View File

@ -19,4 +19,9 @@ public class ValidQuotaResourcePoolController {
public List<TestResourcePoolDTO> listValidQuotaResourcePools() { public List<TestResourcePoolDTO> listValidQuotaResourcePools() {
return validQuotaResourcePoolService.listValidQuotaResourcePools(); return validQuotaResourcePoolService.listValidQuotaResourcePools();
} }
@GetMapping("list/all/valid")
public List<TestResourcePoolDTO> listValidResourcePools() {
return validQuotaResourcePoolService.listValidResourcePools();
}
} }

View File

@ -1,9 +1,16 @@
package io.metersphere.xpack.resourcepool.service; package io.metersphere.xpack.resourcepool.service;
import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.commons.constants.ResourcePoolTypeEnum;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.TestResourcePoolDTO; import io.metersphere.dto.TestResourcePoolDTO;
import io.metersphere.request.resourcepool.QueryResourcePoolRequest;
import io.metersphere.service.BaseTestResourcePoolService; import io.metersphere.service.BaseTestResourcePoolService;
import io.metersphere.service.NodeResourcePoolService;
import io.metersphere.xpack.quota.service.QuotaService; import io.metersphere.xpack.quota.service.QuotaService;
import io.metersphere.xpack.resourcepool.engine.KubernetesResourcePoolService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -11,13 +18,20 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.metersphere.commons.constants.ResourceStatusEnum.INVALID;
import static io.metersphere.commons.constants.ResourceStatusEnum.VALID;
@Service @Service
public class ValidQuotaResourcePoolService { public class ValidQuotaResourcePoolService {
@Resource @Resource
private BaseTestResourcePoolService baseTestResourcePoolService; private BaseTestResourcePoolService baseTestResourcePoolService;
@Resource
private NodeResourcePoolService nodeResourcePoolService;
@Resource
private TestResourcePoolMapper testResourcePoolMapper;
public List<TestResourcePoolDTO> listValidQuotaResourcePools() { public List<TestResourcePoolDTO> listValidQuotaResourcePools() {
return filterQuota(baseTestResourcePoolService.listValidResourcePools()); return filterQuota(listValidResourcePools());
} }
private List<TestResourcePoolDTO> filterQuota(List<TestResourcePoolDTO> list) { private List<TestResourcePoolDTO> filterQuota(List<TestResourcePoolDTO> list) {
@ -31,4 +45,39 @@ public class ValidQuotaResourcePoolService {
} }
return list; return list;
} }
private boolean validateTestResourcePool(TestResourcePoolDTO testResourcePool) {
if (StringUtils.equalsIgnoreCase(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.name())) {
KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class);
if (resourcePoolService == null) {
return false;
}
return resourcePoolService.validate(testResourcePool);
}
return nodeResourcePoolService.validate(testResourcePool);
}
public List<TestResourcePoolDTO> listValidResourcePools() {
QueryResourcePoolRequest request = new QueryResourcePoolRequest();
List<TestResourcePoolDTO> testResourcePools = baseTestResourcePoolService.listResourcePools(request);
// 重新校验 pool
for (TestResourcePoolDTO pool : testResourcePools) {
// 手动设置成无效的, 排除
if (INVALID.name().equals(pool.getStatus())) {
continue;
}
try {
validateTestResourcePool(pool);
} catch (Throwable e) {
LogUtil.error(e.getMessage(), e);
pool.setStatus(INVALID.name());
pool.setUpdateTime(System.currentTimeMillis());
testResourcePoolMapper.updateByPrimaryKeySelective(pool);
}
}
request.setStatus(VALID.name());
return baseTestResourcePoolService.listResourcePools(request);
}
} }