From 709adc8cf5a6bdecdbaf28f652573244bb039000 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Fri, 25 Nov 2022 20:42:19 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E7=BA=AFIPv6=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=88=86=E5=8F=91=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6/?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E6=96=87=E4=BB=B6=E4=B8=80=E7=9B=B4?= =?UTF-8?q?=E5=8D=A1=E4=BD=8F=20#1530?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改CMDB自定义查询规则,减少嵌套深度,补充查询字段 --- .../cc/model/req/ListHostsWithoutBizReq.java | 1 + .../bk/job/common/cc/sdk/BizCmdbClient.java | 74 +++++++++---------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/backend/commons/cmdb-sdk-model/src/main/java/com/tencent/bk/job/common/cc/model/req/ListHostsWithoutBizReq.java b/src/backend/commons/cmdb-sdk-model/src/main/java/com/tencent/bk/job/common/cc/model/req/ListHostsWithoutBizReq.java index 5e7f264006..26fda06ea8 100644 --- a/src/backend/commons/cmdb-sdk-model/src/main/java/com/tencent/bk/job/common/cc/model/req/ListHostsWithoutBizReq.java +++ b/src/backend/commons/cmdb-sdk-model/src/main/java/com/tencent/bk/job/common/cc/model/req/ListHostsWithoutBizReq.java @@ -45,6 +45,7 @@ public class ListHostsWithoutBizReq extends EsbReq { "bk_host_id", "bk_host_innerip", "bk_host_innerip_v6", + "bk_agent_id", "bk_host_name", "bk_os_name", "bk_cloud_id" 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 2c416e1056..3ad37002f8 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 @@ -662,30 +662,32 @@ private List convertToHostInfoDTOList( return applicationHostDTOList; } - private ApplicationHostDTO convertHost(long bizId, CcHostInfoDTO hostInfo) { - ApplicationHostDTO ipInfo = new ApplicationHostDTO(); - ipInfo.setHostId(hostInfo.getHostId()); - // 部分从cmdb同步过来的资源没有ip,需要过滤掉 - if (StringUtils.isBlank(hostInfo.getIp())) { + private ApplicationHostDTO convertHost(long bizId, CcHostInfoDTO ccHostInfo) { + // 部分从cmdb同步过来的资源没有hostId,需要过滤掉 + if (ccHostInfo.getHostId() == null) { return null; } + ApplicationHostDTO hostDTO = new ApplicationHostDTO(); + hostDTO.setHostId(ccHostInfo.getHostId()); - if (hostInfo.getOs() != null && hostInfo.getOs().length() > 512) { - ipInfo.setOsName(hostInfo.getOs().substring(0, 512)); + if (ccHostInfo.getOs() != null && ccHostInfo.getOs().length() > 512) { + log.warn("osName truncated to 512, host={}", ccHostInfo); + hostDTO.setOsName(ccHostInfo.getOs().substring(0, 512)); } else { - ipInfo.setOsName(hostInfo.getOs()); + hostDTO.setOsName(ccHostInfo.getOs()); } - if (hostInfo.getCloudId() != null) { - ipInfo.setCloudAreaId(hostInfo.getCloudId()); + if (ccHostInfo.getCloudId() != null) { + hostDTO.setCloudAreaId(ccHostInfo.getCloudId()); } else { - log.warn("Host does not have cloud area id!|{}", hostInfo); + log.warn("Host does not have cloud area id!|{}", ccHostInfo); return null; } - ipInfo.setIp(hostInfo.getIp()); - ipInfo.setBizId(bizId); - ipInfo.setHostName(hostInfo.getHostName()); - - return ipInfo; + hostDTO.setIp(ccHostInfo.getIp()); + hostDTO.setIpv6(ccHostInfo.getIpv6()); + hostDTO.setAgentId(ccHostInfo.getAgentId()); + hostDTO.setBizId(bizId); + hostDTO.setHostName(ccHostInfo.getHostName()); + return hostDTO; } @Override @@ -1036,33 +1038,27 @@ public List listHostsByCloudIpv6s(List cloudIpv6s) { PropertyFilterDTO condition = new PropertyFilterDTO(); condition.setCondition("OR"); Map> hostGroups = groupHostsByBkCloudId(cloudIpv6s); - hostGroups.forEach((bkCloudId, ipv6s) -> { - ComposeRuleDTO hostRule = new ComposeRuleDTO(); - hostRule.setCondition("AND"); - hostRule.addRule(buildCloudIdRule(bkCloudId)); - - ComposeRuleDTO multiIpv6Rule = buildMultiIpv6LikeRule(ipv6s); - hostRule.addRule(multiIpv6Rule); - - condition.addRule(hostRule); - }); + hostGroups.forEach((bkCloudId, ipv6s) -> + ipv6s.forEach(ipv6 -> { + ComposeRuleDTO hostRule = new ComposeRuleDTO(); + hostRule.setCondition("AND"); + hostRule.addRule(buildCloudIdRule(bkCloudId)); + + BaseRuleDTO ipv6Rule = buildIpv6Rule(ipv6); + hostRule.addRule(ipv6Rule); + condition.addRule(hostRule); + })); req.setCondition(condition); - return listHostsWithoutBiz(req); } - private ComposeRuleDTO buildMultiIpv6LikeRule(List ipv6s) { - ComposeRuleDTO multiIpv6Rule = new ComposeRuleDTO(); - multiIpv6Rule.setCondition("OR"); - ipv6s.forEach(ipv6 -> { - BaseRuleDTO ipv6Rule = new BaseRuleDTO(); - // ipv6字段可能包含多个IPv6地址,故此处使用contains - ipv6Rule.setField("bk_host_innerip_v6"); - ipv6Rule.setOperator("contains"); - ipv6Rule.setValue(ipv6); - multiIpv6Rule.addRule(ipv6Rule); - }); - return multiIpv6Rule; + private BaseRuleDTO buildIpv6Rule(String ipv6) { + BaseRuleDTO ipv6Rule = new BaseRuleDTO(); + // ipv6字段可能包含多个IPv6地址,故此处使用contains + ipv6Rule.setField("bk_host_innerip_v6"); + ipv6Rule.setOperator("contains"); + ipv6Rule.setValue(ipv6); + return ipv6Rule; } private BaseRuleDTO buildCloudIdRule(Long bkCloudId) {