From f26ec51b44cbbe0db3eca108bbae8891f844817b Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Wed, 5 Jul 2023 16:16:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=B1=A0=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../KubernetesResourcePoolService.java | 2 +- .../sdk/service/LoadResourceService.java | 10 ---- .../sdk/service/NodeResourcePoolService.java | 54 ++++++++++++++++++- .../sdk/service/TestResourcePoolService.java | 42 +++++++++------ .../resources/i18n/commons_en_US.properties | 6 +++ .../resources/i18n/commons_zh_CN.properties | 8 +++ .../resources/i18n/commons_zh_TW.properties | 6 +++ 7 files changed, 99 insertions(+), 29 deletions(-) delete mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoadResourceService.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/KubernetesResourcePoolService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/KubernetesResourcePoolService.java index 5fdf9d0f2a..5c80b58d99 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/KubernetesResourcePoolService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/KubernetesResourcePoolService.java @@ -5,5 +5,5 @@ import io.metersphere.sdk.dto.TestResourceDTO; public interface KubernetesResourcePoolService { - boolean validate(TestResourceDTO testResourceDT); + boolean validate(TestResourceDTO testResourceDTO, Boolean usedApiType); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoadResourceService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoadResourceService.java deleted file mode 100644 index 85e476792f..0000000000 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoadResourceService.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.metersphere.sdk.service; - - -import io.metersphere.sdk.dto.TestResourceDTO; - -public interface LoadResourceService { - - boolean validate(TestResourceDTO testResourceDTO, String type); - -} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java index cc9f3e3270..78a54783a8 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/NodeResourcePoolService.java @@ -1,22 +1,72 @@ package io.metersphere.sdk.service; +import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.TestResourceDTO; +import io.metersphere.sdk.dto.TestResourceNodeDTO; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.Translator; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.client.RestTemplate; + +import java.util.List; @Service @Transactional(rollbackFor = Exception.class) public class NodeResourcePoolService { + private final static String nodeControllerUrl = "http://%s:%s/status"; + + private static final RestTemplate restTemplateWithTimeOut = new RestTemplate(); + static { + HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); + httpRequestFactory.setConnectionRequestTimeout(2000); + httpRequestFactory.setConnectTimeout(2000); + restTemplateWithTimeOut.setRequestFactory(httpRequestFactory); + } + public boolean validate(TestResourceDTO testResourceDTO) { - if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { + List nodesList = testResourceDTO.getNodesList(); + if (CollectionUtils.isEmpty(nodesList)) { throw new MSException(Translator.get("no_nodes_message")); } //校验节点 - return true; + List> ipPort = nodesList.stream() + .map(resource -> { + return new ImmutablePair<>(resource.getIp(), resource.getPort()); + }) + .distinct() + .toList(); + if (ipPort.size() < nodesList.size()) { + throw new MSException(Translator.get("duplicate_node_ip_port")); + } + boolean isValid = true; + for (TestResourceNodeDTO testResourceNodeDTO : nodesList) { + isValid = validateNode(testResourceNodeDTO); + } + return isValid; + } + + private boolean validateNode(TestResourceNodeDTO node) { + try { + ResponseEntity entity = restTemplateWithTimeOut.getForEntity(String.format(nodeControllerUrl, node.getIp(), node.getPort()), ResultHolder.class); + ResultHolder body = entity.getBody(); + if (body == null) { + return false; + } + if (body.getData() != null && StringUtils.equalsIgnoreCase("OK", body.getData().toString())) { + return true; + } + } catch (Exception e) { + LogUtils.error(e.getMessage(), e); + } + return false; } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java index 022a4c9de8..3b716e6817 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java @@ -24,7 +24,6 @@ import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -104,11 +103,28 @@ public class TestResourcePoolService { if (testResourcePool.getLoadTest() == null || !testResourcePool.getLoadTest()) { return true; } - LoadResourceService resourcePoolService = CommonBeanFactory.getBean(LoadResourceService.class); - if (resourcePoolService == null) { - return false; + boolean validate = checkNodeOrK8s(testResourceDTO, type, false); + if (!validate) { + testResourcePool.setEnable(false); + } + return validate; + } + + private static boolean checkNodeOrK8s(TestResourceDTO testResourceDTO, String type, Boolean usedApiType) { + if (StringUtils.equalsIgnoreCase(type,ResourcePoolTypeEnum.NODE.name())) { + NodeResourcePoolService resourcePoolService = CommonBeanFactory.getBean(NodeResourcePoolService.class); + if (resourcePoolService != null) { + return resourcePoolService.validate(testResourceDTO); + } else { + return false; + } + } else { + KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class); + if (resourcePoolService == null) { + return false; + } + return resourcePoolService.validate(testResourceDTO, usedApiType); } - return resourcePoolService.validate(testResourceDTO,type); } private boolean checkUiConfig(TestResourceDTO testResourceDTO, TestResourcePool testResourcePool) { @@ -124,19 +140,13 @@ public class TestResourcePoolService { private boolean checkApiConfig(TestResourceDTO testResourceDTO, TestResourcePool testResourcePool, String type) { if (testResourcePool.getApiTest() == null || !testResourcePool.getApiTest()) { - return false; + return true; } - - if (StringUtils.equalsIgnoreCase(type,ResourcePoolTypeEnum.NODE.name())) { - NodeResourcePoolService resourcePoolService = CommonBeanFactory.getBean(NodeResourcePoolService.class); - return resourcePoolService.validate(testResourceDTO); - } else { - KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class); - if (resourcePoolService == null) { - return false; - } - return resourcePoolService.validate(testResourceDTO); + boolean validate = checkNodeOrK8s(testResourceDTO, type, true); + if (!validate) { + testResourcePool.setEnable(false); } + return validate; } public void deleteTestResourcePool(String testResourcePoolId) { diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties index 2506725f43..083fd2b520 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties @@ -73,6 +73,12 @@ test_resource_pool_is_use=This resource pool is in use and cannot be deleted only_one_k8s=Only one K8S can be added test_resource_pool_not_exists=Test resource pool not exists test_resource_pool_invalid=Test resource pool invalid +selenium_grid_is_null=selenium_grid cannot be null +ip_is_null=ip address/domain name cannot be null +token_is_null = Token can not be null +namespace_is_null=Namespaces can not be null +deploy_name_is_null=Deploy Name cannot be null +api_test_image_is_null=API cannot be null #project project_name_is_null=Project name cannot be null project_name_already_exists=The project name already exists diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties index 03f258cc1f..bab6ff54ac 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties @@ -72,6 +72,14 @@ test_resource_pool_is_use=正在使用此资源池,无法删除 only_one_k8s=只能添加一个 K8S test_resource_pool_not_exists=测试资源池不存在 test_resource_pool_invalid=当前测试使用的资源池处于禁用状态 +selenium_grid_is_null=selenium_grid不能为空 +ip_is_null=ip 地址/域名不能为空 +token_is_null = Token 不能为空 +namespace_is_null=命名空间不能为空 +deploy_name_is_null=Deploy Name 不能为空 +api_test_image_is_null=API 镜像不能为空 + + #project project_name_is_null=项目名称不能为空 project_name_already_exists=项目名称已存在 diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties index ae4115cb2c..a4eb93b3cf 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties @@ -73,6 +73,12 @@ test_resource_pool_is_use=正在使用此資源池,無法刪除 only_one_k8s=只能添加一個 K8S test_resource_pool_not_exists=測試資源池不存在 test_resource_pool_invalid=當前測試使用的資源池處於禁用狀態 +selenium_grid_is_null=selenium_grid不能為空 +ip_is_null=ip 地址/域名不能為空 +token_is_null = Token 不能為空 +namespace_is_null=命名空間不能為空 +deploy_name_is_null=Deploy Name 不能為空 +api_test_image_is_null=API 鏡像不能為空 #project project_name_is_null=項目名稱不能為空 project_name_already_exists=項目名稱已存在