diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/CmdbAutoConfiguration.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/CmdbAutoConfiguration.java index ee5996b953..eff27898db 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/CmdbAutoConfiguration.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/CmdbAutoConfiguration.java @@ -25,8 +25,14 @@ package com.tencent.bk.job.common.cc.config; import com.tencent.bk.job.common.WatchableThreadPoolExecutor; +import com.tencent.bk.job.common.cc.mock.MockBizCmdbClient; +import com.tencent.bk.job.common.cc.mock.MockTenantSetCmdbClient; import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; import com.tencent.bk.job.common.cc.sdk.BizSetCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizSetCmdbClient; +import com.tencent.bk.job.common.cc.sdk.ITenantSetCmdbClient; +import com.tencent.bk.job.common.cc.sdk.TenantSetCmdbClient; import com.tencent.bk.job.common.esb.config.AppProperties; import com.tencent.bk.job.common.esb.config.BkApiAutoConfiguration; import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; @@ -89,14 +95,15 @@ public ThreadPoolExecutor cmdbLongTermThreadPoolExecutor(CmdbConfig cmdbConfig, @Bean @Primary - public BizCmdbClient bizCmdbClient(AppProperties appProperties, - BkApiGatewayProperties bkApiGatewayProperties, - CmdbConfig cmdbConfig, - ThreadPoolExecutor cmdbThreadPoolExecutor, - ThreadPoolExecutor cmdbLongTermThreadPoolExecutor, - MeterRegistry meterRegistry, - ObjectProvider flowControllerProvider, - TenantEnvService tenantEnvService) { + @ConditionalOnMockCmdbApiDisabled + public IBizCmdbClient bizCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + ThreadPoolExecutor cmdbThreadPoolExecutor, + ThreadPoolExecutor cmdbLongTermThreadPoolExecutor, + MeterRegistry meterRegistry, + ObjectProvider flowControllerProvider, + TenantEnvService tenantEnvService) { return new BizCmdbClient( appProperties, bkApiGatewayProperties, @@ -111,14 +118,15 @@ public BizCmdbClient bizCmdbClient(AppProperties appProperties, } @Bean("cnBizCmdbClient") - public BizCmdbClient cnBizCmdbClient(AppProperties appProperties, - BkApiGatewayProperties bkApiGatewayProperties, - CmdbConfig cmdbConfig, - ThreadPoolExecutor cmdbThreadPoolExecutor, - ThreadPoolExecutor cmdbLongTermThreadPoolExecutor, - MeterRegistry meterRegistry, - ObjectProvider flowControllerProvider, - TenantEnvService tenantEnvService) { + @ConditionalOnMockCmdbApiDisabled + public IBizCmdbClient cnBizCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + ThreadPoolExecutor cmdbThreadPoolExecutor, + ThreadPoolExecutor cmdbLongTermThreadPoolExecutor, + MeterRegistry meterRegistry, + ObjectProvider flowControllerProvider, + TenantEnvService tenantEnvService) { return new BizCmdbClient( appProperties, bkApiGatewayProperties, @@ -133,12 +141,12 @@ public BizCmdbClient cnBizCmdbClient(AppProperties appProperties, } @Bean - public BizSetCmdbClient bizSetCmdbClient(AppProperties appProperties, - BkApiGatewayProperties bkApiGatewayProperties, - CmdbConfig cmdbConfig, - MeterRegistry meterRegistry, - ObjectProvider flowControllerProvider, - TenantEnvService tenantEnvService) { + public IBizSetCmdbClient bizSetCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + MeterRegistry meterRegistry, + ObjectProvider flowControllerProvider, + TenantEnvService tenantEnvService) { return new BizSetCmdbClient( appProperties, bkApiGatewayProperties, @@ -149,4 +157,49 @@ public BizSetCmdbClient bizSetCmdbClient(AppProperties appProperties, ); } + @Bean + @ConditionalOnMockCmdbApiDisabled + public ITenantSetCmdbClient tenantSetCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + MeterRegistry meterRegistry, + ObjectProvider flowControllerProvider, + TenantEnvService tenantEnvService) { + return new TenantSetCmdbClient( + appProperties, + bkApiGatewayProperties, + cmdbConfig, + flowControllerProvider.getIfAvailable(), + meterRegistry, + tenantEnvService + ); + } + + @Bean + @ConditionalOnMockCmdbApiEnabled + public IBizCmdbClient mockedBizCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + ThreadPoolExecutor cmdbThreadPoolExecutor, + ThreadPoolExecutor cmdbLongTermThreadPoolExecutor, + MeterRegistry meterRegistry, + ObjectProvider flowControllerProvider, + TenantEnvService tenantEnvService) { + return new MockBizCmdbClient( + appProperties, + bkApiGatewayProperties, + cmdbConfig, + cmdbThreadPoolExecutor, + cmdbLongTermThreadPoolExecutor, + meterRegistry, + flowControllerProvider, + tenantEnvService + ); + } + + @Bean + @ConditionalOnMockCmdbApiEnabled + public ITenantSetCmdbClient mockedTenantSetCmdbClient() { + return new MockTenantSetCmdbClient(); + } } diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/ConditionalOnMockCmdbApiDisabled.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/ConditionalOnMockCmdbApiDisabled.java new file mode 100644 index 0000000000..d6d5760322 --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/ConditionalOnMockCmdbApiDisabled.java @@ -0,0 +1,47 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty( + value = "mockApi.cmdb.enabled", + havingValue = "false", + matchIfMissing = true +) +public @interface ConditionalOnMockCmdbApiDisabled { + +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/ConditionalOnMockCmdbApiEnabled.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/ConditionalOnMockCmdbApiEnabled.java new file mode 100644 index 0000000000..2c22b721b8 --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/config/ConditionalOnMockCmdbApiEnabled.java @@ -0,0 +1,46 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty( + value = "mockApi.cmdb.enabled", + havingValue = "true" +) +public @interface ConditionalOnMockCmdbApiEnabled { + +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/mock/MockBizCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/mock/MockBizCmdbClient.java new file mode 100644 index 0000000000..41b7041efc --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/mock/MockBizCmdbClient.java @@ -0,0 +1,129 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.mock; + +import com.tencent.bk.job.common.cc.config.CmdbConfig; +import com.tencent.bk.job.common.cc.model.CcInstanceDTO; +import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO; +import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; +import com.tencent.bk.job.common.esb.config.AppProperties; +import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; +import com.tencent.bk.job.common.esb.constants.EsbLang; +import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; +import com.tencent.bk.job.common.tenant.TenantEnvService; +import com.tencent.bk.job.common.util.FlowController; +import io.micrometer.core.instrument.MeterRegistry; +import lombok.experimental.Delegate; +import org.springframework.beans.factory.ObjectProvider; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ThreadPoolExecutor; + +public class MockBizCmdbClient implements IBizCmdbClient { + + @Delegate + private final IBizCmdbClient proxy; + + public MockBizCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + ThreadPoolExecutor cmdbThreadPoolExecutor, + ThreadPoolExecutor cmdbLongTermThreadPoolExecutor, + MeterRegistry meterRegistry, + ObjectProvider flowControllerProvider, + TenantEnvService tenantEnvService) { + this.proxy = new BizCmdbClient(appProperties, + bkApiGatewayProperties, + cmdbConfig, + EsbLang.EN, + cmdbThreadPoolExecutor, + cmdbLongTermThreadPoolExecutor, + flowControllerProvider.getIfAvailable(), + meterRegistry, + tenantEnvService + ); + } + + private InstanceTopologyDTO mockInstanceTopologyDTO() { + InstanceTopologyDTO bizNode = new InstanceTopologyDTO(); + bizNode.setObjectId("biz"); + bizNode.setInstanceId(1L); + bizNode.setInstanceName("蓝鲸"); + + List moduleList = new ArrayList<>(); + InstanceTopologyDTO idleMachineModule = new InstanceTopologyDTO(); + idleMachineModule.setObjectId("module"); + idleMachineModule.setInstanceId(2L); + idleMachineModule.setInstanceName("空闲机"); + moduleList.add(idleMachineModule); + InstanceTopologyDTO faultMachineModule = new InstanceTopologyDTO(); + faultMachineModule.setObjectId("module"); + faultMachineModule.setInstanceId(3L); + faultMachineModule.setInstanceName("故障机"); + moduleList.add(faultMachineModule); + InstanceTopologyDTO recycleMachineModule = new InstanceTopologyDTO(); + recycleMachineModule.setObjectId("module"); + recycleMachineModule.setInstanceId(4L); + recycleMachineModule.setInstanceName("待回收"); + moduleList.add(recycleMachineModule); + + List setList = new ArrayList<>(); + InstanceTopologyDTO idlePoolSet = new InstanceTopologyDTO(); + idlePoolSet.setObjectId("set"); + idlePoolSet.setInstanceId(2L); + idlePoolSet.setInstanceName("空闲机池"); + idlePoolSet.setChild(moduleList); + + setList.add(idlePoolSet); + idlePoolSet.setChild(setList); + return idlePoolSet; + } + + @Override + public InstanceTopologyDTO getBizInstTopology(long bizId) { + return mockInstanceTopologyDTO(); + } + + @Override + public InstanceTopologyDTO getBizInstCompleteTopology(String tenantId, long bizId) { + return mockInstanceTopologyDTO(); + } + + @Override + public List getHosts(long bizId, List ccInstList) { + ApplicationHostDTO host = new ApplicationHostDTO(); + host.setHostId(1L); + host.setBizId(1L); + host.setIp("127.0.0.1"); + host.setHostName("MockHost1"); + host.setGseAgentStatus(0); + host.setAgentId("MockAgentId1"); + host.setCloudAreaId(0L); + host.setCloudAreaName("MockCloudArea1"); + return null; + } +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/mock/MockTenantSetCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/mock/MockTenantSetCmdbClient.java new file mode 100644 index 0000000000..ba389370e8 --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/mock/MockTenantSetCmdbClient.java @@ -0,0 +1,54 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.mock; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.tencent.bk.job.common.cc.model.tenantset.TenantSetInfo; +import com.tencent.bk.job.common.cc.sdk.ITenantSetCmdbClient; +import com.tencent.bk.job.common.util.json.JsonUtils; + +import java.util.List; + +public class MockTenantSetCmdbClient implements ITenantSetCmdbClient { + @Override + public List listAllTenantSet() { + String respStr = "[\n" + + " {\n" + + " \"id\": 1,\n" + + " \"name\": \"All tenants\",\n" + + " \"maintainer\": \"\",\n" + + " \"description\": \"全租户\",\n" + + " \"default\": 1,\n" + + " \"bk_scope\": {\n" + + " \"match_all\": true\n" + + " },\n" + + " \"bk_created_at\": \"2021-09-06T08:10:50.168Z\",\n" + + " \"bk_updated_at\": \"2021-10-15T02:30:01.867Z\"\n" + + " }\n" + + " ]"; + return JsonUtils.fromJson(respStr, new TypeReference>() { + }); + } +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/BusinessInfoDTO.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/BusinessInfoDTO.java index 5b4424c2d9..662b8294ef 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/BusinessInfoDTO.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/BusinessInfoDTO.java @@ -65,5 +65,5 @@ public class BusinessInfoDTO { * 资源类型 */ @JsonProperty("default") - private Integer deFault; + private Integer deFault = 0; } diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizFilter.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizFilter.java index d7f8783dcf..7b3f30a2c3 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizFilter.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizFilter.java @@ -24,6 +24,7 @@ package com.tencent.bk.job.common.cc.model.bizset; +import com.tencent.bk.job.common.cc.model.filter.Rule; import lombok.Getter; import lombok.Setter; import lombok.ToString; diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetInfo.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetInfo.java index 88077b0dcd..62b195f8d7 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetInfo.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetInfo.java @@ -105,5 +105,5 @@ public class BizSetInfo { * 资源(业务/业务集/租户集)类型,1表示内置资源 */ @JsonProperty("default") - private Integer deFault; + private Integer deFault = 0; } diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetScope.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetScope.java index 82b19990fa..b3b86ca236 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetScope.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetScope.java @@ -25,6 +25,7 @@ package com.tencent.bk.job.common.cc.model.bizset; import com.fasterxml.jackson.annotation.JsonProperty; +import com.tencent.bk.job.common.cc.model.filter.CmdbFilter; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -46,6 +47,6 @@ public class BizSetScope { /** * 业务过滤器 */ - private BizSetFilter filter; + private CmdbFilter filter; } diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/SearchBizSetReq.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/SearchBizSetReq.java index d9b3cde48d..6eed7d203f 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/SearchBizSetReq.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/SearchBizSetReq.java @@ -25,6 +25,7 @@ package com.tencent.bk.job.common.cc.model.bizset; import com.fasterxml.jackson.annotation.JsonProperty; +import com.tencent.bk.job.common.cc.model.filter.CmdbFilter; import com.tencent.bk.job.common.esb.model.EsbReq; import lombok.Getter; import lombok.Setter; @@ -45,7 +46,7 @@ public class SearchBizSetReq extends EsbReq { * 业务集过滤器 */ @JsonProperty("bk_biz_set_filter") - private BizSetFilter filter; + private CmdbFilter filter; /** * 查询字段 diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetFilter.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/filter/CmdbFilter.java similarity index 91% rename from src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetFilter.java rename to src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/filter/CmdbFilter.java index e7ee722d7a..439b546b2b 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/BizSetFilter.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/filter/CmdbFilter.java @@ -22,7 +22,7 @@ * IN THE SOFTWARE. */ -package com.tencent.bk.job.common.cc.model.bizset; +package com.tencent.bk.job.common.cc.model.filter; import lombok.Getter; import lombok.Setter; @@ -31,12 +31,12 @@ import java.util.List; /** - * CMDB接口请求实体类,定义业务集通过组合规则选择业务的过滤器 + * CMDB接口请求实体类,定义通过组合规则选择各种CMDB资源的过滤器 */ @Setter @Getter @ToString -public class BizSetFilter { +public class CmdbFilter { public static final String CONDITION_AND = "AND"; public static final String CONDITION_OR = "OR"; diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/Rule.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/filter/Rule.java similarity index 97% rename from src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/Rule.java rename to src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/filter/Rule.java index 5b3b4e9be8..e3cba733f2 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/bizset/Rule.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/filter/Rule.java @@ -22,7 +22,7 @@ * IN THE SOFTWARE. */ -package com.tencent.bk.job.common.cc.model.bizset; +package com.tencent.bk.job.common.cc.model.filter; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/response/CcCountInfo.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/response/CountInfo.java similarity index 92% rename from src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/response/CcCountInfo.java rename to src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/response/CountInfo.java index 16f90c2590..05078cb47f 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/response/CcCountInfo.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/response/CountInfo.java @@ -31,7 +31,6 @@ import lombok.ToString; import java.util.List; -import java.util.Map; /** * CMDB 返回的通用数据结构 @@ -41,14 +40,14 @@ @ToString @AllArgsConstructor @NoArgsConstructor -public class CcCountInfo { +public class CountInfo { /** * info count */ private Integer count; /** - * 查询返回的数据列表,Map为属性Map + * 查询返回的数据列表 */ - private List> info; + private List info; } diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizEventDetail.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizEventDetail.java index 010196ec80..f38ef8005c 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizEventDetail.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizEventDetail.java @@ -50,7 +50,7 @@ public class BizEventDetail { @JsonProperty("language") private String language; @JsonProperty("default") - private Integer deFault; + private Integer deFault = 0; public static ApplicationDTO toAppInfoDTO(BizEventDetail bizEventDetail) { ApplicationDTO applicationDTO = new ApplicationDTO(); diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizSetEventDetail.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizSetEventDetail.java index c5cf74971e..c082eb7d1a 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizSetEventDetail.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/result/BizSetEventDetail.java @@ -76,7 +76,7 @@ public class BizSetEventDetail { * 资源类型 */ @JsonProperty("default") - private Integer deFault; + private Integer deFault = 0; public ApplicationDTO toApplicationDTO() { ApplicationDTO applicationDTO = new ApplicationDTO(); diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/ListTenantSetReq.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/ListTenantSetReq.java new file mode 100644 index 0000000000..20cdfe6124 --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/ListTenantSetReq.java @@ -0,0 +1,71 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.model.tenantset; + +import com.tencent.bk.job.common.cc.model.filter.CmdbFilter; +import com.tencent.bk.job.common.cc.model.bizset.Page; +import com.tencent.bk.job.common.esb.model.EsbReq; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.util.Arrays; +import java.util.List; + +/** + * CMDB接口请求实体类,用于查询租户集 + */ +@Setter +@Getter +@ToString +public class ListTenantSetReq extends EsbReq { + + /** + * 过滤器 + */ + private CmdbFilter filter; + + /** + * 查询字段 + */ + private List fields = Arrays.asList( + "id", + "name", + "maintainer", + "description", + "default", + "bk_scope", + "bk_created_at", + "bk_created_by", + "bk_updated_at", + "bk_updated_by" + ); + + /** + * 分页参数 + */ + private Page page; + +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/TenantSetInfo.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/TenantSetInfo.java new file mode 100644 index 0000000000..73849b69cd --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/TenantSetInfo.java @@ -0,0 +1,96 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.model.tenantset; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * CMDB接口响应实体类,定义租户集字段 + */ +@Setter +@Getter +@ToString +public class TenantSetInfo { + + /** + * ID + */ + private Long id; + + /** + * 名称 + */ + private String name; + + /** + * 运维人员 + */ + private String maintainer; + + /** + * 描述 + */ + private String description; + + /** + * 资源(业务/业务集/租户集)类型,1表示内置资源 + */ + @JsonProperty("default") + private Integer deFault = 0; + + /** + * 租户范围 + */ + @JsonProperty("bk_scope") + private TenantSetScope scope; + + /** + * 创建时间 + */ + @JsonProperty("bk_created_at") + private String createTime; + + /** + * 创建人 + */ + @JsonProperty("bk_created_by") + private String createUser; + + /** + * 最后修改时间 + */ + @JsonProperty("bk_updated_at") + private String updateTime; + + /** + * 更新人 + */ + @JsonProperty("bk_updated_by") + private String updateUser; + +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/TenantSetScope.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/TenantSetScope.java new file mode 100644 index 0000000000..166b9d0d19 --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/model/tenantset/TenantSetScope.java @@ -0,0 +1,46 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.model.tenantset; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * CMDB接口请求实体类,定义租户集选择租户的范围 + */ +@Setter +@Getter +@ToString +public class TenantSetScope { + + /** + * 是否匹配全部租户 + */ + @JsonProperty("match_all") + private boolean matchAll; + +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BaseCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BaseCmdbClient.java index a8173462bc..7f6105188b 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BaseCmdbClient.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BaseCmdbClient.java @@ -91,6 +91,9 @@ public class BaseCmdbClient extends BkApiV1Client { protected static final String SEARCH_BUSINESS_SET = "/api/v3/findmany/biz_set"; protected static final String SEARCH_BIZ_IN_BUSINESS_SET = "/api/v3/find/biz_set/biz_list"; + // 租户集相关 API + protected static final String LIST_TENANT_SET = "/api/v3/findmany/tenant_set"; + private static final Map interfaceNameMap = new HashMap<>(); protected final String cmdbSupplierAccount; diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizCmdbClient.java index 7c455ea946..512fd54d24 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizCmdbClient.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizCmdbClient.java @@ -44,7 +44,7 @@ import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO; import com.tencent.bk.job.common.cc.model.TopoNodePathDTO; import com.tencent.bk.job.common.cc.model.bizset.BizFilter; -import com.tencent.bk.job.common.cc.model.bizset.Rule; +import com.tencent.bk.job.common.cc.model.filter.Rule; import com.tencent.bk.job.common.cc.model.container.ContainerDTO; import com.tencent.bk.job.common.cc.model.container.ContainerDetailDTO; import com.tencent.bk.job.common.cc.model.container.KubeClusterDTO; @@ -84,7 +84,7 @@ import com.tencent.bk.job.common.cc.model.req.ResourceWatchReq; import com.tencent.bk.job.common.cc.model.req.SearchHostDynamicGroupReq; import com.tencent.bk.job.common.cc.model.req.input.GetHostByIpInput; -import com.tencent.bk.job.common.cc.model.response.CcCountInfo; +import com.tencent.bk.job.common.cc.model.response.CountInfo; import com.tencent.bk.job.common.cc.model.result.BaseCcSearchResult; import com.tencent.bk.job.common.cc.model.result.BizEventDetail; import com.tencent.bk.job.common.cc.model.result.ExecuteDynamicGroupHostResult; @@ -1037,19 +1037,6 @@ public List getHostByIp(GetHostByIpInput input) { return hostInfoList; } - @Override - public ApplicationHostDTO getHostByIp(Long cloudAreaId, String ip) { - ListHostsWithoutBizReq req = makeCmdbBaseReq(ListHostsWithoutBizReq.class); - PropertyFilterDTO condition = new PropertyFilterDTO(); - condition.setCondition(RuleConditionEnum.AND.getCondition()); - condition.addRule(BaseRuleDTO.equals("bk_host_innerip", ip)); - condition.addRule(BaseRuleDTO.equals("bk_cloud_id", cloudAreaId)); - req.setCondition(condition); - - List hosts = listHostsWithoutBiz(req); - return hosts.size() > 0 ? hosts.get(0) : null; - } - @Override public List listHostsByCloudIps(List cloudIps) { if (CollectionUtils.isEmpty(cloudIps)) { @@ -1202,19 +1189,19 @@ public List getObjAttributeList(String objId) { @Override public Set listUsersByRole(Long bizId, String role) { - CcCountInfo searchResult; + CountInfo> searchResult; GetAppReq req = makeCmdbBaseReq(GetAppReq.class); Map condition = new HashMap<>(); condition.put("bk_biz_id", bizId); req.setCondition(condition); req.setFields(Collections.singletonList(role)); String uri = SEARCH_BUSINESS.replace("{bk_supplier_account}", req.getBkSupplierAccount()); - EsbResp esbResp = requestCmdbApiUseContextTenantId( + EsbResp>> esbResp = requestCmdbApiUseContextTenantId( HttpMethodEnum.POST, uri, null, req, - new TypeReference>() { + new TypeReference>>>() { }); searchResult = esbResp.getData(); @@ -1456,6 +1443,7 @@ public void run() { * @param bizId 业务 ID * @return 容器拓扑 */ + @Override public KubeTopologyDTO getBizKubeCacheTopo(long bizId) { GetBizKubeCacheTopoReq req = makeCmdbBaseReq(GetBizKubeCacheTopoReq.class); req.setBizId(bizId); @@ -1476,6 +1464,7 @@ public KubeTopologyDTO getBizKubeCacheTopo(long bizId) { * @param req 请求 * @return 容器列表(分页) */ + @Override public PageData listPageKubeContainerByTopo(ListKubeContainerByTopoReq req) { return listPageKubeContainerByTopo(req, true); } @@ -1505,6 +1494,7 @@ private PageData listPageKubeContainerByTopo(ListKubeContain * @param req 请求 * @return 容器列表 */ + @Override public List listKubeContainerByTopo(ListKubeContainerByTopoReq req) { setSupplierAccount(req); req.setContainerFields(ContainerDTO.Fields.ALL); @@ -1597,6 +1587,7 @@ private void setSupplierAccount(EsbReq esbReq) { * @param containerIds 容器 ID 集合 * @return 容器列表 */ + @Override public List listKubeContainerByIds(long bizId, Collection containerIds) { return listBizKubeContainerByContainerFieldWithInCondition( bizId, ContainerDTO.Fields.ID, containerIds); @@ -1609,6 +1600,7 @@ public List listKubeContainerByIds(long bizId, Collection listKubeContainerByUIds(long bizId, Collection containerUIds) { return listBizKubeContainerByContainerFieldWithInCondition( bizId, ContainerDTO.Fields.CONTAINER_UID, containerUIds); @@ -1651,6 +1643,7 @@ private List listBizKubeContainerByContainerFieldWithInC return containers; } + @Override public List listKubeClusters(KubeClusterQuery query) { ListKubeClusterReq req = makeCmdbBaseReq(ListKubeClusterReq.class); @@ -1736,6 +1729,7 @@ private PageData listPage(CmdbPageReq req, return new PageData<>(originPage.getStart(), originPage.getLimit(), count, response.getData().getInfo()); } + @Override public List listKubeNamespaces(NamespaceQuery query) { ListKubeNamespaceReq req = makeCmdbBaseReq(ListKubeNamespaceReq.class); @@ -1802,6 +1796,7 @@ private ListKubeNamespaceReq buildNextPageListKubeNamespaceReq(ListKubeNamespace return nextPageReq; } + @Override public List listKubeWorkloads(WorkloadQuery query) { ListKubeWorkloadReq req = makeCmdbBaseReq(ListKubeWorkloadReq.class); diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizSetCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizSetCmdbClient.java index d1f7f1425b..47d412b71c 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizSetCmdbClient.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/BizSetCmdbClient.java @@ -27,11 +27,11 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.tencent.bk.job.common.cc.config.CmdbConfig; import com.tencent.bk.job.common.cc.model.bizset.BizInfo; -import com.tencent.bk.job.common.cc.model.bizset.BizSetFilter; +import com.tencent.bk.job.common.cc.model.filter.CmdbFilter; import com.tencent.bk.job.common.cc.model.bizset.BizSetInfo; import com.tencent.bk.job.common.cc.model.bizset.BizSetScope; import com.tencent.bk.job.common.cc.model.bizset.Page; -import com.tencent.bk.job.common.cc.model.bizset.Rule; +import com.tencent.bk.job.common.cc.model.filter.Rule; import com.tencent.bk.job.common.cc.model.bizset.SearchBizInBusinessReq; import com.tencent.bk.job.common.cc.model.bizset.SearchBizInBusinessSetResp; import com.tencent.bk.job.common.cc.model.bizset.SearchBizSetReq; @@ -146,8 +146,8 @@ public List searchAllBizSet(String tenantId) { * @return 业务集信息列表 */ public List listBizSetByIds(String tenantId, List bizSetIds) { - BizSetFilter filter = new BizSetFilter(); - filter.setCondition(BizSetFilter.CONDITION_AND); + CmdbFilter filter = new CmdbFilter(); + filter.setCondition(CmdbFilter.CONDITION_AND); Rule bizSetIdRule = new Rule(); bizSetIdRule.setField("bk_biz_set_id"); bizSetIdRule.setOperator(RuleOperatorEnum.IN.getOperator()); @@ -179,7 +179,7 @@ public List listBizSetByIds(String tenantId, List bizSetIds) { * @param limit 每页大小 * @return 业务集信息列表 */ - private List searchBizSet(String tenantId, BizSetFilter filter, int start, int limit) { + private List searchBizSet(String tenantId, CmdbFilter filter, int start, int limit) { SearchBizSetReq req = makeCmdbBaseReq(SearchBizSetReq.class); Page page = new Page(); page.setEnableCount(false); @@ -380,7 +380,7 @@ public Set listUsersByRole(String tenantId, Long bizSetId, String role) @Override public BizSetInfo queryBizSet(String tenantId, Long bizSetId) { - BizSetFilter filter = new BizSetFilter(); + CmdbFilter filter = new CmdbFilter(); filter.setCondition(RuleConditionEnum.AND.getCondition()); Rule bizSetIdRule = new Rule(); bizSetIdRule.setField("bk_biz_set_id"); diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/IBizCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/IBizCmdbClient.java index 9177c72fd2..058ac0f696 100644 --- a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/IBizCmdbClient.java +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/IBizCmdbClient.java @@ -31,7 +31,16 @@ import com.tencent.bk.job.common.cc.model.CcObjAttributeDTO; import com.tencent.bk.job.common.cc.model.DynamicGroupHostPropDTO; import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO; +import com.tencent.bk.job.common.cc.model.container.ContainerDetailDTO; +import com.tencent.bk.job.common.cc.model.container.KubeClusterDTO; +import com.tencent.bk.job.common.cc.model.container.KubeNamespaceDTO; +import com.tencent.bk.job.common.cc.model.container.KubeTopologyDTO; +import com.tencent.bk.job.common.cc.model.container.KubeWorkloadDTO; +import com.tencent.bk.job.common.cc.model.query.KubeClusterQuery; +import com.tencent.bk.job.common.cc.model.query.NamespaceQuery; +import com.tencent.bk.job.common.cc.model.query.WorkloadQuery; import com.tencent.bk.job.common.cc.model.req.GetTopoNodePathReq; +import com.tencent.bk.job.common.cc.model.req.ListKubeContainerByTopoReq; import com.tencent.bk.job.common.cc.model.req.input.GetHostByIpInput; import com.tencent.bk.job.common.cc.model.result.BizEventDetail; import com.tencent.bk.job.common.cc.model.result.HostBizRelationDTO; @@ -39,6 +48,7 @@ import com.tencent.bk.job.common.cc.model.result.HostRelationEventDetail; import com.tencent.bk.job.common.cc.model.result.HostWithModules; import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; +import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.common.model.dto.HostDTO; @@ -154,15 +164,6 @@ public interface IBizCmdbClient { */ List getHostByIp(GetHostByIpInput input); - /** - * 根据云区域ID+IP获取主机信息 - * - * @param cloudAreaId 云区域ID - * @param ip IP - * @return 主机信息 - */ - ApplicationHostDTO getHostByIp(Long cloudAreaId, String ip); - /** * 根据IP批量获取主机 * @@ -251,4 +252,20 @@ public interface IBizCmdbClient { * 监听CMDB业务事件 */ ResourceWatchResult getAppEvents(Long startTime, String cursor); + + List listKubeContainerByIds(long bizId, Collection containerIds); + + List listKubeContainerByTopo(ListKubeContainerByTopoReq req); + + List listKubeClusters(KubeClusterQuery query); + + List listKubeNamespaces(NamespaceQuery query); + + List listKubeWorkloads(WorkloadQuery query); + + KubeTopologyDTO getBizKubeCacheTopo(long bizId); + + PageData listPageKubeContainerByTopo(ListKubeContainerByTopoReq req); + + List listKubeContainerByUIds(long bizId, Collection containerUIds); } diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/ITenantSetCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/ITenantSetCmdbClient.java new file mode 100644 index 0000000000..f62393717d --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/ITenantSetCmdbClient.java @@ -0,0 +1,43 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.sdk; + +import com.tencent.bk.job.common.cc.model.tenantset.TenantSetInfo; + +import java.util.List; + +/** + * CMDB租户集相关接口 + */ +public interface ITenantSetCmdbClient { + + /** + * 从CMDB获取所有租户集信息 + * + * @return 所有租户集列表 + */ + List listAllTenantSet(); + +} diff --git a/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/TenantSetCmdbClient.java b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/TenantSetCmdbClient.java new file mode 100644 index 0000000000..d8cc057ee2 --- /dev/null +++ b/src/backend/commons/cmdb-sdk/src/main/java/com/tencent/bk/job/common/cc/sdk/TenantSetCmdbClient.java @@ -0,0 +1,158 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.cc.sdk; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.tencent.bk.job.common.cc.config.CmdbConfig; +import com.tencent.bk.job.common.cc.model.bizset.Page; +import com.tencent.bk.job.common.cc.model.filter.CmdbFilter; +import com.tencent.bk.job.common.cc.model.response.CountInfo; +import com.tencent.bk.job.common.cc.model.tenantset.ListTenantSetReq; +import com.tencent.bk.job.common.cc.model.tenantset.TenantSetInfo; +import com.tencent.bk.job.common.constant.ErrorCode; +import com.tencent.bk.job.common.constant.HttpMethodEnum; +import com.tencent.bk.job.common.constant.TenantIdConstants; +import com.tencent.bk.job.common.esb.config.AppProperties; +import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; +import com.tencent.bk.job.common.esb.model.EsbReq; +import com.tencent.bk.job.common.esb.model.EsbResp; +import com.tencent.bk.job.common.exception.InternalCmdbException; +import com.tencent.bk.job.common.tenant.TenantEnvService; +import com.tencent.bk.job.common.util.FlowController; +import io.micrometer.core.instrument.MeterRegistry; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.List; + +/** + * cmdb API Client - 租户集相关 + */ +@Slf4j +public class TenantSetCmdbClient extends BaseCmdbClient implements ITenantSetCmdbClient { + + + public TenantSetCmdbClient(AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties, + CmdbConfig cmdbConfig, + FlowController flowController, + MeterRegistry meterRegistry, + TenantEnvService tenantEnvService) { + super( + flowController, + appProperties, + bkApiGatewayProperties, + cmdbConfig, + meterRegistry, + tenantEnvService, + null + ); + } + + /** + * 查询租户集数量 + * + * @return 租户集数量 + */ + public int listTenantSetCount() { + ListTenantSetReq req = EsbReq.buildRequest(ListTenantSetReq.class, cmdbSupplierAccount); + Page page = new Page(); + page.setStart(0); + page.setLimit(0); + page.setEnableCount(true); + req.setPage(page); + req.setFilter(null); + try { + EsbResp> resp = requestCmdbApi( + TenantIdConstants.SYSTEM_TENANT_ID, + HttpMethodEnum.POST, + LIST_TENANT_SET, + null, + req, + new TypeReference>>() { + }); + if (!resp.getResult()) { + throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null); + } + return resp.getData().getCount(); + } catch (Exception e) { + throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null); + } + } + + /** + * 查询全部租户集信息 + * + * @return 租户集信息列表 + */ + @Override + public List listAllTenantSet() { + int tenantSetCount = listTenantSetCount(); + log.info("{} tenantSet found in cmdb", tenantSetCount); + // 分批查询 + int limit = 500; + int start = 0; + List tenantSetInfoList = new ArrayList<>(); + while (start < tenantSetCount) { + tenantSetInfoList.addAll(listTenantSet(null, start, limit)); + start += limit; + } + return tenantSetInfoList; + } + + /** + * 查询租户集信息 + * + * @param filter 查询条件 + * @param start 分页起始 + * @param limit 每页大小 + * @return 租户集信息列表 + */ + private List listTenantSet(CmdbFilter filter, int start, int limit) { + ListTenantSetReq req = makeCmdbBaseReq(ListTenantSetReq.class); + Page page = new Page(); + page.setEnableCount(false); + page.setStart(start); + page.setLimit(limit); + req.setPage(page); + req.setFilter(filter); + try { + EsbResp> resp = requestCmdbApi( + TenantIdConstants.SYSTEM_TENANT_ID, + HttpMethodEnum.POST, + LIST_TENANT_SET, + null, + req, + new TypeReference>>() { + }); + if (!resp.getResult() || resp.getData() == null) { + throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null); + } + return resp.getData().getInfo(); + } catch (Exception e) { + throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null); + } + } +} diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties index a572087b79..08cfe27930 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties @@ -58,6 +58,9 @@ 1210101=License 不可用 1210001=GSE 不可用 1210002=GSE 数据异常:{0} +1210003=初始化租户失败:{0} +1210004=已有另一个初始化租户任务正在执行:{0} + 1211001=CMDB 服务不可用 1211002=CMDB API返回数据异常 1211003=根据动态分组 ID 查找主机失败,动态分组 ID :{0},原因:{1},请确认指定的动态分组在业务下是否存在 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties index c647973f31..40edd4524c 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties @@ -58,6 +58,9 @@ 1210101=License is not available 1210001=GSE is not available 1210002=GSE data unexpected:{0} +1210003=Init tenant error: {0} +1210004=Another initTenantTask is running: {0} + 1211001=CMDB service is unavailable 1211002=CMDB API returned data exception 1211003=Fail to find host by dynamic group, id:{0}, reason:{1}, please confirm the specified dynamic group in business diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties index 251adb49e9..c522200c5e 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties @@ -58,6 +58,9 @@ 1210101=License is not available 1210001=GSE is not available 1210002=GSE data unexpected:{0} +1210003=Init tenant error: {0} +1210004=Another initTenantTask is running: {0} + 1211001=CMDB service is unavailable 1211002=CMDB API returned data exception 1211003=Fail to find host by dynamic group, id:{0}, reason:{1}, please confirm the specified dynamic group in business diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties index a572087b79..08cfe27930 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties @@ -58,6 +58,9 @@ 1210101=License 不可用 1210001=GSE 不可用 1210002=GSE 数据异常:{0} +1210003=初始化租户失败:{0} +1210004=已有另一个初始化租户任务正在执行:{0} + 1211001=CMDB 服务不可用 1211002=CMDB API返回数据异常 1211003=根据动态分组 ID 查找主机失败,动态分组 ID :{0},原因:{1},请确认指定的动态分组在业务下是否存在 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties index 2846c9ddd1..0e856af7f0 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties @@ -58,6 +58,9 @@ 1210101=License 不可用 1210001=GSE 不可用 1210002=GSE 数据异常:{0} +1210003=初始化租户失败:{0} +1210004=已有另一个初始化租户任务正在执行:{0} + 1211001=CMDB 服务不可用 1211002=CMDB API返回数据异常 1211003=根据动态分组 ID 查找主机失败,动态分组 ID :{0},原因:{1},请确认指定的动态分组在业务下是否存在 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties index 25cc76df9b..e15b077c38 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties @@ -132,3 +132,6 @@ validation.constraints.WinInterpreterInvalidSuffix.message=解释器路径不合 ## MySQL validation.constraints.NotExceedMySQLFieldLength.message=字段过长 + +## 多租户 +validation.constraints.TenantIdCannotBeBlank.message=租户ID不能为空 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties index c0234f1354..acbd37ea6f 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties @@ -132,3 +132,6 @@ validation.constraints.WinInterpreterInvalidSuffix.message=Interpreter path is i ## MySQL validation.constraints.NotExceedMySQLFieldLength.message=Field content too long + +## 多租户 +validation.constraints.TenantIdCannotBeBlank.message=tenantId cannot be blank diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties index c0234f1354..acbd37ea6f 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties @@ -132,3 +132,6 @@ validation.constraints.WinInterpreterInvalidSuffix.message=Interpreter path is i ## MySQL validation.constraints.NotExceedMySQLFieldLength.message=Field content too long + +## 多租户 +validation.constraints.TenantIdCannotBeBlank.message=tenantId cannot be blank diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties index 6c7beb4b46..cf444fb9d1 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties @@ -133,3 +133,6 @@ validation.constraints.WinInterpreterInvalidSuffix.message=解释器路径不合 ## MySQL validation.constraints.NotExceedMySQLFieldLength.message=字段过长 + +## 多租户 +validation.constraints.TenantIdCannotBeBlank.message=租户ID不能为空 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties index 6c7beb4b46..cf444fb9d1 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties @@ -133,3 +133,6 @@ validation.constraints.WinInterpreterInvalidSuffix.message=解释器路径不合 ## MySQL validation.constraints.NotExceedMySQLFieldLength.message=字段过长 + +## 多租户 +validation.constraints.TenantIdCannotBeBlank.message=租户ID不能为空 diff --git a/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/config/IamAutoConfiguration.java b/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/config/IamAutoConfiguration.java index 5bd59b41d1..8eb6da6427 100644 --- a/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/config/IamAutoConfiguration.java +++ b/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/config/IamAutoConfiguration.java @@ -31,7 +31,7 @@ import com.tencent.bk.job.common.iam.client.ApiGwIamClient; import com.tencent.bk.job.common.iam.client.EsbIamClient; import com.tencent.bk.job.common.iam.client.IIamClient; -import com.tencent.bk.job.common.iam.client.MockIamClient; +import com.tencent.bk.job.common.iam.mock.MockIamClient; import com.tencent.bk.job.common.iam.http.IamHttpClientServiceImpl; import com.tencent.bk.job.common.iam.mock.MockBusinessAuthHelper; import com.tencent.bk.job.common.iam.service.AppAuthService; diff --git a/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/MockIamClient.java b/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/mock/MockIamClient.java similarity index 96% rename from src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/MockIamClient.java rename to src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/mock/MockIamClient.java index 0818c37192..9934390ac2 100644 --- a/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/MockIamClient.java +++ b/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/mock/MockIamClient.java @@ -22,8 +22,9 @@ * IN THE SOFTWARE. */ -package com.tencent.bk.job.common.iam.client; +package com.tencent.bk.job.common.iam.mock; +import com.tencent.bk.job.common.iam.client.IIamClient; import com.tencent.bk.job.common.iam.dto.EsbIamAction; import com.tencent.bk.job.common.iam.dto.EsbIamAuthedPolicy; import com.tencent.bk.job.common.iam.dto.EsbIamBatchAuthedPolicy; diff --git a/src/backend/commons/common-web/src/main/java/com/tencent/bk/job/common/web/config/OpSecurityConfig.java b/src/backend/commons/common-web/src/main/java/com/tencent/bk/job/common/web/config/OpSecurityConfig.java new file mode 100644 index 0000000000..c32dbf97bd --- /dev/null +++ b/src/backend/commons/common-web/src/main/java/com/tencent/bk/job/common/web/config/OpSecurityConfig.java @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.web.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * OP接口安全配置 + */ +@Order(200) +@Configuration +@EnableWebSecurity +public class OpSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable(); + http.authorizeRequests() + .antMatchers("/op/**").authenticated() + .anyRequest().permitAll() + .and() + .httpBasic(); + } +} diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java index dbbfc6b3de..40639b80c5 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java @@ -47,6 +47,10 @@ public class ErrorCode { public static final int GSE_ERROR = 1210001; // GSE数据异常:{0} public static final int GSE_API_DATA_ERROR = 1210002; + // 初始化租户失败:{0} + public static final int INIT_TENANT_ERROR = 1210003; + // 已有另一个初始化租户任务正在执行:{0} + public static final int INIT_TENANT_TASK_ALREADY_RUNNING = 1210004; // CMDB错误 // CMDB服务状态不可达 - 地址配置错误或者地址无法正确解析 diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ResourceScopeTypeEnum.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ResourceScopeTypeEnum.java index f3b49fb731..fc55a7d690 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ResourceScopeTypeEnum.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ResourceScopeTypeEnum.java @@ -38,7 +38,11 @@ public enum ResourceScopeTypeEnum { /** * CMDB业务集 */ - BIZ_SET("biz_set"); + BIZ_SET("biz_set"), + /** + * CMDB租户集 + */ + TENANT_SET("tenant_set"); /** * 资源范围类型 diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/ApplicationAttrsDO.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/ApplicationAttrsDO.java index d42ec57da7..5f799a9cde 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/ApplicationAttrsDO.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/ApplicationAttrsDO.java @@ -47,4 +47,9 @@ public class ApplicationAttrsDO { * cmdb业务集是否包含所有子业务 */ private Boolean matchAllBiz; + + /** + * cmdb租户集是否包含所有租户 + */ + private Boolean matchAllTenant; } diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/auth/impl/CMDBTopoPathService.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/auth/impl/CMDBTopoPathService.java index 89c692b91c..b1a9eb8433 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/auth/impl/CMDBTopoPathService.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/auth/impl/CMDBTopoPathService.java @@ -26,7 +26,7 @@ import com.google.common.collect.Lists; import com.tencent.bk.job.common.cc.model.result.HostBizRelationDTO; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.iam.constant.ResourceTypeId; import com.tencent.bk.job.common.util.ConcurrencyUtil; import com.tencent.bk.sdk.iam.service.TopoPathService; @@ -52,11 +52,11 @@ @Service("cmdbTopoPathService") public class CMDBTopoPathService implements TopoPathService { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; private final ExecutorService executorService; @Autowired - public CMDBTopoPathService(BizCmdbClient bizCmdbClient, + public CMDBTopoPathService(IBizCmdbClient bizCmdbClient, @Qualifier("getHostTopoPathExecutor") ExecutorService executorService) { this.bizCmdbClient = bizCmdbClient; diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/ContainerServiceImpl.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/ContainerServiceImpl.java index 69c2da77b5..e2558ad45e 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/ContainerServiceImpl.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/ContainerServiceImpl.java @@ -40,7 +40,7 @@ import com.tencent.bk.job.common.cc.model.query.NamespaceQuery; import com.tencent.bk.job.common.cc.model.query.WorkloadQuery; import com.tencent.bk.job.common.cc.model.req.ListKubeContainerByTopoReq; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.constant.LabelSelectorOperatorEnum; import com.tencent.bk.job.common.model.dto.Container; import com.tencent.bk.job.common.model.dto.HostDTO; @@ -72,11 +72,12 @@ @Service public class ContainerServiceImpl implements ContainerService { private final HostService hostService; - private final BizCmdbClient cmdbClient; + private final IBizCmdbClient cmdbClient; private final AppScopeMappingService appScopeMappingService; @Autowired - public ContainerServiceImpl(HostService hostService, BizCmdbClient cmdbClient, + public ContainerServiceImpl(HostService hostService, + IBizCmdbClient cmdbClient, AppScopeMappingService appScopeMappingService) { this.hostService = hostService; this.cmdbClient = cmdbClient; diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskInstanceExecuteObjectProcessor.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskInstanceExecuteObjectProcessor.java index 38024edb30..de019bd6fa 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskInstanceExecuteObjectProcessor.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskInstanceExecuteObjectProcessor.java @@ -28,7 +28,7 @@ import com.tencent.bk.job.common.cc.model.container.KubeNamespaceDTO; import com.tencent.bk.job.common.cc.model.query.KubeClusterQuery; import com.tencent.bk.job.common.cc.model.query.NamespaceQuery; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.constant.ErrorCode; import com.tencent.bk.job.common.constant.TaskVariableTypeEnum; import com.tencent.bk.job.common.exception.FailedPreconditionException; @@ -40,7 +40,6 @@ import com.tencent.bk.job.common.gse.util.AgentUtils; import com.tencent.bk.job.common.gse.v2.model.resp.AgentState; import com.tencent.bk.job.common.metrics.CommonMetricTags; -import com.tencent.bk.job.common.model.HostCompositeKey; import com.tencent.bk.job.common.model.dto.Container; import com.tencent.bk.job.common.model.dto.HostDTO; import com.tencent.bk.job.common.model.dto.ResourceScope; @@ -108,7 +107,7 @@ public class TaskInstanceExecuteObjectProcessor { private final MeterRegistry meterRegistry; - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; public TaskInstanceExecuteObjectProcessor(HostService hostService, ApplicationService applicationService, @@ -117,7 +116,8 @@ public TaskInstanceExecuteObjectProcessor(HostService hostService, WhiteHostCache whiteHostCache, @Qualifier(DefaultBeanNames.PREFER_V2_AGENT_STATE_CLIENT) AgentStateClient preferV2AgentStateClient, - MeterRegistry meterRegistry, BizCmdbClient bizCmdbClient) { + MeterRegistry meterRegistry, + IBizCmdbClient bizCmdbClient) { this.hostService = hostService; this.applicationService = applicationService; this.containerService = containerService; diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TopoServiceImpl.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TopoServiceImpl.java index 0df3700362..597ac65e03 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TopoServiceImpl.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TopoServiceImpl.java @@ -27,7 +27,7 @@ import com.google.common.collect.Lists; import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO; import com.tencent.bk.job.common.cc.model.req.GetTopoNodePathReq; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.util.CustomCollectionUtils; import com.tencent.bk.job.execute.model.DynamicServerTopoNodeDTO; import com.tencent.bk.job.execute.service.TopoService; @@ -43,10 +43,10 @@ @Slf4j public class TopoServiceImpl implements TopoService { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired - public TopoServiceImpl(BizCmdbClient bizCmdbClient) { + public TopoServiceImpl(IBizCmdbClient bizCmdbClient) { this.bizCmdbClient = bizCmdbClient; } diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java index c9cc2456c7..a31925de96 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java @@ -27,16 +27,11 @@ import com.tencent.bk.job.common.annotation.InternalAPI; import com.tencent.bk.job.common.model.InternalResponse; import com.tencent.bk.job.manage.model.inner.ServiceScriptDTO; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tentent.bk.job.common.api.feign.annotation.SmartFeignClient; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import org.apache.commons.lang3.tuple.Pair; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; @Api(tags = {"job-manage:service:Script_Management"}) @@ -46,31 +41,16 @@ public interface ServiceScriptResource { @ApiOperation(value = "根据业务ID、脚本版本ID获取脚本", produces = "application/json") @GetMapping("/service/script/app/{appId}/scriptVersion/{scriptVersionId}") InternalResponse getScriptByAppIdAndScriptVersionId(@RequestHeader("username") - String username, - @PathVariable("appId") - Long appId, - @PathVariable("scriptVersionId") - Long scriptVersionId); + String username, + @PathVariable("appId") + Long appId, + @PathVariable("scriptVersionId") + Long scriptVersionId); @ApiOperation(value = "根据脚本版本ID获取脚本", produces = "application/json") @GetMapping("/service/script/scriptVersion/{scriptVersionId}") - InternalResponse getScriptByScriptVersionId(@PathVariable("scriptVersionId") Long scriptVersionId); - - @ApiOperation(value = "指定Id创建脚本版本", produces = "application/json") - @PostMapping("/service/script/app/{appId}/createScriptWithVersionId") - InternalResponse> createScriptWithVersionId( - @ApiParam("用户名,网关自动传入") @RequestHeader("username") String username, - @ApiParam("创建时间") @RequestHeader(value = "X-Create-Time", required = false) Long createTime, - @ApiParam("修改时间") @RequestHeader(value = "X-Update-Time", required = false) Long lastModifyTime, - @ApiParam("最后修改人") @RequestHeader(value = "X-Update-User", required = false) String lastModifyUser, - @ApiParam("脚本状态") @RequestHeader(value = "X-Script-Status", required = false) Integer scriptStatus, - @ApiParam(value = "业务ID", required = true, example = "2") @PathVariable("appId") Long appId, - @ApiParam(value = "新增/更新的脚本对象", name = "scriptCreateUpdateReq", - required = true) @RequestBody ScriptCreateReq scriptCreateReq); - - @ApiOperation(value = "获取脚本基本信息", produces = "application/json") - @GetMapping("/service/script/{scriptId}") - InternalResponse getBasicScriptInfo(@PathVariable("scriptId") String scriptId); + InternalResponse getScriptByScriptVersionId(@PathVariable("scriptVersionId") + Long scriptVersionId); @ApiOperation(value = "获取已上线版本", produces = "application/json") @GetMapping("/service/script/scriptVersion/online/{scriptId}") diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/op/TenantOpResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/op/TenantOpResource.java new file mode 100644 index 0000000000..a077d1091c --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/op/TenantOpResource.java @@ -0,0 +1,50 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.api.op; + +import com.tencent.bk.job.common.model.Response; +import com.tencent.bk.job.manage.model.op.req.InitTenantReq; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"job-manage:api:Tenant-OP"}) +@RequestMapping("/op/tenant") +@RestController +public interface TenantOpResource { + + @ApiOperation(value = "初始化租户", produces = "application/json") + @PostMapping("/init") + Response initTenant(@ApiParam(value = "初始化租户请求", required = true) + @RequestBody + @Validated + InitTenantReq req); + +} diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/op/req/InitTenantReq.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/op/req/InitTenantReq.java new file mode 100644 index 0000000000..7bb6fd962d --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/op/req/InitTenantReq.java @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.model.op.req; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * 初始化租户请求体 + */ +@Data +public class InitTenantReq { + + @ApiModelProperty(value = "需要初始化的租户ID", required = true) + @NotBlank(message = "{validation.constraints.TenantIdCannotBeBlank.message}") + private String tenantId; + +} diff --git a/src/backend/job-manage/boot-job-manage/src/test/java/com/tencent/bk/job/manage/dao/globalsetting/impl/DangerousRuleDAOImplIntegrationTest.java b/src/backend/job-manage/boot-job-manage/src/test/java/com/tencent/bk/job/manage/dao/globalsetting/impl/CurrentTenantDangerousRuleDAOImplIntegrationTest.java similarity index 67% rename from src/backend/job-manage/boot-job-manage/src/test/java/com/tencent/bk/job/manage/dao/globalsetting/impl/DangerousRuleDAOImplIntegrationTest.java rename to src/backend/job-manage/boot-job-manage/src/test/java/com/tencent/bk/job/manage/dao/globalsetting/impl/CurrentTenantDangerousRuleDAOImplIntegrationTest.java index 68ebdd8cdc..48a530842b 100644 --- a/src/backend/job-manage/boot-job-manage/src/test/java/com/tencent/bk/job/manage/dao/globalsetting/impl/DangerousRuleDAOImplIntegrationTest.java +++ b/src/backend/job-manage/boot-job-manage/src/test/java/com/tencent/bk/job/manage/dao/globalsetting/impl/CurrentTenantDangerousRuleDAOImplIntegrationTest.java @@ -24,7 +24,9 @@ package com.tencent.bk.job.manage.dao.globalsetting.impl; -import com.tencent.bk.job.manage.dao.globalsetting.DangerousRuleDAO; +import com.tencent.bk.job.common.model.User; +import com.tencent.bk.job.common.util.JobContextUtil; +import com.tencent.bk.job.manage.dao.globalsetting.CurrentTenantDangerousRuleDAO; import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; import com.tencent.bk.job.manage.model.query.DangerousRuleQuery; import org.junit.jupiter.api.DisplayName; @@ -52,31 +54,31 @@ @TestPropertySource(locations = "classpath:test.properties") @Sql(value = {"/init_dangerous_rule_data.sql"}) @SqlConfig(encoding = "utf-8") -class DangerousRuleDAOImplIntegrationTest { +class CurrentTenantDangerousRuleDAOImplIntegrationTest { @Autowired - private DangerousRuleDAO dangerousRuleDAO; + private CurrentTenantDangerousRuleDAO currentTenantDangerousRuleDAO; @Test void listDangerousRulesByScriptType() { DangerousRuleDTO dangerousRule = new DangerousRuleDTO(); - dangerousRule.setTenantId("tencent"); + JobContextUtil.setUser(new User("tencent", "anon")); dangerousRule.setScriptType(1); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(8); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(8); dangerousRule.setScriptType(2); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(5); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(5); dangerousRule.setScriptType(3); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(4); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(4); dangerousRule.setScriptType(4); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(5); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(5); dangerousRule.setScriptType(5); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(2); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(2); dangerousRule.setScriptType(6); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(3); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(3); dangerousRule.setScriptType(7); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(0); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(0); dangerousRule.setScriptType(8); - assertThat(dangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(0); + assertThat(currentTenantDangerousRuleDAO.listDangerousRules(dangerousRule)).hasSize(0); } @Test @@ -90,67 +92,67 @@ void listDangerousRulesByQuery() { .expression("sql") .tenantId("tencent") .build(); - List dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + List dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(3); query.setExpression("shell"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(8); query.setExpression("per"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(4); query.setExpression("l"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(9); query.setExpression("python"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(5); query.setExpression("java"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(0); // 规则说明模式查询 query.setExpression(null); query.setDescription("Bat"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(5); query.setDescription("test"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(0); query.setDescription("pow"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(0); query.setDescription(",SQL-6"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(2); query.setDescription("Shell-1,Python-4"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(1); // 拦截动作查询 query.setDescription(null); query.setAction(Arrays.asList(new Byte[]{1})); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(7); query.setAction(Arrays.asList(new Byte[]{0})); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(3); query.setAction(Arrays.asList(new Byte[]{0, 1})); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(10); // 多添加查询 query.setAction(Arrays.asList(new Byte[]{0})); query.setExpression("shell"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(2); query.setAction(Arrays.asList(new Byte[]{0})); query.setExpression("sql"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(0); query.setAction(Arrays.asList(new Byte[]{1})); query.setExpression("sql"); query.setDescription("SQL"); - dangerousRuleDTOS = dangerousRuleDAO.listDangerousRules(query); + dangerousRuleDTOS = currentTenantDangerousRuleDAO.listDangerousRules(query); assertThat(dangerousRuleDTOS).hasSize(3); } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbDangerousRuleV3ResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbDangerousRuleV3ResourceImpl.java index 83276a851b..d3f0dd75b7 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbDangerousRuleV3ResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbDangerousRuleV3ResourceImpl.java @@ -43,7 +43,7 @@ import com.tencent.bk.job.manage.model.query.DangerousRuleQuery; import com.tencent.bk.job.manage.model.web.request.globalsetting.AddOrUpdateDangerousRuleReq; import com.tencent.bk.job.manage.model.web.vo.globalsetting.DangerousRuleVO; -import com.tencent.bk.job.manage.service.DangerousRuleService; +import com.tencent.bk.job.manage.service.CurrentTenantDangerousRuleService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; @@ -55,11 +55,11 @@ @RestController @Slf4j public class EsbDangerousRuleV3ResourceImpl implements EsbDangerousRuleV3Resource { - private final DangerousRuleService dangerousRuleService; + private final CurrentTenantDangerousRuleService currentTenantDangerousRuleService; @Autowired - public EsbDangerousRuleV3ResourceImpl(DangerousRuleService dangerousRuleService) { - this.dangerousRuleService = dangerousRuleService; + public EsbDangerousRuleV3ResourceImpl(CurrentTenantDangerousRuleService currentTenantDangerousRuleService) { + this.currentTenantDangerousRuleService = currentTenantDangerousRuleService; } @Override @@ -74,7 +74,7 @@ public EsbResp createDangerousRule(String username, req.setScriptTypeList(request.getScriptTypeList()); req.setDescription(request.getDescription()); req.setAction(request.getAction()); - DangerousRuleDTO dangerousRule = dangerousRuleService.createDangerousRule(user, req); + DangerousRuleDTO dangerousRule = currentTenantDangerousRuleService.createDangerousRule(user, req); return EsbResp.buildSuccessResp(dangerousRule.toEsbDangerousRuleV3DTO()); } @@ -86,14 +86,14 @@ public EsbResp updateDangerousRule(String username, @AuditRequestBody EsbUpdateDangerousRuleV3Req request) { User user = JobContextUtil.getUser(); AddOrUpdateDangerousRuleReq req = new AddOrUpdateDangerousRuleReq(); - DangerousRuleDTO dangerousRuleDTO = dangerousRuleService.getDangerousRuleById(request.getId()); + DangerousRuleDTO dangerousRuleDTO = currentTenantDangerousRuleService.getDangerousRuleById(request.getId()); req.setId(request.getId()); req.setExpression(request.getExpression()); req.setScriptTypeList(request.getScriptTypeList()); req.setDescription(request.getDescription()); req.setAction(request.getAction()); req.setStatus(dangerousRuleDTO.getStatus()); - DangerousRuleDTO updateDangerousRuleDTO = dangerousRuleService.updateDangerousRule(user, req); + DangerousRuleDTO updateDangerousRuleDTO = currentTenantDangerousRuleService.updateDangerousRule(user, req); return EsbResp.buildSuccessResp(updateDangerousRuleDTO.toEsbDangerousRuleV3DTO()); } @@ -104,7 +104,7 @@ public EsbResp deleteDangerousRule(String username, String appCode, @AuditRequestBody EsbManageDangerousRuleV3Req request) { User user = JobContextUtil.getUser(); - dangerousRuleService.deleteDangerousRuleById(user, request.getId()); + currentTenantDangerousRuleService.deleteDangerousRuleById(user, request.getId()); return EsbResp.buildSuccessResp(null); } @@ -120,8 +120,9 @@ public EsbResp> getDangerousRuleListUsingPost( .description(request.getDescription()) .scriptTypeList(request.getScriptTypeList()) .action(request.getAction() != null ? Collections.singletonList(request.getAction().byteValue()) : null) + .tenantId(JobContextUtil.getTenantId()) .build(); - List dangerousRuleVOS = dangerousRuleService.listDangerousRules(query); + List dangerousRuleVOS = currentTenantDangerousRuleService.listDangerousRules(query); List esbDangerousRuleV3DTOList = dangerousRuleVOS.stream() .map(DangerousRuleVO::toEsbDangerousRuleV3DTO) .collect(Collectors.toList()); @@ -135,7 +136,7 @@ public EsbResp enableDangerousRule(String username, String appCode, @AuditRequestBody EsbManageDangerousRuleV3Req request) { User user = JobContextUtil.getUser(); - DangerousRuleDTO dangerousRuleDTO = dangerousRuleService.updateDangerousRuleStatus(user, + DangerousRuleDTO dangerousRuleDTO = currentTenantDangerousRuleService.updateDangerousRuleStatus(user, request.getId(), EnableStatusEnum.ENABLED); return EsbResp.buildSuccessResp(dangerousRuleDTO.toEsbManageDangerousRuleV3DTO()); @@ -148,7 +149,7 @@ public EsbResp disableDangerousRule(String username, String appCode, @AuditRequestBody EsbManageDangerousRuleV3Req request) { User user = JobContextUtil.getUser(); - DangerousRuleDTO dangerousRuleDTO = dangerousRuleService.updateDangerousRuleStatus(user, + DangerousRuleDTO dangerousRuleDTO = currentTenantDangerousRuleService.updateDangerousRuleStatus(user, request.getId(), EnableStatusEnum.DISABLED); return EsbResp.buildSuccessResp(dangerousRuleDTO.toEsbManageDangerousRuleV3DTO()); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java index d44e11b617..8fdaced681 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java @@ -27,20 +27,16 @@ import com.tencent.bk.job.common.constant.ErrorCode; import com.tencent.bk.job.common.exception.InvalidParamException; import com.tencent.bk.job.common.exception.NotFoundException; -import com.tencent.bk.job.common.i18n.service.MessageI18nService; import com.tencent.bk.job.common.model.InternalResponse; -import com.tencent.bk.job.common.mysql.JobTransactional; import com.tencent.bk.job.common.util.ArrayUtil; import com.tencent.bk.job.manage.api.common.ScriptDTOBuilder; import com.tencent.bk.job.manage.api.inner.ServiceScriptResource; import com.tencent.bk.job.manage.model.dto.ScriptDTO; import com.tencent.bk.job.manage.model.dto.converter.ScriptConverter; import com.tencent.bk.job.manage.model.inner.ServiceScriptDTO; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.service.ScriptManager; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; @@ -48,14 +44,12 @@ @Slf4j public class ServiceScriptResourceImpl implements ServiceScriptResource { private final ScriptManager scriptManager; - private final MessageI18nService i18nService; private final ScriptDTOBuilder scriptDTOBuilder; @Autowired - public ServiceScriptResourceImpl(MessageI18nService i18nService, ScriptManager scriptManager, + public ServiceScriptResourceImpl(ScriptManager scriptManager, ScriptDTOBuilder scriptDTOBuilder) { - this.i18nService = i18nService; this.scriptManager = scriptManager; this.scriptDTOBuilder = scriptDTOBuilder; } @@ -98,47 +92,6 @@ public InternalResponse getScriptByScriptVersionId(Long script return InternalResponse.buildSuccessResp(scriptVersion); } - @Override - @JobTransactional(transactionManager = "jobManageTransactionManager") - public InternalResponse> createScriptWithVersionId(String username, Long createTime, - Long lastModifyTime, String lastModifyUser, - Integer scriptStatus, Long appId, - ScriptCreateReq scriptCreateReq) { - if (log.isDebugEnabled()) { - log.debug("createScriptWithVersionId,operator={},appId={},script={},status={}", username, appId, - scriptCreateReq, scriptStatus); - } - ScriptDTO script = scriptDTOBuilder.buildFromScriptCreateReq(scriptCreateReq); - script.setAppId(appId); - script.setCreator(username); - if (StringUtils.isNotBlank(lastModifyUser)) { - script.setLastModifyUser(lastModifyUser); - } else { - script.setLastModifyUser(username); - } - if (scriptStatus != null && scriptStatus > 0) { - script.setStatus(scriptStatus); - } - return InternalResponse.buildSuccessResp( - scriptManager.createScriptWithVersionId(appId, script, createTime, lastModifyTime)); - } - - @Override - public InternalResponse getBasicScriptInfo(String scriptId) { - if (StringUtils.isEmpty(scriptId)) { - log.warn("Get script by id, param scriptId is empty"); - throw new InvalidParamException(ErrorCode.MISSING_PARAM); - } - - ScriptDTO script = scriptManager.getScriptWithoutTagByScriptId(scriptId); - if (script == null) { - log.warn("Get script by id:{}, the script is not exist", scriptId); - throw new NotFoundException(ErrorCode.SCRIPT_NOT_EXIST, ArrayUtil.toArray(scriptId)); - } - ServiceScriptDTO serviceScript = ScriptConverter.convertToServiceScriptDTO(script); - return InternalResponse.buildSuccessResp(serviceScript); - } - @Override public InternalResponse getOnlineScriptVersion(String scriptId) { if (StringUtils.isEmpty(scriptId)) { diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/op/impl/TenantOpResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/op/impl/TenantOpResourceImpl.java new file mode 100644 index 0000000000..46d0e68b00 --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/op/impl/TenantOpResourceImpl.java @@ -0,0 +1,115 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.api.op.impl; + +import com.tencent.bk.job.common.constant.ErrorCode; +import com.tencent.bk.job.common.model.Response; +import com.tencent.bk.job.common.redis.util.DistributedUniqueTask; +import com.tencent.bk.job.common.util.ip.IpUtils; +import com.tencent.bk.job.manage.api.op.TenantOpResource; +import com.tencent.bk.job.manage.model.op.req.InitTenantReq; +import com.tencent.bk.job.manage.service.impl.sync.BizSetSyncService; +import com.tencent.bk.job.manage.service.impl.sync.BizSyncService; +import com.tencent.bk.job.manage.service.impl.sync.TenantHostSyncService; +import com.tencent.bk.job.manage.service.impl.sync.TenantSetSyncService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.util.StopWatch; +import org.springframework.web.bind.annotation.RestController; + +/** + * 多租户OP接口实现类 + */ +@Slf4j +@RestController +public class TenantOpResourceImpl implements TenantOpResource { + private static final String machineIp = IpUtils.getFirstMachineIP(); + private static final String REDIS_KEY_INIT_TENANT_PREFIX = "initTenant-"; + private final RedisTemplate redisTemplate; + private final BizSyncService bizSyncService; + private final BizSetSyncService bizSetSyncService; + private final TenantSetSyncService tenantSetSyncService; + private final TenantHostSyncService tenantHostSyncService; + + // TODO:用户数据、消息通知渠道数据同步 + @Autowired + public TenantOpResourceImpl(RedisTemplate redisTemplate, + BizSyncService bizSyncService, + BizSetSyncService bizSetSyncService, + TenantSetSyncService tenantSetSyncService, + TenantHostSyncService tenantHostSyncService) { + this.redisTemplate = redisTemplate; + this.bizSyncService = bizSyncService; + this.bizSetSyncService = bizSetSyncService; + this.tenantSetSyncService = tenantSetSyncService; + this.tenantHostSyncService = tenantHostSyncService; + } + + @Override + public Response initTenant(InitTenantReq req) { + String tenantId = req.getTenantId(); + log.info("initTenantTask(tenantId={}) start", tenantId); + StopWatch watch = new StopWatch(); + Object taskResult = false; + try { + // 分布式唯一性保证 + taskResult = new DistributedUniqueTask<>( + redisTemplate, + "InitTenant-" + tenantId, + REDIS_KEY_INIT_TENANT_PREFIX + tenantId, + machineIp, + () -> doInitTenant(tenantId) + ).execute(); + if (taskResult == null) { + // 任务已在其他实例执行 + return Response.buildCommonFailResp(ErrorCode.INIT_TENANT_TASK_ALREADY_RUNNING, new String[]{tenantId}); + } + return Response.buildSuccessResp(taskResult); + } catch (Exception e) { + log.error("initTenantTask failed", e); + return Response.buildCommonFailResp(ErrorCode.INIT_TENANT_ERROR, new String[]{tenantId}); + } finally { + if (watch.isRunning()) { + watch.stop(); + } + if (taskResult != null) { + log.info("initTenantTask finished, timeConsuming={}", watch.prettyPrint()); + } + } + } + + private Object doInitTenant(String tenantId) { + // 1.同步业务 + bizSyncService.syncBizFromCMDB(tenantId); + // 2.同步业务集 + bizSetSyncService.syncBizSetFromCMDB(tenantId); + // 3.同步租户集 + tenantSetSyncService.syncTenantSetFromCMDB(); + // 4.同步租户下所有业务的主机 + tenantHostSyncService.syncAllBizHosts(tenantId); + return true; + } +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebDangerousRuleResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebDangerousRuleResourceImpl.java index c5aa351894..1abd163d28 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebDangerousRuleResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebDangerousRuleResourceImpl.java @@ -37,7 +37,7 @@ import com.tencent.bk.job.manage.model.web.request.globalsetting.AddOrUpdateDangerousRuleReq; import com.tencent.bk.job.manage.model.web.request.globalsetting.MoveDangerousRuleReq; import com.tencent.bk.job.manage.model.web.vo.globalsetting.DangerousRuleVO; -import com.tencent.bk.job.manage.service.DangerousRuleService; +import com.tencent.bk.job.manage.service.CurrentTenantDangerousRuleService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; @@ -47,10 +47,10 @@ @RestController @Slf4j public class WebDangerousRuleResourceImpl implements WebDangerousRuleResource { - private final DangerousRuleService dangerousRuleService; + private final CurrentTenantDangerousRuleService dangerousRuleService; @Autowired - public WebDangerousRuleResourceImpl(DangerousRuleService dangerousRuleService) { + public WebDangerousRuleResourceImpl(CurrentTenantDangerousRuleService dangerousRuleService) { this.dangerousRuleService = dangerousRuleService; } @@ -70,6 +70,7 @@ public Response> listDangerousRules(String username, .description(description) .scriptTypeList(scriptTypeList) .action(action) + .tenantId(JobContextUtil.getTenantId()) .build(); return Response.buildSuccessResp(dangerousRuleService.listDangerousRules(query)); } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java index e2322f441d..023526c9c9 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java @@ -168,7 +168,7 @@ public Response> listPageScript(String username, User user = JobContextUtil.getUser(); ScriptQuery scriptQuery = buildListPageScriptQuery(null, name, type, tags, panelTag, panelType, creator, lastModifyUser, scriptId, content, start, pageSize, orderField, order); - + scriptQuery.setTenantId(user.getTenantId()); PageData pageData = publicScriptService.listPageScript(scriptQuery); PageData resultPageData = pageVOs(pageData, start, pageSize); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationDAO.java index 2ecfed953e..dd361ca8b2 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationDAO.java @@ -56,6 +56,8 @@ public interface ApplicationDAO { List listAllBizSetAppsWithDeleted(); + List listAllTenantSetAppsWithDeleted(); + List listAppsByScopeType(ResourceScopeTypeEnum scopeType); Long insertApp(ApplicationDTO applicationDTO); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ScriptDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ScriptDAO.java index 0bace04c7b..2c0f847501 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ScriptDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ScriptDAO.java @@ -132,15 +132,6 @@ public interface ScriptDAO { */ List listScriptVersionsByScriptId(String scriptId); - /** - * 业务下是否存在相同脚本Id - * - * @param appId - * @param scriptId - * @return - */ - boolean isExistDuplicateScriptId(Long appId, String scriptId); - /** * 业务下是否存在同名脚本 * @@ -277,7 +268,7 @@ PageData listPageOnlineScript(ScriptQuery scriptCondition, * @param appId 业务 ID * @return 脚本数 */ - long countScriptByAppId(long appId); + Long countScriptByAppId(long appId); /** * 脚本版本号是否重复 @@ -288,14 +279,6 @@ PageData listPageOnlineScript(ScriptQuery scriptCondition, */ boolean isExistDuplicateVersion(String scriptId, String version); - /** - * 脚本版本Id是否重复 - * - * @param scriptVersionId 脚本版本Id - * @return 是否重复 - */ - boolean isExistDuplicateScriptId(Long scriptVersionId); - /** * 业务下是否存在任意脚本 * diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/DangerousRuleDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/CurrentTenantDangerousRuleDAO.java similarity index 98% rename from src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/DangerousRuleDAO.java rename to src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/CurrentTenantDangerousRuleDAO.java index a020b9ddb6..c6fff2c67f 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/DangerousRuleDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/CurrentTenantDangerousRuleDAO.java @@ -30,7 +30,7 @@ import java.util.List; -public interface DangerousRuleDAO { +public interface CurrentTenantDangerousRuleDAO { Long insertDangerousRule(DangerousRuleDTO dangerousRuleDTO); int updateDangerousRule(DangerousRuleDTO dangerousRuleDTO); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/TenantDangerousRuleDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/TenantDangerousRuleDAO.java new file mode 100644 index 0000000000..7d18c55945 --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/TenantDangerousRuleDAO.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.dao.globalsetting; + +import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; + +import java.util.List; + +/** + * 对指定租户的高危语句规则进行操作的DAO + */ +public interface TenantDangerousRuleDAO { + + List listDangerousRules(String tenantId, DangerousRuleDTO dangerousRuleQuery); + +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/BaseDangerousRuleDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/BaseDangerousRuleDAOImpl.java new file mode 100644 index 0000000000..df1f1ebc4d --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/BaseDangerousRuleDAOImpl.java @@ -0,0 +1,105 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.dao.globalsetting.impl; + +import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; +import com.tencent.bk.job.manage.model.tables.DangerousRule; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.jooq.Condition; +import org.jooq.DSLContext; +import org.jooq.Record; +import org.jooq.TableField; +import org.springframework.beans.factory.annotation.Qualifier; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 高危语句规则操作基础DAO实现,封装部分公共逻辑 + */ +@Slf4j +public class BaseDangerousRuleDAOImpl { + + protected static final DangerousRule T = DangerousRule.DANGEROUS_RULE; + protected final DSLContext dslContext; + + protected static final TableField[] ALL_FIELDS = { + T.ID, + T.EXPRESSION, + T.DESCRIPTION, + T.PRIORITY, + T.SCRIPT_TYPE, + T.CREATOR, + T.CREATE_TIME, + T.LAST_MODIFY_USER, + T.LAST_MODIFY_TIME, + T.ACTION, + T.STATUS, + }; + + public BaseDangerousRuleDAOImpl(@Qualifier("job-manage-dsl-context") DSLContext dslContext) { + this.dslContext = dslContext; + } + + protected List listDangerousRulesByConditions(Integer scriptType, List conditions) { + val records = + dslContext.select(ALL_FIELDS) + .from(T) + .where(conditions) + .orderBy(T.PRIORITY) + .fetch(); + if (records.isEmpty()) { + return Collections.emptyList(); + } else { + List dangerousRuleList = records.map(this::convertRecordToDto); + if (scriptType == null) { + return dangerousRuleList; + } + int typeFlag = 1 << scriptType - 1; + return dangerousRuleList.stream() + .filter(rule -> (rule.getScriptType() & (typeFlag)) == typeFlag) + .collect(Collectors.toList()); + } + } + + protected DangerousRuleDTO convertRecordToDto(Record record) { + return new DangerousRuleDTO( + record.get(T.ID), + record.get(T.EXPRESSION), + record.get(T.DESCRIPTION), + record.get(T.PRIORITY), + record.get(T.SCRIPT_TYPE), + record.get(T.CREATOR), + record.get(T.CREATE_TIME).longValue(), + record.get(T.LAST_MODIFY_USER), + record.get(T.LAST_MODIFY_TIME).longValue(), + record.get(T.ACTION).intValue(), + record.get(T.STATUS).intValue() + ); + } + +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/DangerousRuleDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/CurrentTenantDangerousRuleDAOImpl.java similarity index 79% rename from src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/DangerousRuleDAOImpl.java rename to src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/CurrentTenantDangerousRuleDAOImpl.java index deb2175b46..020ee99d61 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/DangerousRuleDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/CurrentTenantDangerousRuleDAOImpl.java @@ -25,20 +25,17 @@ package com.tencent.bk.job.manage.dao.globalsetting.impl; import com.tencent.bk.job.common.mysql.util.JooqDataTypeUtil; +import com.tencent.bk.job.common.util.JobContextUtil; import com.tencent.bk.job.manage.api.common.constants.EnableStatusEnum; import com.tencent.bk.job.manage.api.common.constants.script.ScriptTypeEnum; -import com.tencent.bk.job.manage.dao.globalsetting.DangerousRuleDAO; +import com.tencent.bk.job.manage.dao.globalsetting.CurrentTenantDangerousRuleDAO; import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; import com.tencent.bk.job.manage.model.query.DangerousRuleQuery; -import com.tencent.bk.job.manage.model.tables.DangerousRule; -import com.tencent.bk.job.manage.model.tables.records.DangerousRuleRecord; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.StringUtils; import org.jooq.Condition; import org.jooq.DSLContext; -import org.jooq.Record; -import org.jooq.Result; import org.jooq.conf.ParamType; import org.jooq.impl.DSL; import org.jooq.types.ULong; @@ -49,18 +46,18 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; +/** + * 对当前租户下的高危语句规则进行操作的DAO实现,租户ID从JobContext中获取。 + */ @Repository @Slf4j -public class DangerousRuleDAOImpl implements DangerousRuleDAO { - - private static final DangerousRule T = DangerousRule.DANGEROUS_RULE; - private final DSLContext dslContext; +public class CurrentTenantDangerousRuleDAOImpl extends BaseDangerousRuleDAOImpl + implements CurrentTenantDangerousRuleDAO { @Autowired - public DangerousRuleDAOImpl(@Qualifier("job-manage-dsl-context") DSLContext dslContext) { - this.dslContext = dslContext; + public CurrentTenantDangerousRuleDAOImpl(@Qualifier("job-manage-dsl-context") DSLContext dslContext) { + super(dslContext); } private void setDefaultValue(DangerousRuleDTO dangerousRuleDTO) { @@ -69,6 +66,7 @@ private void setDefaultValue(DangerousRuleDTO dangerousRuleDTO) { } } + @SuppressWarnings("DataFlowIssue") @Override public Long insertDangerousRule(DangerousRuleDTO dangerousRuleDTO) { setDefaultValue(dangerousRuleDTO); @@ -95,7 +93,7 @@ public Long insertDangerousRule(DangerousRuleDTO dangerousRuleDTO) { ULong.valueOf(dangerousRuleDTO.getLastModifyTime()), JooqDataTypeUtil.getByteFromInteger(dangerousRuleDTO.getAction()), JooqDataTypeUtil.getByteFromInteger(dangerousRuleDTO.getStatus()), - dangerousRuleDTO.getTenantId() + JobContextUtil.getTenantId() ).returning(T.ID); try { return query.fetchOne().getId(); @@ -117,7 +115,7 @@ public int updateDangerousRule(DangerousRuleDTO dangerousRuleDTO) { .set(T.LAST_MODIFY_TIME, ULong.valueOf(System.currentTimeMillis())) .set(T.ACTION, JooqDataTypeUtil.getByteFromInteger(dangerousRuleDTO.getAction())) .set(T.STATUS, JooqDataTypeUtil.getByteFromInteger(dangerousRuleDTO.getStatus())) - .where(T.ID.eq(dangerousRuleDTO.getId())); + .where(buildIdConditions(dangerousRuleDTO.getId())); try { return query.execute(); } catch (Exception e) { @@ -127,19 +125,31 @@ public int updateDangerousRule(DangerousRuleDTO dangerousRuleDTO) { } } + private List buildIdConditions(Long id) { + List conditions = buildTenantIdConditions(); + conditions.add(T.ID.eq(id)); + return conditions; + } + + private List buildTenantIdConditions() { + List conditions = new ArrayList<>(); + conditions.add(T.TENANT_ID.eq(JobContextUtil.getTenantId())); + return conditions; + } @Override public int deleteDangerousRuleById(Long id) { - return dslContext.deleteFrom(T).where( - T.ID.eq(id) - ).execute(); + return dslContext.deleteFrom(T) + .where(buildIdConditions(id)) + .execute(); } @Override public DangerousRuleDTO getDangerousRuleById(Long id) { - val record = dslContext.selectFrom(T).where( - T.ID.eq(id) - ).fetchOne(); + val record = dslContext.select(ALL_FIELDS) + .from(T) + .where(buildIdConditions(id)) + .fetchOne(); if (record == null) { return null; } else { @@ -149,7 +159,8 @@ val record = dslContext.selectFrom(T).where( @Override public DangerousRuleDTO getDangerousRuleByPriority(String tenantId, int priority) { - val record = dslContext.selectFrom(T) + val record = dslContext.select(ALL_FIELDS) + .from(T) .where(T.PRIORITY.eq(priority)) .and(T.TENANT_ID.eq(tenantId)) .fetchOne(); @@ -162,7 +173,8 @@ val record = dslContext.selectFrom(T) @Override public List listDangerousRules(String tenantId) { - val records = dslContext.selectFrom(T) + val records = dslContext.select(ALL_FIELDS) + .from(T) .where(T.TENANT_ID.eq(tenantId)) .orderBy(T.PRIORITY) .fetch(); @@ -176,31 +188,18 @@ public List listDangerousRules(String tenantId) { @Override public List listDangerousRules(DangerousRuleDTO dangerousRuleQuery) { Integer scriptType = dangerousRuleQuery.getScriptType(); - List conditions = new ArrayList<>(); + List conditions = buildTenantIdConditions(); if (dangerousRuleQuery.getStatus() != null) { conditions.add(T.STATUS.eq(JooqDataTypeUtil.getByteFromInteger(dangerousRuleQuery.getStatus()))); } - conditions.add(T.TENANT_ID.eq(dangerousRuleQuery.getTenantId())); - val records = - dslContext.selectFrom(T).where(conditions).orderBy(T.PRIORITY).fetch(); - if (records.isEmpty()) { - return Collections.emptyList(); - } else { - List dangerousRuleList = records.map(this::convertRecordToDto); - if (scriptType == null) { - return dangerousRuleList; - } - int typeFlag = 1 << scriptType - 1; - return dangerousRuleList.stream() - .filter(rule -> (rule.getScriptType() & (typeFlag)) == typeFlag) - .collect(Collectors.toList()); - } + return listDangerousRulesByConditions(scriptType, conditions); } @Override public List listDangerousRules(DangerousRuleQuery query) { List conditions = buildConditionList(query); - Result records = dslContext.selectFrom(T) + val records = dslContext.select(ALL_FIELDS) + .from(T) .where(conditions) .orderBy(T.PRIORITY) .fetch(); @@ -274,20 +273,4 @@ public int updateDangerousRuleStatus(String userName, Long id, EnableStatusEnum } } - private DangerousRuleDTO convertRecordToDto(Record record) { - return new DangerousRuleDTO( - record.get(T.ID), - record.get(T.EXPRESSION), - record.get(T.DESCRIPTION), - record.get(T.PRIORITY), - record.get(T.SCRIPT_TYPE), - record.get(T.CREATOR), - record.get(T.CREATE_TIME).longValue(), - record.get(T.LAST_MODIFY_USER), - record.get(T.LAST_MODIFY_TIME).longValue(), - record.get(T.ACTION).intValue(), - record.get(T.STATUS).intValue(), - record.get(T.TENANT_ID)); - } - } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/TenantDangerousRuleDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/TenantDangerousRuleDAOImpl.java new file mode 100644 index 0000000000..4d6d5a3720 --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/globalsetting/impl/TenantDangerousRuleDAOImpl.java @@ -0,0 +1,68 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.dao.globalsetting.impl; + +import com.tencent.bk.job.common.mysql.util.JooqDataTypeUtil; +import com.tencent.bk.job.manage.dao.globalsetting.TenantDangerousRuleDAO; +import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; +import lombok.extern.slf4j.Slf4j; +import org.jooq.Condition; +import org.jooq.DSLContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; + +/** + * 对指定租户的高危语句规则进行操作的DAO实现 + */ +@Repository +@Slf4j +public class TenantDangerousRuleDAOImpl extends BaseDangerousRuleDAOImpl implements TenantDangerousRuleDAO { + + @Autowired + public TenantDangerousRuleDAOImpl(@Qualifier("job-manage-dsl-context") DSLContext dslContext) { + super(dslContext); + } + + private List buildTenantIdConditions(String tenantId) { + List conditions = new ArrayList<>(); + conditions.add(T.TENANT_ID.eq(tenantId)); + return conditions; + } + + @Override + public List listDangerousRules(String tenantId, DangerousRuleDTO dangerousRuleQuery) { + Integer scriptType = dangerousRuleQuery.getScriptType(); + List conditions = buildTenantIdConditions(tenantId); + if (dangerousRuleQuery.getStatus() != null) { + conditions.add(T.STATUS.eq(JooqDataTypeUtil.getByteFromInteger(dangerousRuleQuery.getStatus()))); + } + return listDangerousRulesByConditions(scriptType, conditions); + } + +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationDAOImpl.java index ad58abf4b7..0003dbc77d 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationDAOImpl.java @@ -215,6 +215,13 @@ public List listAllBizSetAppsWithDeleted() { return listAppsByConditions(conditions); } + @Override + public List listAllTenantSetAppsWithDeleted() { + List conditions = new ArrayList<>(); + conditions.add(T_APP.BK_SCOPE_TYPE.equal(ResourceScopeTypeEnum.TENANT_SET.getValue())); + return listAppsByConditions(conditions); + } + @Override public List listAppsByScopeType(ResourceScopeTypeEnum scopeType) { List conditions = getBasicNotDeletedConditions(); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java index 3269767d8e..a978da3490 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ScriptDAOImpl.java @@ -40,11 +40,13 @@ import com.tencent.bk.job.manage.model.query.ScriptQuery; import com.tencent.bk.job.manage.model.tables.Script; import com.tencent.bk.job.manage.model.tables.ScriptVersion; +import lombok.val; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.jooq.Condition; import org.jooq.DSLContext; import org.jooq.Record; +import org.jooq.Record1; import org.jooq.Record6; import org.jooq.Result; import org.jooq.SortField; @@ -227,7 +229,7 @@ private List> buildOrderFields(BaseSearchCondition baseSearchCondit private PageData pageResult(Result result, long count, int start, int length) { List scripts = new ArrayList<>(); - if (result.size() != 0) { + if (!result.isEmpty()) { scripts = result.map(this::extractScriptData); } @@ -248,7 +250,7 @@ public List listScripts(ScriptQuery scriptQuery) { .orderBy(TB_SCRIPT.NAME.asc()) .fetch(); List scripts = new ArrayList<>(); - if (result.size() != 0) { + if (!result.isEmpty()) { scripts = result.map(this::extractScriptData); } return scripts; @@ -317,7 +319,7 @@ public List batchGetScriptVersionsByIds(Collection scriptVersio .and(TB_SCRIPT_VERSION.IS_DELETED.eq(UByte.valueOf(0)))) .fetch(); - if (result.size() == 0) { + if (result.isEmpty()) { return Collections.emptyList(); } @@ -494,16 +496,12 @@ public List listScriptVersionsByScriptId(String scriptId) { return result.map(this::extractScriptVersionData); } - @Override - public boolean isExistDuplicateScriptId(Long appId, String scriptId) { - int count = dslContext.selectCount().from(TB_SCRIPT).where(TB_SCRIPT.APP_ID.eq(ULong.valueOf(appId)) - .and(TB_SCRIPT.ID.eq(scriptId))).fetchOne(0, Integer.class); - return (count >= 1); - } - + @SuppressWarnings("DataFlowIssue") @Override public boolean isExistDuplicateName(Long appId, String scriptName) { - int count = dslContext.selectCount().from(TB_SCRIPT).where(TB_SCRIPT.APP_ID.eq(ULong.valueOf(appId))) + int count = dslContext.selectCount() + .from(TB_SCRIPT) + .where(TB_SCRIPT.APP_ID.eq(ULong.valueOf(appId))) .and(TB_SCRIPT.NAME.eq(scriptName)) .and(TB_SCRIPT.IS_DELETED.eq(UByte.valueOf(0))) .fetchOne(0, Integer.class); @@ -542,10 +540,7 @@ private ScriptBasicDTO extractScriptBasicDTO(Record record) { scriptBasicDTO.setAppId(record.get(TB_SCRIPT.APP_ID).longValue()); int scriptScopeValue = record.get(TB_SCRIPT.IS_PUBLIC, Integer.class); - boolean isPublic = false; - if (scriptScopeValue == ScriptScopeEnum.PUBLIC.getValue()) { - isPublic = true; - } + boolean isPublic = scriptScopeValue == ScriptScopeEnum.PUBLIC.getValue(); scriptBasicDTO.setPublicScript(isPublic); scriptBasicDTO.setType(record.get(TB_SCRIPT.TYPE, Integer.class)); scriptBasicDTO.setCategory(record.get(TB_SCRIPT.CATEGORY, Integer.class)); @@ -562,10 +557,7 @@ private ScriptDTO extractScriptData(Record record) { script.setType(record.get(TB_SCRIPT.TYPE, Integer.class)); int scriptScopeValue = record.get(TB_SCRIPT.IS_PUBLIC, Integer.class); - boolean isPublic = false; - if (scriptScopeValue == ScriptScopeEnum.PUBLIC.getValue()) { - isPublic = true; - } + boolean isPublic = scriptScopeValue == ScriptScopeEnum.PUBLIC.getValue(); script.setPublicScript(isPublic); script.setAppId(record.get(TB_SCRIPT.APP_ID).longValue()); script.setCategory(record.get(TB_SCRIPT.CATEGORY, Integer.class)); @@ -587,10 +579,7 @@ private ScriptDTO extractScriptVersionData(Record record) { script.setName(record.get(TB_SCRIPT.NAME)); script.setType(record.get(TB_SCRIPT.TYPE, Integer.class)); int scriptScopeValue = record.get(TB_SCRIPT.IS_PUBLIC, Integer.class); - boolean isPublic = false; - if (scriptScopeValue == ScriptScopeEnum.PUBLIC.getValue()) { - isPublic = true; - } + boolean isPublic = scriptScopeValue == ScriptScopeEnum.PUBLIC.getValue(); script.setPublicScript(isPublic); script.setAppId(record.get(TB_SCRIPT.APP_ID).longValue()); script.setCategory(record.get(TB_SCRIPT.CATEGORY, Integer.class)); @@ -625,7 +614,7 @@ public Map batchGetOnlineByScriptIds(List scriptIds) .and(TB_SCRIPT_VERSION.IS_DELETED.eq(UByte.valueOf(0)))) .fetch(); List scriptVersions = new ArrayList<>(); - if (result.size() > 0) { + if (!result.isEmpty()) { scriptVersions = result.map(this::extractScriptVersionData); } @@ -665,7 +654,7 @@ public void updateScriptVersion(String operator, Long scriptVersionId, ScriptDTO @Override public List listScriptNames(Long appId, String keyword) { String likePattern = "%" + keyword + "%"; - Result result = + val result = dslContext.select(TB_SCRIPT.NAME).from(TB_SCRIPT).where(TB_SCRIPT.APP_ID.eq(ULong.valueOf(appId))) .and(TB_SCRIPT.IS_DELETED.eq(UByte.valueOf(0))) .and(TB_SCRIPT.NAME.like(likePattern)) @@ -682,7 +671,7 @@ public List listScriptNames(Long appId, String keyword) { @Override public List listPublicScriptNames(String tenantId, String keyword) { String likePattern = "%" + keyword + "%"; - Result result = + Result> result = dslContext.select(TB_SCRIPT.NAME) .from(TB_SCRIPT) .where(TB_SCRIPT.TENANT_ID.eq(tenantId)) @@ -700,7 +689,7 @@ public List listPublicScriptNames(String tenantId, String keyword) { } public List listOnlineScriptForApp(long appId) { - Result result = dslContext.select(ALL_FIELDS) + val result = dslContext.select(ALL_FIELDS) .from(TB_SCRIPT_VERSION).join(TB_SCRIPT) .on(TB_SCRIPT.ID.eq(TB_SCRIPT_VERSION.SCRIPT_ID)).where(TB_SCRIPT.APP_ID.eq(ULong.valueOf(appId)) .and(TB_SCRIPT_VERSION.STATUS.eq(UByte.valueOf(JobResourceStatusEnum.ONLINE.getValue()))) @@ -719,8 +708,7 @@ public List listOnlineScriptForApp(long appId) { @Override public List listOnlinePublicScript(String tenantId) { - - Result result = dslContext + val result = dslContext .select(ALL_FIELDS) .from(TB_SCRIPT) .join(TB_SCRIPT_VERSION) @@ -749,14 +737,17 @@ public PageData listPageOnlineScript(ScriptQuery scriptCondition, List conditions = buildScriptConditionList(scriptCondition, baseSearchCondition); conditions.add(ScriptVersion.SCRIPT_VERSION.STATUS.eq(UByte.valueOf(JobResourceStatusEnum.ONLINE.getValue()))); - long total = dslContext.selectCount().from(tbScriptVersion).join(tbScript) - .on(tbScript.ID.eq(tbScriptVersion.SCRIPT_ID)). - where(conditions).fetchOne(0, Long.class); + Long total = dslContext.selectCount() + .from(tbScriptVersion) + .join(tbScript) + .on(tbScript.ID.eq(tbScriptVersion.SCRIPT_ID)) + .where(conditions) + .fetchOne(0, Long.class); int start = baseSearchCondition.getStartOrDefault(0); int length = baseSearchCondition.getLengthOrDefault(10); - Result result = dslContext.select(ALL_FIELDS).from(tbScriptVersion).join(tbScript) + val result = dslContext.select(ALL_FIELDS).from(tbScriptVersion).join(tbScript) .on(tbScript.ID.eq(tbScriptVersion.SCRIPT_ID)) .where(conditions) .orderBy(tbScriptVersion.LAST_MODIFY_TIME.desc()) @@ -781,13 +772,13 @@ public PageData listPageScriptVersion(ScriptQuery scriptQuery) { Script tbScript = Script.SCRIPT; ScriptVersion tbScriptVersion = ScriptVersion.SCRIPT_VERSION; - long count = getScriptVersionPageCount(scriptQuery); + Long count = getScriptVersionPageCount(scriptQuery); BaseSearchCondition baseSearchCondition = scriptQuery.getBaseSearchCondition(); int start = baseSearchCondition.getStartOrDefault(0); int length = baseSearchCondition.getLengthOrDefault(10); - Result result = dslContext.select(ALL_FIELDS).from(tbScriptVersion).join(tbScript) + val result = dslContext.select(ALL_FIELDS).from(tbScriptVersion).join(tbScript) .on(tbScript.ID.eq(tbScriptVersion.SCRIPT_ID)) .where(buildScriptVersionConditionList(scriptQuery, baseSearchCondition)) .orderBy(tbScriptVersion.LAST_MODIFY_TIME.desc()) @@ -808,7 +799,7 @@ public PageData listPageScriptVersion(ScriptQuery scriptQuery) { return scriptVersionPageData; } - private long getScriptVersionPageCount(ScriptQuery scriptQuery) { + private Long getScriptVersionPageCount(ScriptQuery scriptQuery) { BaseSearchCondition baseSearchCondition = scriptQuery.getBaseSearchCondition(); Script tbScript = Script.SCRIPT; ScriptVersion tbScriptVersion = ScriptVersion.SCRIPT_VERSION; @@ -846,29 +837,24 @@ public ScriptDTO getOnlineScriptVersionByScriptId(String scriptId) { } @Override - public long countScriptByAppId(long appId) { + public Long countScriptByAppId(long appId) { Script tbScript = Script.SCRIPT.as("tbScript"); - return dslContext.selectCount().from(tbScript) + return dslContext.selectCount() + .from(tbScript) .where(tbScript.APP_ID.eq(ULong.valueOf(appId)) - .and(tbScript.IS_DELETED.eq(UByte.valueOf(0)))).fetchOne().value1(); + .and(tbScript.IS_DELETED.eq(UByte.valueOf(0)))) + .fetchOne(0, Long.class); } @Override public boolean isExistDuplicateVersion(String scriptId, String version) { - int count = dslContext.selectCount().from(TB_SCRIPT_VERSION) + Integer count = dslContext.selectCount() + .from(TB_SCRIPT_VERSION) .where(TB_SCRIPT_VERSION.SCRIPT_ID.eq(scriptId)) .and(TB_SCRIPT_VERSION.VERSION.eq(version)) .and(TB_SCRIPT_VERSION.IS_DELETED.eq(UByte.valueOf(0))) .fetchOne(0, Integer.class); - return (count >= 1); - } - - @Override - public boolean isExistDuplicateScriptId(Long scriptVersionId) { - int count = dslContext.selectCount().from(TB_SCRIPT_VERSION) - .where(TB_SCRIPT_VERSION.ID.eq(ULong.valueOf(scriptVersionId))) - .fetchOne(0, Integer.class); - return (count >= 1); + return (count != null && count >= 1); } @Override @@ -904,16 +890,20 @@ public Integer countScripts(Long appId, ScriptTypeEnum scriptTypeEnum, conditions.add(tbScriptVersion.STATUS.eq(UByte.valueOf(jobResourceStatusEnum.getValue()))); } conditions.add(tbScript.IS_DELETED.eq(UByte.valueOf(0))); - return dslContext.select(DSL.countDistinct(tbScript.ID)).from(tbScript).join(tbScriptVersion) + return dslContext.select(DSL.countDistinct(tbScript.ID)) + .from(tbScript) + .join(tbScriptVersion) .on(tbScript.ID.eq(tbScriptVersion.SCRIPT_ID)) - .where(conditions).fetchOne().value1(); + .where(conditions) + .fetchOne(0, Integer.class); } @Override public Integer countScripts() { Script tbScript = Script.SCRIPT.as("tbScript"); return dslContext.selectCount().from(tbScript) - .where(tbScript.IS_DELETED.eq(UByte.valueOf(0))).fetchOne().value1(); + .where(tbScript.IS_DELETED.eq(UByte.valueOf(0))) + .fetchOne(0, Integer.class); } @Override @@ -932,9 +922,12 @@ public Integer countScriptVersions(Long appId, ScriptTypeEnum scriptTypeEnum, conditions.add(tbScriptVersion.STATUS.eq(UByte.valueOf(jobResourceStatusEnum.getValue()))); } conditions.add(tbScript.IS_DELETED.eq(UByte.valueOf(0))); - return dslContext.select(DSL.countDistinct(tbScriptVersion.ID)).from(tbScript).join(tbScriptVersion) + return dslContext.select(DSL.countDistinct(tbScriptVersion.ID)) + .from(tbScript) + .join(tbScriptVersion) .on(tbScript.ID.eq(tbScriptVersion.SCRIPT_ID)) - .where(conditions).fetchOne().value1(); + .where(conditions) + .fetchOne(0, Integer.class); } @Override @@ -944,7 +937,7 @@ public List listAppScriptIds(Long appId) { if (appId != null) { conditions.add(TB_SCRIPT.APP_ID.eq(ULong.valueOf(appId))); } - Result result = dslContext.select(TB_SCRIPT.ID).from(TB_SCRIPT) + val result = dslContext.select(TB_SCRIPT.ID).from(TB_SCRIPT) .where(conditions) .fetch(); List scriptIdList = new ArrayList<>(); @@ -961,7 +954,7 @@ public List listPublicScriptIds(String tenantId) { conditions.add(TB_SCRIPT.IS_DELETED.eq(UByte.valueOf(0))); conditions.add(TB_SCRIPT.APP_ID.eq(ULong.valueOf(PUBLIC_APP_ID))); conditions.add(TB_SCRIPT.TENANT_ID.eq(tenantId)); - Result result = dslContext + val result = dslContext .select(TB_SCRIPT.ID) .from(TB_SCRIPT) .where(conditions) diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/manager/cache/DangerousRuleCache.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/manager/cache/DangerousRuleCache.java index fd896371cb..f6ddb253f7 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/manager/cache/DangerousRuleCache.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/manager/cache/DangerousRuleCache.java @@ -26,7 +26,7 @@ import com.tencent.bk.job.common.util.json.JsonUtils; import com.tencent.bk.job.manage.api.common.constants.rule.HighRiskGrammarRuleStatusEnum; -import com.tencent.bk.job.manage.dao.globalsetting.DangerousRuleDAO; +import com.tencent.bk.job.manage.dao.globalsetting.TenantDangerousRuleDAO; import com.tencent.bk.job.manage.model.db.DangerousRuleDO; import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; import lombok.extern.slf4j.Slf4j; @@ -46,13 +46,13 @@ @Slf4j public class DangerousRuleCache { public static final String DANGEROUS_RULE_HASH_KEY = "job:manage:dangerousRules"; - private final DangerousRuleDAO dangerousRuleDAO; + private final TenantDangerousRuleDAO tenantDangerousRuleDAO; private final RedisTemplate redisTemplate; @Autowired - public DangerousRuleCache(DangerousRuleDAO dangerousRuleDAO, + public DangerousRuleCache(TenantDangerousRuleDAO tenantDangerousRuleDAO, @Qualifier("jsonRedisTemplate") RedisTemplate redisTemplate) { - this.dangerousRuleDAO = dangerousRuleDAO; + this.tenantDangerousRuleDAO = tenantDangerousRuleDAO; this.redisTemplate = redisTemplate; } @@ -71,7 +71,10 @@ public List listDangerousRuleFromCache(String tenantId, Intege DangerousRuleDTO dangerousRuleQuery = new DangerousRuleDTO(); dangerousRuleQuery.setScriptType(scriptType); dangerousRuleQuery.setStatus(HighRiskGrammarRuleStatusEnum.ENABLED.getCode()); - List dangerousRuleDTOList = dangerousRuleDAO.listDangerousRules(dangerousRuleQuery); + List dangerousRuleDTOList = tenantDangerousRuleDAO.listDangerousRules( + tenantId, + dangerousRuleQuery + ); if (CollectionUtils.isEmpty(dangerousRuleDTOList)) { redisTemplate.opsForHash().put(hashKey, String.valueOf(scriptType), new ArrayList()); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/migration/AddHostIdForWhiteIpMigrationTask.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/migration/AddHostIdForWhiteIpMigrationTask.java index a23b97ee3c..4a64bd8b5b 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/migration/AddHostIdForWhiteIpMigrationTask.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/migration/AddHostIdForWhiteIpMigrationTask.java @@ -24,7 +24,7 @@ package com.tencent.bk.job.manage.migration; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.manage.dao.whiteip.WhiteIPIPDAO; import com.tencent.bk.job.manage.model.dto.whiteip.WhiteIPIPDTO; @@ -49,10 +49,10 @@ public class AddHostIdForWhiteIpMigrationTask { private final WhiteIPIPDAO whiteIPIPDAO; - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired - public AddHostIdForWhiteIpMigrationTask(WhiteIPIPDAO whiteIPIPDAO, BizCmdbClient bizCmdbClient) { + public AddHostIdForWhiteIpMigrationTask(WhiteIPIPDAO whiteIPIPDAO, IBizCmdbClient bizCmdbClient) { this.whiteIPIPDAO = whiteIPIPDAO; this.bizCmdbClient = bizCmdbClient; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/globalsetting/DangerousRuleDTO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/globalsetting/DangerousRuleDTO.java index 0a187bd8f1..f2ce569a69 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/globalsetting/DangerousRuleDTO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/globalsetting/DangerousRuleDTO.java @@ -98,11 +98,6 @@ public class DangerousRuleDTO { */ private Integer status; - /** - * 租户 ID - */ - private String tenantId; - public DangerousRuleDTO(Long id, String expression, String description, @@ -113,8 +108,7 @@ public DangerousRuleDTO(Long id, String lastModifier, Long lastModifyTime, Integer action, - Integer status, - String tenantId) { + Integer status) { this.id = id; this.expression = expression; this.description = description; @@ -126,7 +120,6 @@ public DangerousRuleDTO(Long id, this.lastModifyTime = lastModifyTime; this.action = action; this.status = status; - this.tenantId = tenantId; } public DangerousRuleVO toVO() { diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/DangerousRuleService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CurrentTenantDangerousRuleService.java similarity index 95% rename from src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/DangerousRuleService.java rename to src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CurrentTenantDangerousRuleService.java index 035fa0e61a..435685c0f8 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/DangerousRuleService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CurrentTenantDangerousRuleService.java @@ -34,9 +34,10 @@ import java.util.List; -public interface DangerousRuleService { - - List listDangerousRules(User user); +/** + * 对当前租户下的高危语句规则进行相关操作的接口 + */ +public interface CurrentTenantDangerousRuleService { DangerousRuleDTO getDangerousRuleById(Long id); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/ScriptManager.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/ScriptManager.java index d34fc93b6f..93d4aa426d 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/ScriptManager.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/ScriptManager.java @@ -38,7 +38,6 @@ import com.tencent.bk.job.manage.model.dto.script.ScriptCitedTaskTemplateDTO; import com.tencent.bk.job.manage.model.query.ScriptQuery; import com.tencent.bk.job.manage.model.web.vo.TagCountVO; -import org.apache.commons.lang3.tuple.Pair; import java.util.Collection; import java.util.List; @@ -102,14 +101,6 @@ public interface ScriptManager { */ List listScriptBasicInfoByScriptIds(Collection scriptIds); - /** - * 根据scriptId查询脚本基本信息,不包含标签信息 - * - * @param scriptId 脚本ID - * @return 脚本 - */ - ScriptDTO getScriptWithoutTagByScriptId(String scriptId); - /** * 根据脚本ID查询所有版本的脚本 * @@ -156,17 +147,6 @@ public interface ScriptManager { */ ScriptDTO updateScriptVersion(ScriptDTO scriptVersion); - /** - * 指定版本Id创建脚本版本 - * - * @param appId 业务ID - * @param script 脚本信息 - */ - Pair createScriptWithVersionId(Long appId, - ScriptDTO script, - Long createTime, - Long lastModifyTime); - /** * 删除脚本 * diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizDynamicGroupHostService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizDynamicGroupHostService.java index a32102a1b5..b90de25e77 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizDynamicGroupHostService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizDynamicGroupHostService.java @@ -25,7 +25,7 @@ package com.tencent.bk.job.manage.service.host.impl; import com.tencent.bk.job.common.cc.model.DynamicGroupHostPropDTO; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.constant.ErrorCode; import com.tencent.bk.job.common.constant.ResourceScopeTypeEnum; import com.tencent.bk.job.common.exception.NotImplementedException; @@ -57,11 +57,11 @@ @Service public class BizDynamicGroupHostService { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; private final HostDetailService hostDetailService; @Autowired - public BizDynamicGroupHostService(BizCmdbClient bizCmdbClient, + public BizDynamicGroupHostService(IBizCmdbClient bizCmdbClient, HostDetailService hostDetailService) { this.bizCmdbClient = bizCmdbClient; this.hostDetailService = hostDetailService; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java index 187a7d652c..d599c0e179 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java @@ -25,7 +25,6 @@ package com.tencent.bk.job.manage.service.host.impl; import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; import com.tencent.bk.job.common.cc.sdk.CmdbClientFactory; import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.constant.JobConstants; @@ -71,13 +70,13 @@ public class HostServiceImpl implements HostService { private final ApplicationHostDAO applicationHostDAO; private final ApplicationService applicationService; private final HostCache hostCache; - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired public HostServiceImpl(ApplicationHostDAO applicationHostDAO, ApplicationService applicationService, HostCache hostCache, - BizCmdbClient bizCmdbClient) { + IBizCmdbClient bizCmdbClient) { this.applicationHostDAO = applicationHostDAO; this.applicationService = applicationService; this.hostCache = hostCache; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AppRoleServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AppRoleServiceImpl.java index 7fe53de433..d17aa6173d 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AppRoleServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AppRoleServiceImpl.java @@ -25,9 +25,9 @@ package com.tencent.bk.job.manage.service.impl; import com.tencent.bk.job.common.cc.model.AppRoleDTO; -import com.tencent.bk.job.common.cc.sdk.BizSetCmdbClient; import com.tencent.bk.job.common.cc.sdk.CmdbClientFactory; import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizSetCmdbClient; import com.tencent.bk.job.common.constant.ResourceScopeTypeEnum; import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.model.dto.ResourceScope; @@ -48,11 +48,11 @@ public class AppRoleServiceImpl implements AppRoleService { private final ApplicationService applicationService; - private final BizSetCmdbClient bizSetCmdbClient; + private final IBizSetCmdbClient bizSetCmdbClient; @Autowired public AppRoleServiceImpl(@Lazy ApplicationService applicationService, - BizSetCmdbClient bizSetCmdbClient) { + IBizSetCmdbClient bizSetCmdbClient) { this.applicationService = applicationService; this.bizSetCmdbClient = bizSetCmdbClient; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizDynamicGroupService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizDynamicGroupService.java index 4604ebc4d6..971dd4fc5d 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizDynamicGroupService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizDynamicGroupService.java @@ -25,7 +25,7 @@ package com.tencent.bk.job.manage.service.impl; import com.tencent.bk.job.common.cc.model.CcDynamicGroupDTO; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.manage.model.dto.DynamicGroupDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -41,10 +41,10 @@ @Service public class BizDynamicGroupService { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired - public BizDynamicGroupService(BizCmdbClient bizCmdbClient) { + public BizDynamicGroupService(IBizCmdbClient bizCmdbClient) { this.bizCmdbClient = bizCmdbClient; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ContainerServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ContainerServiceImpl.java index 483dc6873c..669935e917 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ContainerServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ContainerServiceImpl.java @@ -27,7 +27,7 @@ import com.tencent.bk.job.common.cc.model.container.ContainerDetailDTO; import com.tencent.bk.job.common.cc.model.container.KubeTopologyDTO; import com.tencent.bk.job.common.cc.model.req.ListKubeContainerByTopoReq; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.manage.model.query.ContainerQuery; import com.tencent.bk.job.manage.service.ContainerService; @@ -41,10 +41,10 @@ @Slf4j public class ContainerServiceImpl implements ContainerService { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired - public ContainerServiceImpl(BizCmdbClient bizCmdbClient) { + public ContainerServiceImpl(IBizCmdbClient bizCmdbClient) { this.bizCmdbClient = bizCmdbClient; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/DangerousRuleServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CurrentTenantDangerousRuleServiceImpl.java similarity index 76% rename from src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/DangerousRuleServiceImpl.java rename to src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CurrentTenantDangerousRuleServiceImpl.java index afaf752d41..5a8ee15013 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/DangerousRuleServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CurrentTenantDangerousRuleServiceImpl.java @@ -32,14 +32,14 @@ import com.tencent.bk.job.common.model.User; import com.tencent.bk.job.common.mysql.JobTransactional; import com.tencent.bk.job.manage.api.common.constants.EnableStatusEnum; -import com.tencent.bk.job.manage.dao.globalsetting.DangerousRuleDAO; +import com.tencent.bk.job.manage.dao.globalsetting.CurrentTenantDangerousRuleDAO; import com.tencent.bk.job.manage.manager.cache.DangerousRuleCache; import com.tencent.bk.job.manage.model.dto.globalsetting.DangerousRuleDTO; import com.tencent.bk.job.manage.model.query.DangerousRuleQuery; import com.tencent.bk.job.manage.model.web.request.globalsetting.AddOrUpdateDangerousRuleReq; import com.tencent.bk.job.manage.model.web.request.globalsetting.MoveDangerousRuleReq; import com.tencent.bk.job.manage.model.web.vo.globalsetting.DangerousRuleVO; -import com.tencent.bk.job.manage.service.DangerousRuleService; +import com.tencent.bk.job.manage.service.CurrentTenantDangerousRuleService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -47,30 +47,27 @@ import java.util.List; import java.util.stream.Collectors; +/** + * 对当前租户下的高危语句规则进行操作的服务实现,租户ID从JobContext中获取。 + */ @Slf4j @Service -public class DangerousRuleServiceImpl implements DangerousRuleService { +public class CurrentTenantDangerousRuleServiceImpl implements CurrentTenantDangerousRuleService { - private final DangerousRuleDAO dangerousRuleDAO; + private final CurrentTenantDangerousRuleDAO currentTenantDangerousRuleDAO; private final DangerousRuleCache dangerousRuleCache; @Autowired - public DangerousRuleServiceImpl( - DangerousRuleDAO dangerousRuleDAO, + public CurrentTenantDangerousRuleServiceImpl( + CurrentTenantDangerousRuleDAO currentTenantDangerousRuleDAO, DangerousRuleCache dangerousRuleCache) { - this.dangerousRuleDAO = dangerousRuleDAO; + this.currentTenantDangerousRuleDAO = currentTenantDangerousRuleDAO; this.dangerousRuleCache = dangerousRuleCache; } - @Override - public List listDangerousRules(User user) { - return dangerousRuleDAO.listDangerousRules(user.getTenantId()).stream().map(DangerousRuleDTO::toVO) - .collect(Collectors.toList()); - } - @Override public DangerousRuleDTO getDangerousRuleById(Long id) { - return dangerousRuleDAO.getDangerousRuleById(id); + return currentTenantDangerousRuleDAO.getDangerousRuleById(id); } @Override @@ -80,9 +77,9 @@ public DangerousRuleDTO getDangerousRuleById(Long id) { ) public DangerousRuleDTO createDangerousRule(User user, AddOrUpdateDangerousRuleReq req) { int scriptType = DangerousRuleDTO.encodeScriptType(req.getScriptTypeList()); - int maxPriority = dangerousRuleDAO.getMaxPriority(user.getTenantId()); + int maxPriority = currentTenantDangerousRuleDAO.getMaxPriority(user.getTenantId()); log.info(String.format("current maxPriority:%d", maxPriority)); - long id = dangerousRuleDAO.insertDangerousRule( + long id = currentTenantDangerousRuleDAO.insertDangerousRule( new DangerousRuleDTO( null, req.getExpression(), req.getDescription(), @@ -93,15 +90,14 @@ public DangerousRuleDTO createDangerousRule(User user, AddOrUpdateDangerousRuleR user.getUsername(), System.currentTimeMillis(), req.getAction(), - EnableStatusEnum.DISABLED.getValue(), - user.getTenantId() + EnableStatusEnum.DISABLED.getValue() ) ); // 清理缓存 dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), req.getScriptTypeList()); - return getDangerousRuleById(id); + return currentTenantDangerousRuleDAO.getDangerousRuleById(id); } @Override @@ -111,9 +107,9 @@ public DangerousRuleDTO createDangerousRule(User user, AddOrUpdateDangerousRuleR ) public DangerousRuleDTO updateDangerousRule(User user, AddOrUpdateDangerousRuleReq req) { int scriptType = DangerousRuleDTO.encodeScriptType(req.getScriptTypeList()); - DangerousRuleDTO existDangerousRuleDTO = dangerousRuleDAO.getDangerousRuleById(req.getId()); + DangerousRuleDTO existDangerousRuleDTO = currentTenantDangerousRuleDAO.getDangerousRuleById(req.getId()); if (existDangerousRuleDTO != null) { - dangerousRuleDAO.updateDangerousRule( + currentTenantDangerousRuleDAO.updateDangerousRule( new DangerousRuleDTO( req.getId(), req.getExpression(), @@ -125,15 +121,14 @@ public DangerousRuleDTO updateDangerousRule(User user, AddOrUpdateDangerousRuleR user.getUsername(), System.currentTimeMillis(), req.getAction(), - req.getStatus(), - user.getTenantId() + req.getStatus() ) ); // 清理缓存 List existScriptTypes = DangerousRuleDTO.decodeScriptType(existDangerousRuleDTO.getScriptType()); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), existScriptTypes); } - return getDangerousRuleById(req.getId()); + return currentTenantDangerousRuleDAO.getDangerousRuleById(req.getId()); } @@ -145,25 +140,25 @@ public DangerousRuleDTO updateDangerousRule(User user, AddOrUpdateDangerousRuleR ) public Integer moveDangerousRule(User user, MoveDangerousRuleReq req) { int dir = req.getDir(); - DangerousRuleDTO currentRuleDTO = dangerousRuleDAO.getDangerousRuleById(req.getId()); + DangerousRuleDTO currentRuleDTO = currentTenantDangerousRuleDAO.getDangerousRuleById(req.getId()); if (currentRuleDTO == null) { log.info("id=%d dangerousRule not exist"); return 0; } if (dir == -1) { //往上移动 - int minPriority = dangerousRuleDAO.getMinPriority(user.getTenantId()); + int minPriority = currentTenantDangerousRuleDAO.getMinPriority(user.getTenantId()); if (currentRuleDTO.getPriority() <= minPriority) { log.info("Fail to move, id=%d dangerousRule already has min priority"); return 0; } //需要移动的情况 - DangerousRuleDTO upperRuleDTO = dangerousRuleDAO.getDangerousRuleByPriority(user.getTenantId(), + DangerousRuleDTO upperRuleDTO = currentTenantDangerousRuleDAO.getDangerousRuleByPriority(user.getTenantId(), currentRuleDTO.getPriority() - 1); upperRuleDTO.setPriority(upperRuleDTO.getPriority() + 1); currentRuleDTO.setPriority(currentRuleDTO.getPriority() - 1); - dangerousRuleDAO.updateDangerousRule(upperRuleDTO); - dangerousRuleDAO.updateDangerousRule(currentRuleDTO); + currentTenantDangerousRuleDAO.updateDangerousRule(upperRuleDTO); + currentTenantDangerousRuleDAO.updateDangerousRule(currentRuleDTO); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), DangerousRuleDTO.decodeScriptType(upperRuleDTO.getScriptType())); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), @@ -171,21 +166,22 @@ public Integer moveDangerousRule(User user, MoveDangerousRuleReq req) { return 2; } else if (dir == 1) { //往下移动 - int maxPriority = dangerousRuleDAO.getMaxPriority(user.getTenantId()); + int maxPriority = currentTenantDangerousRuleDAO.getMaxPriority(user.getTenantId()); if (currentRuleDTO.getPriority() >= maxPriority) { log.info("Fail to move, id=%d dangerousRule already has max priority"); return 0; } //需要移动的情况 - DangerousRuleDTO downerRuleDTO = dangerousRuleDAO.getDangerousRuleByPriority(user.getTenantId(), - currentRuleDTO.getPriority() + 1); + DangerousRuleDTO downerRuleDTO = + currentTenantDangerousRuleDAO.getDangerousRuleByPriority(user.getTenantId(), + currentRuleDTO.getPriority() + 1); if (downerRuleDTO == null) { return 0; } downerRuleDTO.setPriority(downerRuleDTO.getPriority() - 1); currentRuleDTO.setPriority(currentRuleDTO.getPriority() + 1); - dangerousRuleDAO.updateDangerousRule(downerRuleDTO); - dangerousRuleDAO.updateDangerousRule(currentRuleDTO); + currentTenantDangerousRuleDAO.updateDangerousRule(downerRuleDTO); + currentTenantDangerousRuleDAO.updateDangerousRule(currentRuleDTO); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), DangerousRuleDTO.decodeScriptType(downerRuleDTO.getScriptType())); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), @@ -204,11 +200,12 @@ public Integer moveDangerousRule(User user, MoveDangerousRuleReq req) { content = EventContentConstants.DELETE_HIGH_RISK_DETECT_RULE ) public Integer deleteDangerousRuleById(User user, Long id) { - DangerousRuleDTO existDangerousRuleDTO = dangerousRuleDAO.getDangerousRuleById(id); + DangerousRuleDTO existDangerousRuleDTO = currentTenantDangerousRuleDAO.getDangerousRuleById(id); if (existDangerousRuleDTO == null) { return -1; } - List dangerousRuleDTOList = dangerousRuleDAO.listDangerousRules(user.getTenantId()); + List dangerousRuleDTOList = + currentTenantDangerousRuleDAO.listDangerousRules(user.getTenantId()); for (int i = 0; i < dangerousRuleDTOList.size(); i++) { if (dangerousRuleDTOList.get(i).getId().equals(id)) { dangerousRuleDTOList.remove(i); @@ -217,11 +214,11 @@ public Integer deleteDangerousRuleById(User user, Long id) { } try { //每次删除后维持有序 - dangerousRuleDAO.deleteDangerousRuleById(id); + currentTenantDangerousRuleDAO.deleteDangerousRuleById(id); for (int i = 0; i < dangerousRuleDTOList.size(); i++) { DangerousRuleDTO dangerousRuleDTO = dangerousRuleDTOList.get(i); dangerousRuleDTO.setPriority(i + 1); - dangerousRuleDAO.updateDangerousRule(dangerousRuleDTO); + currentTenantDangerousRuleDAO.updateDangerousRule(dangerousRuleDTO); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), DangerousRuleDTO.decodeScriptType(dangerousRuleDTO.getScriptType())); } @@ -236,7 +233,7 @@ public Integer deleteDangerousRuleById(User user, Long id) { @Override public List listDangerousRules(DangerousRuleQuery query) { - return dangerousRuleDAO.listDangerousRules(query) + return currentTenantDangerousRuleDAO.listDangerousRules(query) .stream() .map(DangerousRuleDTO::toVO) .collect(Collectors.toList()); @@ -248,8 +245,8 @@ public List listDangerousRules(DangerousRuleQuery query) { content = EventContentConstants.EDIT_HIGH_RISK_DETECT_RULE ) public DangerousRuleDTO updateDangerousRuleStatus(User user, Long id, EnableStatusEnum status) { - dangerousRuleDAO.updateDangerousRuleStatus(user.getUsername(), id, status); - DangerousRuleDTO dangerousRuleDTO = getDangerousRuleById(id); + currentTenantDangerousRuleDAO.updateDangerousRuleStatus(user.getUsername(), id, status); + DangerousRuleDTO dangerousRuleDTO = currentTenantDangerousRuleDAO.getDangerousRuleById(id); dangerousRuleCache.deleteDangerousRuleCacheByScriptTypes(user.getTenantId(), DangerousRuleDTO.decodeScriptType(dangerousRuleDTO.getScriptType())); return dangerousRuleDTO; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java index 39b0fd31ab..337210ff39 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java @@ -25,13 +25,11 @@ package com.tencent.bk.job.manage.service.impl; import com.tencent.bk.job.common.constant.ErrorCode; -import com.tencent.bk.job.common.constant.JobConstants; import com.tencent.bk.job.common.constant.JobResourceTypeEnum; import com.tencent.bk.job.common.exception.AlreadyExistsException; import com.tencent.bk.job.common.exception.FailedPreconditionException; import com.tencent.bk.job.common.exception.InvalidParamException; import com.tencent.bk.job.common.exception.NotFoundException; -import com.tencent.bk.job.common.exception.ServiceException; import com.tencent.bk.job.common.i18n.service.MessageI18nService; import com.tencent.bk.job.common.iam.exception.PermissionDeniedException; import com.tencent.bk.job.common.iam.model.AuthResult; @@ -40,8 +38,6 @@ import com.tencent.bk.job.common.model.dto.AppResourceScope; import com.tencent.bk.job.common.mysql.JobTransactional; import com.tencent.bk.job.common.util.JobUUID; -import com.tencent.bk.job.common.util.date.DateUtils; -import com.tencent.bk.job.common.util.json.JsonUtils; import com.tencent.bk.job.manage.api.common.constants.JobResourceStatusEnum; import com.tencent.bk.job.manage.api.common.constants.script.ScriptTypeEnum; import com.tencent.bk.job.manage.auth.TemplateAuthService; @@ -70,8 +66,6 @@ import com.tencent.bk.job.manage.service.template.impl.TemplateScriptStatusUpdateService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Lazy; @@ -179,11 +173,6 @@ public List listScriptBasicInfoByScriptIds(Collection sc return scriptDAO.listScriptBasicInfoByScriptIds(scriptIds); } - @Override - public ScriptDTO getScriptWithoutTagByScriptId(String scriptId) { - return scriptDAO.getScriptByScriptId(scriptId); - } - @Override public List listScriptVersion(Long appId, String scriptId) { List scripts = scriptDAO.listScriptVersionsByScriptId(scriptId); @@ -326,13 +315,6 @@ private void saveScriptTags(String operator, Long appId, String scriptId, List createScriptWithVersionId( - Long appId, - ScriptDTO script, - Long createTime, - Long lastModifyTime - ) throws ServiceException { - log.info("Begin to createScriptWithVersionId, appId={}, script={}, createTime={}, " + - "lastModifyTime={}", appId, JsonUtils.toJson(script), createTime, lastModifyTime); - script.setCreateTime(getTimeOrDefault(createTime)); - script.setLastModifyTime(getTimeOrDefault(lastModifyTime)); - final long targetAppId = script.isPublicScript() ? PUBLIC_APP_ID : appId; - script.setAppId(targetAppId); - - // 默认为未上线状态 - Integer status = script.getStatus(); - if (status == null || status < 0) { - script.setStatus(JobResourceStatusEnum.DRAFT.getValue()); - } - - if (script.getScriptVersionId() != null && script.getScriptVersionId() > 0) { - if (scriptDAO.isExistDuplicateScriptId(script.getScriptVersionId())) { - log.warn("scriptVersionId:{} is exist, scriptId:{}", script.getScriptVersionId(), script.getId()); - throw new AlreadyExistsException(ErrorCode.SCRIPT_VERSION_ID_EXIST); - } - } - - if (StringUtils.isNotBlank(script.getId())) { - if (scriptDAO.isExistDuplicateVersion(script.getId(), script.getVersion())) { - log.warn("Script version:{} is exist, scriptId:{}", script.getVersion(), script.getId()); - throw new AlreadyExistsException(ErrorCode.SCRIPT_VERSION_NAME_EXIST); - } - if (!scriptDAO.isExistDuplicateScriptId(appId, script.getId())) { - //脚本不存在,新增脚本和脚本版本 - boolean isNameDuplicate = scriptDAO.isExistDuplicateName(targetAppId, script.getName()); - if (isNameDuplicate) { - log.warn("The script name:{} is exist for app:{}", script.getName(), targetAppId); - throw new AlreadyExistsException(ErrorCode.SCRIPT_NAME_DUPLICATE); - } - saveScriptAndScriptVersionToDB(script); - log.info("script created with specified id:{}", script.getId()); - } else { - // 脚本存在,新增脚本版本 - saveScriptVersionToDB(script); - } - } else { - //脚本不存在,新增脚本 - boolean isNameDuplicate = scriptDAO.isExistDuplicateName(targetAppId, script.getName()); - if (isNameDuplicate) { - log.warn("The script name:{} is exist for app:{}", script.getName(), targetAppId); - throw new AlreadyExistsException(ErrorCode.SCRIPT_NAME_DUPLICATE); - } - - script.setId(JobUUID.getUUID()); - saveScriptAndScriptVersionToDB(script); - } - saveScriptTags(appId, script); - return Pair.of(script.getId(), script.getScriptVersionId()); - } - - public void saveScriptAndScriptVersionToDB(ScriptDTO script) { - // 插入script - String scriptId = scriptDAO.saveScript(script); - script.setId(scriptId); - // 插入script_version - Long scriptVersionId = scriptDAO.saveScriptVersion(script); - script.setScriptVersionId(scriptVersionId); - } - - public void saveScriptVersionToDB(ScriptDTO script) { - // 插入script_version - Long scriptVersionId = scriptDAO.saveScriptVersion(script); - scriptDAO.updateScriptLastModify(script.getId(), script.getLastModifyUser(), script.getLastModifyTime()); - script.setScriptVersionId(scriptVersionId); - } - @Override @JobTransactional(transactionManager = "jobManageTransactionManager") public void deleteScriptVersion(Long appId, Long scriptVersionId) { diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizEventWatcher.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizEventWatcher.java index 0652f955fa..6ee39ec7d8 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizEventWatcher.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizEventWatcher.java @@ -28,7 +28,7 @@ import com.tencent.bk.job.common.cc.model.result.BizEventDetail; import com.tencent.bk.job.common.cc.model.result.ResourceEvent; import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.exception.NotFoundException; import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.util.json.JsonUtils; @@ -48,7 +48,7 @@ @Component public class BizEventWatcher extends AbstractCmdbResourceEventWatcher { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; private final ApplicationService applicationService; private final AtomicBoolean bizWatchFlag = new AtomicBoolean(true); @@ -57,7 +57,7 @@ public class BizEventWatcher extends AbstractCmdbResourceEventWatcher redisTemplate, Tracer tracer, CmdbEventSampler cmdbEventSampler, - BizCmdbClient bizCmdbClient, + IBizCmdbClient bizCmdbClient, ApplicationService applicationService) { super("biz", redisTemplate, tracer, cmdbEventSampler); this.bizCmdbClient = bizCmdbClient; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetEventWatcher.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetEventWatcher.java index 2ea5e7d352..f9df2628fb 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetEventWatcher.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetEventWatcher.java @@ -28,7 +28,7 @@ import com.tencent.bk.job.common.cc.model.result.BizSetEventDetail; import com.tencent.bk.job.common.cc.model.result.ResourceEvent; import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; -import com.tencent.bk.job.common.cc.sdk.BizSetCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizSetCmdbClient; import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.manage.metrics.CmdbEventSampler; import com.tencent.bk.job.manage.metrics.MetricsConstants; @@ -50,7 +50,7 @@ public class BizSetEventWatcher extends AbstractCmdbResourceEventWatcher { private final ApplicationService applicationService; private final BizSetService bizSetService; - private final BizSetCmdbClient bizSetCmdbClient; + private final IBizSetCmdbClient bizSetCmdbClient; @Autowired public BizSetEventWatcher(RedisTemplate redisTemplate, @@ -58,7 +58,7 @@ public BizSetEventWatcher(RedisTemplate redisTemplate, CmdbEventSampler cmdbEventSampler, ApplicationService applicationService, BizSetService bizSetService, - BizSetCmdbClient bizSetCmdbClient) { + IBizSetCmdbClient bizSetCmdbClient) { super("bizSet", redisTemplate, tracer, cmdbEventSampler); this.applicationService = applicationService; this.bizSetService = bizSetService; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetRelationEventWatcher.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetRelationEventWatcher.java index d3767bddf2..c331c65c5c 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetRelationEventWatcher.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/BizSetRelationEventWatcher.java @@ -28,7 +28,7 @@ import com.tencent.bk.job.common.cc.model.result.BizSetRelationEventDetail; import com.tencent.bk.job.common.cc.model.result.ResourceEvent; import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; -import com.tencent.bk.job.common.cc.sdk.BizSetCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizSetCmdbClient; import com.tencent.bk.job.common.constant.ResourceScopeTypeEnum; import com.tencent.bk.job.common.model.dto.ApplicationAttrsDO; import com.tencent.bk.job.common.model.dto.ApplicationDTO; @@ -54,7 +54,7 @@ public class BizSetRelationEventWatcher extends AbstractCmdbResourceEventWatcher { private final ApplicationService applicationService; private final BizSetService bizSetService; - private final BizSetCmdbClient bizSetCmdbClient; + private final IBizSetCmdbClient bizSetCmdbClient; @Autowired public BizSetRelationEventWatcher(RedisTemplate redisTemplate, @@ -62,7 +62,7 @@ public BizSetRelationEventWatcher(RedisTemplate redisTemplate, CmdbEventSampler cmdbEventSampler, ApplicationService applicationService, BizSetService bizSetService, - BizSetCmdbClient bizSetCmdbClient) { + IBizSetCmdbClient bizSetCmdbClient) { super("bizSetRelation", redisTemplate, tracer, cmdbEventSampler); this.applicationService = applicationService; this.bizSetService = bizSetService; @@ -113,7 +113,8 @@ public void handleEvent(ResourceEvent event) { @Override protected Tags getEventMetricTags() { - return Tags.of(MetricsConstants.TAG_KEY_CMDB_EVENT_TYPE, MetricsConstants.TAG_VALUE_CMDB_EVENT_TYPE_BIZ_SET_RELATION); + return Tags.of(MetricsConstants.TAG_KEY_CMDB_EVENT_TYPE, + MetricsConstants.TAG_VALUE_CMDB_EVENT_TYPE_BIZ_SET_RELATION); } @Override diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java index 37482e7484..aefc5e7acf 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java @@ -27,7 +27,7 @@ import com.tencent.bk.job.common.cc.model.result.HostEventDetail; import com.tencent.bk.job.common.cc.model.result.ResourceEvent; import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.gse.service.AgentStateClient; import com.tencent.bk.job.manage.config.GseConfig; import com.tencent.bk.job.manage.config.JobManageConfig; @@ -58,7 +58,7 @@ public class HostEventWatcher extends AbstractCmdbResourceEventWatcher redisTemplate, Tracer tracer, CmdbEventSampler cmdbEventSampler, - BizCmdbClient bizCmdbClient, + IBizCmdbClient bizCmdbClient, HostService hostService, @Qualifier(GseConfig.MANAGE_BEAN_AGENT_STATE_CLIENT) - AgentStateClient agentStateClient, + AgentStateClient agentStateClient, JobManageConfig jobManageConfig) { super("host", redisTemplate, tracer, cmdbEventSampler); this.tracer = tracer; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostRelationEventWatcher.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostRelationEventWatcher.java index 3e91e25abb..425fe13eb5 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostRelationEventWatcher.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostRelationEventWatcher.java @@ -27,7 +27,7 @@ import com.tencent.bk.job.common.cc.model.result.HostRelationEventDetail; import com.tencent.bk.job.common.cc.model.result.ResourceEvent; import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.manage.dao.ApplicationHostDAO; import com.tencent.bk.job.manage.dao.HostTopoDAO; import com.tencent.bk.job.manage.manager.host.HostCache; @@ -58,7 +58,7 @@ public class HostRelationEventWatcher extends AbstractCmdbResourceEventWatcher redisTemplate, Tracer tracer, CmdbEventSampler cmdbEventSampler, - BizCmdbClient bizCmdbClient, + IBizCmdbClient bizCmdbClient, ApplicationService applicationService, ApplicationHostDAO applicationHostDAO, HostTopoDAO hostTopoDAO, diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostSyncService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostSyncService.java index b3328a3bd9..456bdebf54 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostSyncService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostSyncService.java @@ -28,7 +28,7 @@ import com.tencent.bk.job.common.cc.model.result.HostProp; import com.tencent.bk.job.common.cc.model.result.HostWithModules; import com.tencent.bk.job.common.cc.model.result.ModuleProp; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.constant.CcNodeTypeEnum; import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; @@ -70,14 +70,14 @@ public class HostSyncService { private final ApplicationHostDAO applicationHostDAO; private final HostTopoDAO hostTopoDAO; private final HostService hostService; - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired public HostSyncService(AppHostsUpdateHelper appHostsUpdateHelper, ApplicationHostDAO applicationHostDAO, HostTopoDAO hostTopoDAO, HostService hostService, - BizCmdbClient bizCmdbClient) { + IBizCmdbClient bizCmdbClient) { this.appHostsUpdateHelper = appHostsUpdateHelper; this.applicationHostDAO = applicationHostDAO; this.hostTopoDAO = hostTopoDAO; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/SyncServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/SyncServiceImpl.java index 76d4527663..51d4e48a7a 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/SyncServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/SyncServiceImpl.java @@ -102,6 +102,7 @@ public class SyncServiceImpl implements SyncService { private final ApplicationCache applicationCache; private final BizSyncService bizSyncService; private final BizSetSyncService bizSetSyncService; + private final TenantSetSyncService tenantSetSyncService; private final HostSyncService hostSyncService; private final AgentStatusSyncService agentStatusSyncService; private final BizSetEventWatcher bizSetEventWatcher; @@ -111,6 +112,7 @@ public class SyncServiceImpl implements SyncService { @Autowired public SyncServiceImpl(BizSyncService bizSyncService, BizSetSyncService bizSetSyncService, + TenantSetSyncService tenantSetSyncService, HostSyncService hostSyncService, AgentStatusSyncService agentStatusSyncService, ApplicationDAO applicationDAO, @@ -134,6 +136,7 @@ public SyncServiceImpl(BizSyncService bizSyncService, this.applicationCache = applicationCache; this.bizSyncService = bizSyncService; this.bizSetSyncService = bizSetSyncService; + this.tenantSetSyncService = tenantSetSyncService; this.hostSyncService = hostSyncService; this.agentStatusSyncService = agentStatusSyncService; this.bizEventWatcher = bizEventWatcher; @@ -238,6 +241,7 @@ private void doSyncApp() { watch.start("total"); List tenantList = userMgrApiClient.listAllTenant(); try { + tenantSetSyncService.syncTenantSetFromCMDB(); // 遍历所有租户 for (OpenApiTenant openApiTenant : tenantList) { // 从CMDB同步业务信息 diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/TenantHostSyncService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/TenantHostSyncService.java new file mode 100644 index 0000000000..44af7de0e8 --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/TenantHostSyncService.java @@ -0,0 +1,77 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.service.impl.sync; + +import com.tencent.bk.job.common.model.dto.ApplicationDTO; +import com.tencent.bk.job.common.model.dto.BasicHostDTO; +import com.tencent.bk.job.manage.dao.ApplicationDAO; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Triple; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +@SuppressWarnings("FieldCanBeLocal") +@Slf4j +@Service +public class TenantHostSyncService { + + + private final ApplicationDAO applicationDAO; + private final HostSyncService hostSyncService; + + @Autowired + public TenantHostSyncService(ApplicationDAO applicationDAO, HostSyncService hostSyncService) { + this.applicationDAO = applicationDAO; + this.hostSyncService = hostSyncService; + } + + public void syncAllBizHosts(String tenantId) { + log.info("syncAllBizHosts: tenantId={}", tenantId); + List appList = applicationDAO.listAllAppsForTenant(tenantId); + List failedAppList = new ArrayList<>(); + for (ApplicationDTO applicationDTO : appList) { + Triple, Long, Long> triple = hostSyncService.syncBizHostsAtOnce(applicationDTO); + if (triple == null) { + failedAppList.add(applicationDTO); + } + } + if (!failedAppList.isEmpty()) { + log.warn( + "syncAllBizHosts: tenantId={}, failedAppList={}", + tenantId, + failedAppList.stream().map( + applicationDTO -> "(" + applicationDTO.getBizIdIfBizApp() + "," + applicationDTO.getName() + ")" + ).collect(Collectors.toList()) + ); + } + log.info("syncAllBizHosts end: tenantId={}", tenantId); + } + +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/TenantSetSyncService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/TenantSetSyncService.java new file mode 100644 index 0000000000..23d1de605a --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/TenantSetSyncService.java @@ -0,0 +1,187 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.service.impl.sync; + +import com.tencent.bk.job.common.cc.config.CmdbConfig; +import com.tencent.bk.job.common.cc.model.tenantset.TenantSetInfo; +import com.tencent.bk.job.common.cc.model.tenantset.TenantSetScope; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.ITenantSetCmdbClient; +import com.tencent.bk.job.common.constant.ResourceScopeTypeEnum; +import com.tencent.bk.job.common.model.dto.ApplicationAttrsDO; +import com.tencent.bk.job.common.model.dto.ApplicationDTO; +import com.tencent.bk.job.common.model.dto.ResourceScope; +import com.tencent.bk.job.common.util.json.JsonUtils; +import com.tencent.bk.job.manage.dao.ApplicationDAO; +import com.tencent.bk.job.manage.dao.ApplicationHostDAO; +import com.tencent.bk.job.manage.service.ApplicationService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * CMDB租户集同步逻辑 + */ +@Slf4j +@Service +public class TenantSetSyncService extends BasicAppSyncService { + + private final ApplicationDAO applicationDAO; + protected final ITenantSetCmdbClient tenantSetCmdbClient; + private final CmdbConfig cmdbConfig; + + @Autowired + public TenantSetSyncService(ApplicationDAO applicationDAO, + ApplicationHostDAO applicationHostDAO, + ApplicationService applicationService, + IBizCmdbClient bizCmdbClient, + ITenantSetCmdbClient tenantSetCmdbClient, + CmdbConfig cmdbConfig) { + super(applicationDAO, applicationHostDAO, applicationService, bizCmdbClient); + this.applicationDAO = applicationDAO; + this.tenantSetCmdbClient = tenantSetCmdbClient; + this.cmdbConfig = cmdbConfig; + } + + /** + * 从CMDB同步租户集信息到本地DB + */ + public void syncTenantSetFromCMDB() { + log.info("[{}] Begin to sync tenantSet from cmdb", Thread.currentThread().getName()); + List ccTenantSets = tenantSetCmdbClient.listAllTenantSet(); + if (log.isInfoEnabled()) { + log.info("Sync cmdb tenantSet result: {}", JsonUtils.toJson(ccTenantSets)); + } + + // CMDB租户Id + Set cmdbTenantSetIds = ccTenantSets.stream() + .map(TenantSetInfo::getId) + .collect(Collectors.toSet()); + // CMDB接口空数据保护 + if (cmdbTenantSetIds.isEmpty()) { + log.warn("CMDB TenantSet data is empty, quit sync"); + return; + } + log.info("Cmdb sync tenantSetIds: {}", cmdbTenantSetIds); + + List localTenantSetApps = applicationDAO.listAllTenantSetAppsWithDeleted(); + // 本地租户bizId + Set localTenantSetIds = + localTenantSetApps.stream() + .map(localTenantSetApp -> Long.valueOf(localTenantSetApp.getScope().getId())) + .collect(Collectors.toSet()); + log.info("Local cached tenantSetIds: {}", localTenantSetIds); + + // 对比租户集信息,分出要新增的/要改的/要删的分别处理 + List insertApps = computeInsertApps(ccTenantSets, localTenantSetIds); + List updateApps = computeUpdateApps(ccTenantSets, localTenantSetApps, localTenantSetIds); + List deleteApps = computeDeleteApps(cmdbTenantSetIds, localTenantSetApps); + + applyAppsChangeByScope(insertApps, deleteApps, updateApps); + } + + /** + * 计算新增租户集 + * + * @param cmdbTenantSets 从cmdb同步的租户集 + * @param localTenantSetIds 本地缓存的租户集ID列表 + * @return 新增的租户 + */ + private List computeInsertApps(List cmdbTenantSets, Set localTenantSetIds) { + // CMDB-本地:计算新增租户集 + List insertList = + cmdbTenantSets.stream() + .filter(ccTenantSet -> !localTenantSetIds.contains(ccTenantSet.getId())) + .map(this::convertTenantSetToApplication) + .collect(Collectors.toList()); + log.info("Insert tenantSetIds: {}", String.join(",", + insertList.stream().map(tenantSetAppInfoDTO -> tenantSetAppInfoDTO.getScope().getId()) + .collect(Collectors.toSet()))); + return insertList; + } + + /** + * 计算更新租户集 + * + * @param cmdbTenantSets 从cmdb同步的租户集 + * @param localTenantSetApps 本次缓存的租户集 + * @param localTenantSetIds 本地缓存的租户集ID列表 + * @return 新增的租户 + */ + private List computeUpdateApps(List cmdbTenantSets, + List localTenantSetApps, + Set localTenantSetIds) { + // 本地&CMDB交集:计算需要更新的租户集 + List updateList = + cmdbTenantSets.stream().filter(ccTenantSetAppInfoDTO -> + localTenantSetIds.contains(ccTenantSetAppInfoDTO.getId())) + .map(this::convertTenantSetToApplication) + .collect(Collectors.toList()); + log.info("Update tenantSetIds: {}", String.join(",", + updateList.stream().map(applicationInfoDTO -> + applicationInfoDTO.getScope().getId()).collect(Collectors.toSet()))); + // 将本地租户集的appId赋给CMDB拿到的租户集 + updateAppIdByScope(updateList, genScopeAppIdMap(localTenantSetApps)); + return updateList; + } + + /** + * 计算删除的租户集 + * + * @param cmdbTenantSetIds cmdb租户集ID列表 + * @param localTenantSetApps 本地缓存的租户集 + * @return 需要删除的租户 + */ + private List computeDeleteApps(Set cmdbTenantSetIds, + List localTenantSetApps) { + // 本地-CMDB:计算需要删除的租户集 + List deleteList = localTenantSetApps.stream().filter(localApp -> + !cmdbTenantSetIds.contains(Long.valueOf(localApp.getScope().getId()))) + .collect(Collectors.toList()); + log.info("Delete tenantSetIds: {}", String.join(",", + deleteList.stream().map(applicationInfoDTO -> + applicationInfoDTO.getScope().getId()).collect(Collectors.toSet()))); + return deleteList; + } + + private ApplicationDTO convertTenantSetToApplication(TenantSetInfo tenantSetInfo) { + ApplicationDTO appInfoDTO = new ApplicationDTO(); + appInfoDTO.setBkSupplierAccount(cmdbConfig.getDefaultSupplierAccount()); + appInfoDTO.setName(tenantSetInfo.getName()); + TenantSetScope scope = tenantSetInfo.getScope(); + ApplicationAttrsDO attrs = new ApplicationAttrsDO(); + // 全租户 + attrs.setMatchAllTenant(scope != null && scope.isMatchAll()); + appInfoDTO.setAttrs(attrs); + appInfoDTO.setScope( + new ResourceScope(ResourceScopeTypeEnum.TENANT_SET, tenantSetInfo.getId().toString())); + appInfoDTO.setDeFault(tenantSetInfo.getDeFault()); + return appInfoDTO; + } +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/topo/BizTopoService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/topo/BizTopoService.java index c44b129cbf..74c6a089d8 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/topo/BizTopoService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/topo/BizTopoService.java @@ -26,7 +26,7 @@ import com.tencent.bk.job.common.cc.model.CcInstanceDTO; import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO; -import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; +import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient; import com.tencent.bk.job.common.cc.util.TopologyUtil; import com.tencent.bk.job.common.util.json.JsonUtils; import com.tencent.bk.job.manage.model.web.request.chooser.host.BizTopoNode; @@ -45,10 +45,10 @@ @Service public class BizTopoService { - private final BizCmdbClient bizCmdbClient; + private final IBizCmdbClient bizCmdbClient; @Autowired - public BizTopoService(BizCmdbClient bizCmdbClient) { + public BizTopoService(IBizCmdbClient bizCmdbClient) { this.bizCmdbClient = bizCmdbClient; } diff --git a/support-files/kubernetes/charts/bk-job/values.yaml b/support-files/kubernetes/charts/bk-job/values.yaml index eff1b231bb..2dc7f68cd1 100644 --- a/support-files/kubernetes/charts/bk-job/values.yaml +++ b/support-files/kubernetes/charts/bk-job/values.yaml @@ -458,6 +458,10 @@ mockApi: iam: # 是否启用IAM Mock API enabled: false + # 配置平台API + cmdb: + # 是否启用CMDB Mock API + enabled: false ## 蓝鲸GSE参数配置 gse: