From cda0fe8503e65e3d6b3530940e686887a1b293c8 Mon Sep 17 00:00:00 2001 From: chenjr15 Date: Tue, 14 Jan 2025 17:36:33 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=85=B3?= =?UTF-8?q?=E9=97=ADtaskManagement=E4=BB=BB=E5=8A=A1=E6=89=AB=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cloud-server/service/task/task_timing.go | 5 +++++ pkg/cc/service.go | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/cmd/cloud-server/service/task/task_timing.go b/cmd/cloud-server/service/task/task_timing.go index 0c5e17d3d0..d15a7a9e7b 100644 --- a/cmd/cloud-server/service/task/task_timing.go +++ b/cmd/cloud-server/service/task/task_timing.go @@ -25,6 +25,7 @@ import ( "hcm/pkg/api/core" coretask "hcm/pkg/api/core/task" datatask "hcm/pkg/api/data-service/task" + "hcm/pkg/cc" "hcm/pkg/client" "hcm/pkg/criteria/enumor" "hcm/pkg/dal/dao/tools" @@ -36,6 +37,10 @@ import ( // TimingHandleTaskMgmtState 定时更新任务管理数据状态 func TimingHandleTaskMgmtState(c *client.ClientSet, sd serviced.State, interval time.Duration) { + if cc.CloudServer().TaskManagement.Disable { + logs.Warnf("task management state background update has been disabled") + return + } for { time.Sleep(interval) diff --git a/pkg/cc/service.go b/pkg/cc/service.go index 826a0fb766..86fbcd5b95 100644 --- a/pkg/cc/service.go +++ b/pkg/cc/service.go @@ -110,6 +110,12 @@ func (s ApiServerSetting) Validate() error { return nil } +// TaskManagement ... +type TaskManagement struct { + // 关闭任务管理轮询 + Disable bool `yaml:"disable"` +} + // CloudServerSetting defines cloud server used setting options. type CloudServerSetting struct { Network Network `yaml:"network"` @@ -124,6 +130,7 @@ type CloudServerSetting struct { Itsm ApiGateway `yaml:"itsm"` CloudSelection CloudSelection `yaml:"cloudSelection"` Cmsi CMSI `yaml:"cmsi"` + TaskManagement TaskManagement `yaml:"taskManagement"` } // trySetFlagBindIP try set flag bind ip. From 37504ba3aa71bfc1c699331a53546ea60a84336b Mon Sep 17 00:00:00 2001 From: chenjr15 Date: Wed, 22 Jan 2025 11:49:18 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E5=AE=89=E5=85=A8=E7=BB=84?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=94=AF=E6=8C=81=E5=8F=AF=E8=A7=81=E8=8C=83?= =?UTF-8?q?=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=121678806 --story=121678900 --story=121727886 --story=121727880 --story=121727876 --story=121727875 --- cmd/hc-service/logics/res-sync/aws/client.go | 1 + .../res-sync/aws/security_group_usage_biz.go | 50 ++++++ .../logics/res-sync/azure/client.go | 1 + .../azure/security_group_usage_biz.go | 50 ++++++ .../logics/res-sync/huawei/client.go | 1 + .../huawei/security_group_usage_biz.go | 50 ++++++ .../logics/res-sync/tcloud/client.go | 1 + .../tcloud/load_balancer_security_group.go | 41 +++-- .../tcloud/security_group_usage_biz.go | 50 ++++++ .../usage-biz-rel/sg_usage_biz_rel_manager.go | 147 ++++++++++++++++++ .../sync/aws/security_group_usage_biz.go | 122 +++++++++++++++ .../sync/azure/security_group_usage_biz.go | 122 +++++++++++++++ .../sync/huawei/security_group_usage_biz.go | 122 +++++++++++++++ .../sync/tcloud/security_group_usage_biz.go | 122 +++++++++++++++ pkg/api/core/cloud/security_group.go | 5 + 15 files changed, 872 insertions(+), 13 deletions(-) create mode 100644 cmd/hc-service/logics/res-sync/aws/security_group_usage_biz.go create mode 100644 cmd/hc-service/logics/res-sync/azure/security_group_usage_biz.go create mode 100644 cmd/hc-service/logics/res-sync/huawei/security_group_usage_biz.go create mode 100644 cmd/hc-service/logics/res-sync/tcloud/security_group_usage_biz.go create mode 100644 cmd/hc-service/logics/res-sync/usage-biz-rel/sg_usage_biz_rel_manager.go create mode 100644 cmd/hc-service/service/sync/aws/security_group_usage_biz.go create mode 100644 cmd/hc-service/service/sync/azure/security_group_usage_biz.go create mode 100644 cmd/hc-service/service/sync/huawei/security_group_usage_biz.go create mode 100644 cmd/hc-service/service/sync/tcloud/security_group_usage_biz.go diff --git a/cmd/hc-service/logics/res-sync/aws/client.go b/cmd/hc-service/logics/res-sync/aws/client.go index bf8de751e1..b47f0411a2 100644 --- a/cmd/hc-service/logics/res-sync/aws/client.go +++ b/cmd/hc-service/logics/res-sync/aws/client.go @@ -46,6 +46,7 @@ type Interface interface { SecurityGroup(kt *kit.Kit, params *SyncBaseParams, opt *SyncSGOption) (*SyncResult, error) RemoveSecurityGroupDeleteFromCloud(kt *kit.Kit, accountID string, region string) error + SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error Subnet(kt *kit.Kit, params *SyncBaseParams, opt *SyncSubnetOption) (*SyncResult, error) RemoveSubnetDeleteFromCloud(kt *kit.Kit, accountID string, region string) error diff --git a/cmd/hc-service/logics/res-sync/aws/security_group_usage_biz.go b/cmd/hc-service/logics/res-sync/aws/security_group_usage_biz.go new file mode 100644 index 0000000000..16ee286a27 --- /dev/null +++ b/cmd/hc-service/logics/res-sync/aws/security_group_usage_biz.go @@ -0,0 +1,50 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package aws + +import ( + usagebizrelmgr "hcm/cmd/hc-service/logics/res-sync/usage-biz-rel" + cloudcore "hcm/pkg/api/core/cloud" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SecurityGroupUsageBiz 通过安全组关联资源的业务ID,更新安全组使用业务ID +func (cli *client) SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error { + + mgr := usagebizrelmgr.NewUsageBizRelManager(cli.dbCli) + + for i := range params.SGList { + sg := ¶ms.SGList[i] + err := mgr.SyncSecurityGroupUsageBiz(kt, sg) + if err != nil { + logs.Errorf("sync aws security group usage biz failed, err: %v, sg: %+v, rid: %s", err, sg, kt.Rid) + return err + } + } + return nil +} + +// SyncSGUsageBizParams 同步安全组使用业务参数,使用业务只依赖本地数据 +type SyncSGUsageBizParams struct { + AccountID string `json:"account_id" validate:"required"` + Region string `json:"region" validate:"required"` + SGList []cloudcore.BaseSecurityGroup +} diff --git a/cmd/hc-service/logics/res-sync/azure/client.go b/cmd/hc-service/logics/res-sync/azure/client.go index abe32f1828..7052f0a62a 100644 --- a/cmd/hc-service/logics/res-sync/azure/client.go +++ b/cmd/hc-service/logics/res-sync/azure/client.go @@ -44,6 +44,7 @@ type Interface interface { SecurityGroup(kt *kit.Kit, params *SyncBaseParams, opt *SyncSGOption) (*SyncResult, error) RemoveSecurityGroupDeleteFromCloud(kt *kit.Kit, accountID string, resGroupName string) error + SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error Subnet(kt *kit.Kit, params *SyncBaseParams, opt *SyncSubnetOption) (*SyncResult, error) RemoveSubnetDeleteFromCloud(kt *kit.Kit, accountID, resGroupName, cloudVpcID string) error diff --git a/cmd/hc-service/logics/res-sync/azure/security_group_usage_biz.go b/cmd/hc-service/logics/res-sync/azure/security_group_usage_biz.go new file mode 100644 index 0000000000..e737a5cc65 --- /dev/null +++ b/cmd/hc-service/logics/res-sync/azure/security_group_usage_biz.go @@ -0,0 +1,50 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package azure + +import ( + usagebizrelmgr "hcm/cmd/hc-service/logics/res-sync/usage-biz-rel" + cloudcore "hcm/pkg/api/core/cloud" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SecurityGroupUsageBiz 通过安全组关联资源的业务ID,更新安全组使用业务ID +func (cli *client) SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error { + + mgr := usagebizrelmgr.NewUsageBizRelManager(cli.dbCli) + + for i := range params.SGList { + sg := ¶ms.SGList[i] + err := mgr.SyncSecurityGroupUsageBiz(kt, sg) + if err != nil { + logs.Errorf("sync azure security group usage biz failed, err: %v, sg: %+v, rid: %s", err, sg, kt.Rid) + return err + } + } + return nil +} + +// SyncSGUsageBizParams 同步安全组使用业务参数,使用业务只依赖本地数据 +type SyncSGUsageBizParams struct { + AccountID string + ResourceGroupName string + SGList []cloudcore.BaseSecurityGroup +} diff --git a/cmd/hc-service/logics/res-sync/huawei/client.go b/cmd/hc-service/logics/res-sync/huawei/client.go index 3873f0e3f6..86408342d9 100644 --- a/cmd/hc-service/logics/res-sync/huawei/client.go +++ b/cmd/hc-service/logics/res-sync/huawei/client.go @@ -46,6 +46,7 @@ type Interface interface { SecurityGroup(kt *kit.Kit, params *SyncBaseParams, opt *SyncSGOption) (*SyncResult, error) RemoveSecurityGroupDeleteFromCloud(kt *kit.Kit, accountID string, region string) error + SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error Subnet(kt *kit.Kit, params *SyncBaseParams, opt *SyncSubnetOption) (*SyncResult, error) RemoveSubnetDeleteFromCloud(kt *kit.Kit, accountID, region, cloudVpcID string) error diff --git a/cmd/hc-service/logics/res-sync/huawei/security_group_usage_biz.go b/cmd/hc-service/logics/res-sync/huawei/security_group_usage_biz.go new file mode 100644 index 0000000000..c4f8868638 --- /dev/null +++ b/cmd/hc-service/logics/res-sync/huawei/security_group_usage_biz.go @@ -0,0 +1,50 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package huawei + +import ( + usagebizrelmgr "hcm/cmd/hc-service/logics/res-sync/usage-biz-rel" + cloudcore "hcm/pkg/api/core/cloud" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SecurityGroupUsageBiz 通过安全组关联资源的业务ID,更新安全组使用业务ID +func (cli *client) SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error { + + mgr := usagebizrelmgr.NewUsageBizRelManager(cli.dbCli) + + for i := range params.SGList { + sg := ¶ms.SGList[i] + err := mgr.SyncSecurityGroupUsageBiz(kt, sg) + if err != nil { + logs.Errorf("sync huawei security group usage biz failed, err: %v, sg: %+v, rid: %s", err, sg, kt.Rid) + return err + } + } + return nil +} + +// SyncSGUsageBizParams 同步安全组使用业务参数,使用业务只依赖本地数据 +type SyncSGUsageBizParams struct { + AccountID string `json:"account_id" validate:"required"` + Region string `json:"region" validate:"required"` + SGList []cloudcore.BaseSecurityGroup +} diff --git a/cmd/hc-service/logics/res-sync/tcloud/client.go b/cmd/hc-service/logics/res-sync/tcloud/client.go index 189a16edac..e79be3a763 100644 --- a/cmd/hc-service/logics/res-sync/tcloud/client.go +++ b/cmd/hc-service/logics/res-sync/tcloud/client.go @@ -46,6 +46,7 @@ type Interface interface { RemoveSecurityGroupDeleteFromCloud(kt *kit.Kit, accountID string, region string) error RemoveSecurityGroupDeleteFromCloudV2(kt *kit.Kit, accountID string, region string, allCloudIDMap map[string]struct{}) error + SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error Subnet(kt *kit.Kit, params *SyncBaseParams, opt *SyncSubnetOption) (*SyncResult, error) RemoveSubnetDeleteFromCloud(kt *kit.Kit, accountID string, region string) error diff --git a/cmd/hc-service/logics/res-sync/tcloud/load_balancer_security_group.go b/cmd/hc-service/logics/res-sync/tcloud/load_balancer_security_group.go index a65ba90081..6fa2307d00 100644 --- a/cmd/hc-service/logics/res-sync/tcloud/load_balancer_security_group.go +++ b/cmd/hc-service/logics/res-sync/tcloud/load_balancer_security_group.go @@ -25,10 +25,10 @@ import ( "hcm/cmd/hc-service/logics/res-sync/common" typeslb "hcm/pkg/adaptor/types/load-balancer" "hcm/pkg/api/core" - "hcm/pkg/api/core/cloud" corelb "hcm/pkg/api/core/cloud/load-balancer" dataservice "hcm/pkg/api/data-service" protocloud "hcm/pkg/api/data-service/cloud" + "hcm/pkg/criteria/constant" "hcm/pkg/criteria/enumor" "hcm/pkg/dal/dao/tools" "hcm/pkg/kit" @@ -183,20 +183,35 @@ func (cli *client) getCloudLbSgBinding(kt *kit.Kit, params *SyncBaseParams, opt if len(allSgCloudIDs) == 0 { return make(map[string]string), make(map[string][]string), nil } - // 2. 获取本地id 映射 - sgReq := &protocloud.SecurityGroupListReq{ - Field: []string{"id", "cloud_id"}, - Filter: tools.ExpressionAnd(tools.RuleIn("cloud_id", allSgCloudIDs)), - Page: core.NewDefaultBasePage(), - } - sgResp, err := cli.dbCli.Global.SecurityGroup.ListSecurityGroup(kt.Ctx, kt.Header(), sgReq) + allSgCloudIDs = slice.Unique(allSgCloudIDs) + cloudSgMap, err := cli.getSGCloudIDToLocalIDMap(kt, allSgCloudIDs) if err != nil { - logs.Errorf("fail to get sg list, err: %v, rid: %s", err, kt.Rid) return nil, nil, err } - // cloudID->localID - cloudSgMap := cvt.SliceToMap(sgResp.Details, func(sg cloud.BaseSecurityGroup) (string, string) { - return sg.CloudID, sg.ID - }) return cloudSgMap, lbSgCloudMap, nil } + +func (cli *client) getSGCloudIDToLocalIDMap(kt *kit.Kit, allSgCloudIDs []string) (map[string]string, error) { + // cloudID->localID + cloudSgMap := make(map[string]string, len(allSgCloudIDs)) + for _, idxBatch := range slice.Split(allSgCloudIDs, constant.BatchOperationMaxLimit) { + sgReq := &protocloud.SecurityGroupListReq{ + Field: []string{"id", "cloud_id"}, + Filter: tools.ExpressionAnd(tools.RuleIn("cloud_id", idxBatch)), + Page: core.NewDefaultBasePage(), + } + sgResp, err := cli.dbCli.Global.SecurityGroup.ListSecurityGroup(kt.Ctx, kt.Header(), sgReq) + if err != nil { + logs.Errorf("fail to get sg list, err: %v, sg ids: %s rid: %s", err, idxBatch, kt.Rid) + return nil, err + } + + for i := range sgResp.Details { + sgCloudID := sgResp.Details[i].CloudID + sgLocalID := sgResp.Details[i].ID + cloudSgMap[sgCloudID] = sgLocalID + } + } + + return cloudSgMap, nil +} diff --git a/cmd/hc-service/logics/res-sync/tcloud/security_group_usage_biz.go b/cmd/hc-service/logics/res-sync/tcloud/security_group_usage_biz.go new file mode 100644 index 0000000000..81981be259 --- /dev/null +++ b/cmd/hc-service/logics/res-sync/tcloud/security_group_usage_biz.go @@ -0,0 +1,50 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package tcloud + +import ( + usagebizrelmgr "hcm/cmd/hc-service/logics/res-sync/usage-biz-rel" + cloudcore "hcm/pkg/api/core/cloud" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SecurityGroupUsageBiz 通过安全组关联资源的业务ID,更新安全组使用业务ID +func (cli *client) SecurityGroupUsageBiz(kt *kit.Kit, params *SyncSGUsageBizParams) error { + + mgr := usagebizrelmgr.NewUsageBizRelManager(cli.dbCli) + + for i := range params.SGList { + sg := ¶ms.SGList[i] + err := mgr.SyncSecurityGroupUsageBiz(kt, sg) + if err != nil { + logs.Errorf("sync security group usage biz failed, err: %v, sg: %+v, rid: %s", err, sg, kt.Rid) + return err + } + } + return nil +} + +// SyncSGUsageBizParams 同步安全组使用业务参数,使用业务只依赖本地数据 +type SyncSGUsageBizParams struct { + AccountID string `json:"account_id" validate:"required"` + Region string `json:"region" validate:"required"` + SGList []cloudcore.BaseSecurityGroup +} diff --git a/cmd/hc-service/logics/res-sync/usage-biz-rel/sg_usage_biz_rel_manager.go b/cmd/hc-service/logics/res-sync/usage-biz-rel/sg_usage_biz_rel_manager.go new file mode 100644 index 0000000000..8502e7e061 --- /dev/null +++ b/cmd/hc-service/logics/res-sync/usage-biz-rel/sg_usage_biz_rel_manager.go @@ -0,0 +1,147 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package usagebizrelmgr + +import ( + "slices" + "sort" + + "hcm/pkg/api/core" + cloudcore "hcm/pkg/api/core/cloud" + protocloud "hcm/pkg/api/data-service/cloud" + dataservice "hcm/pkg/client/data-service" + "hcm/pkg/criteria/constant" + "hcm/pkg/criteria/enumor" + "hcm/pkg/dal/dao/tools" + "hcm/pkg/kit" + "hcm/pkg/logs" + cvt "hcm/pkg/tools/converter" + "hcm/pkg/tools/slice" +) + +// UsageBizRelManager 安全组使用业务关联管理 +type UsageBizRelManager struct { + dbCli *dataservice.Client +} + +// NewUsageBizRelManager ... +func NewUsageBizRelManager(dbCli *dataservice.Client) *UsageBizRelManager { + return &UsageBizRelManager{ + dbCli: dbCli, + } +} + +// SyncSecurityGroupUsageBiz 同步安全组使用业务 +func (mgr *UsageBizRelManager) SyncSecurityGroupUsageBiz(kt *kit.Kit, sg *cloudcore.BaseSecurityGroup) error { + if slice.IsItemInSlice(sg.UsageBizIDs, -1) { + // 已经是最大范围了 + return nil + } + bizIDList, err := mgr.querySGIDMap(kt, sg) + if err != nil { + return err + } + // 要求顺序一致 + if slices.Compare(sg.UsageBizIDs, bizIDList) == 0 { + // no change + return nil + } + req := &protocloud.ResUsageBizRelUpdateReq{ + UsageBizIDs: bizIDList, + ResCloudID: sg.CloudID, + ResVendor: sg.Vendor, + } + err = mgr.dbCli.Global.ResUsageBizRel.SetBizRels(kt, enumor.SecurityGroupCloudResType, sg.ID, req) + if err != nil { + logs.Errorf("update res usage biz rel failed, err: %v, rid: %s", err, kt.Rid) + return err + } + return nil +} + +func (mgr *UsageBizRelManager) querySGIDMap(kt *kit.Kit, sg *cloudcore.BaseSecurityGroup) ( + []int64, error) { + + var usageBizResCountMap = make(map[int64]int, len(sg.UsageBizIDs)) + for i := range sg.UsageBizIDs { + usageBizResCountMap[sg.UsageBizIDs[i]] = 0 + } + + // 1. 查询当前实际的安全组关联使用业务id + relReq := &core.ListReq{ + Filter: tools.EqualExpression("security_group_id", sg.ID), + Page: core.NewDefaultBasePage(), + Fields: []string{"res_type", "res_id"}, + } + resTypeIDMap := make(map[enumor.CloudResourceType][]string) + for { + resRelResp, err := mgr.dbCli.Global.SGCommonRel.ListSgCommonRels(kt, relReq) + // 2. 查询当前实际的业务 + if err != nil { + logs.Errorf("list sg common rel failed, err: %v, rid: %s", err, kt.Rid) + return nil, err + } + for i := range resRelResp.Details { + detail := resRelResp.Details[i] + resTypeIDMap[detail.ResType] = append(resTypeIDMap[detail.ResType], detail.ResID) + } + if len(resRelResp.Details) < int(relReq.Page.Limit) { + break + } + relReq.Page.Start += uint32(relReq.Page.Limit) + } + for resType := range resTypeIDMap { + resIDs := resTypeIDMap[resType] + for _, resIDBatch := range slice.Split(resIDs, constant.BatchOperationMaxLimit) { + // 查询资源的业务id + basicInfoReq := protocloud.ListResourceBasicInfoReq{ + ResourceType: resType, + IDs: resIDBatch, + Fields: []string{"id", "bk_biz_id"}, + } + basicInfo, err := mgr.dbCli.Global.Cloud.ListResBasicInfo(kt, basicInfoReq) + if err != nil { + logs.Errorf("list res basic info failed, err: %v, rid: %s", err, kt.Rid) + return nil, err + } + for resID := range basicInfo { + if basicInfo[resID].BkBizID <= 0 { + // 跳过无效业务id + continue + } + usageBizResCountMap[basicInfo[resID].BkBizID] += 1 + } + } + } + + bizList := cvt.MapKeyToSlice(usageBizResCountMap) + // 按资源数量排序 + sort.Slice(bizList, func(i, j int) bool { + return usageBizResCountMap[bizList[i]] > usageBizResCountMap[bizList[j]] + }) + if sg.MgmtBizID > 0 { + // 保证管理业务id在第一位 + newBizList := make([]int64, 0, len(bizList)+1) + newBizList[0] = sg.MgmtBizID + copy(newBizList[1:], bizList) + bizList = newBizList + } + return bizList, nil +} diff --git a/cmd/hc-service/service/sync/aws/security_group_usage_biz.go b/cmd/hc-service/service/sync/aws/security_group_usage_biz.go new file mode 100644 index 0000000000..255c9a9abd --- /dev/null +++ b/cmd/hc-service/service/sync/aws/security_group_usage_biz.go @@ -0,0 +1,122 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package aws + +import ( + "hcm/cmd/hc-service/logics/res-sync/aws" + "hcm/pkg/api/core" + corecloud "hcm/pkg/api/core/cloud" + "hcm/pkg/api/data-service/cloud" + "hcm/pkg/api/hc-service/sync" + dataservice "hcm/pkg/client/data-service" + "hcm/pkg/criteria/errf" + "hcm/pkg/dal/dao/tools" + "hcm/pkg/kit" + "hcm/pkg/logs" + "hcm/pkg/rest" +) + +// SyncSecurityGroupUsageBiz 同步安全组使用业务id列表,这里的同步和云上资源同步不同,不完全适配ResourceSync的机制(非云资源) +func (svc *service) SyncSecurityGroupUsageBiz(cts *rest.Contexts) (interface{}, error) { + + req := new(sync.AwsSyncReq) + if err := cts.DecodeInto(req); err != nil { + return nil, errf.NewFromErr(errf.DecodeRequestFailed, err) + } + + if err := req.Validate(); err != nil { + return nil, errf.NewFromErr(errf.InvalidParameter, err) + } + syncCli, err := svc.syncCli.Aws(cts.Kit, req.AccountID) + if err != nil { + logs.Errorf("init aws sync client failed for account %s, err: %v, rid: %s", req.AccountID, err, cts.Kit.Rid) + return nil, err + } + hd := &SGUsageBizRelSyncHandler{ + accountID: req.AccountID, + region: req.Region, + offset: 0, + dbCli: svc.dataCli, + syncCli: syncCli, + } + + return nil, hd.Sync(cts.Kit) +} + +// SGUsageBizRelSyncHandler ... +type SGUsageBizRelSyncHandler struct { + accountID string + region string + offset uint + dbCli *dataservice.Client + syncCli aws.Interface +} + +// Sync 同步安全组使用业务id列表 +func (h *SGUsageBizRelSyncHandler) Sync(kt *kit.Kit) error { + for { + + sgList, err := h.nextSGID(kt) + if err != nil { + logs.Errorf("list security group for sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + if len(sgList) == 0 { + break + } + if err := h.syncUsageBizs(kt, sgList); err != nil { + logs.Errorf("sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + } + return nil +} +func (h *SGUsageBizRelSyncHandler) nextSGID(kt *kit.Kit) ([]corecloud.BaseSecurityGroup, error) { + + listReq := &cloud.SecurityGroupListReq{ + Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids"}, + Filter: tools.ExpressionAnd( + tools.RuleEqual("account_id", h.accountID), + tools.RuleEqual("region", h.region), + ), + Page: &core.BasePage{ + Start: uint32(h.offset), + Limit: core.DefaultMaxPageLimit, + }, + } + sgResp, err := h.dbCli.Global.SecurityGroup.ListSecurityGroup(kt.Ctx, kt.Header(), listReq) + if err != nil { + logs.Errorf("list sg of Aws %s at %s failed, err: %v, rid: %s", err, h.accountID, h.region, kt.Rid) + return nil, err + } + + h.offset += uint(len(sgResp.Details)) + + return sgResp.Details, nil +} + +func (h *SGUsageBizRelSyncHandler) syncUsageBizs(kt *kit.Kit, sgList []corecloud.BaseSecurityGroup) error { + params := &aws.SyncSGUsageBizParams{ + AccountID: h.accountID, + Region: h.region, + SGList: sgList, + } + return h.syncCli.SecurityGroupUsageBiz(kt, params) +} diff --git a/cmd/hc-service/service/sync/azure/security_group_usage_biz.go b/cmd/hc-service/service/sync/azure/security_group_usage_biz.go new file mode 100644 index 0000000000..def37bf6b4 --- /dev/null +++ b/cmd/hc-service/service/sync/azure/security_group_usage_biz.go @@ -0,0 +1,122 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package azure + +import ( + "hcm/cmd/hc-service/logics/res-sync/azure" + "hcm/pkg/api/core" + corecloud "hcm/pkg/api/core/cloud" + "hcm/pkg/api/data-service/cloud" + "hcm/pkg/api/hc-service/sync" + dataservice "hcm/pkg/client/data-service" + "hcm/pkg/criteria/errf" + "hcm/pkg/dal/dao/tools" + "hcm/pkg/kit" + "hcm/pkg/logs" + "hcm/pkg/rest" +) + +// SyncSecurityGroupUsageBiz 同步安全组使用业务id列表,这里的同步和云上资源同步不同,不完全适配ResourceSync的机制(非云资源) +func (svc *service) SyncSecurityGroupUsageBiz(cts *rest.Contexts) (interface{}, error) { + + req := new(sync.AzureSyncReq) + if err := cts.DecodeInto(req); err != nil { + return nil, errf.NewFromErr(errf.DecodeRequestFailed, err) + } + + if err := req.Validate(); err != nil { + return nil, errf.NewFromErr(errf.InvalidParameter, err) + } + syncCli, err := svc.syncCli.Azure(cts.Kit, req.AccountID) + if err != nil { + logs.Errorf("init Azure sync client failed for account %s, err: %v, rid: %s", req.AccountID, err, cts.Kit.Rid) + return nil, err + } + hd := &SGUsageBizRelSyncHandler{ + accountID: req.AccountID, + resourceGroupName: req.ResourceGroupName, + offset: 0, + dbCli: svc.dataCli, + syncCli: syncCli, + } + + return nil, hd.Sync(cts.Kit) +} + +// SGUsageBizRelSyncHandler ... +type SGUsageBizRelSyncHandler struct { + accountID string + resourceGroupName string + offset uint + dbCli *dataservice.Client + syncCli azure.Interface +} + +// Sync 同步安全组使用业务id列表 +func (h *SGUsageBizRelSyncHandler) Sync(kt *kit.Kit) error { + for { + + sgList, err := h.nextSGID(kt) + if err != nil { + logs.Errorf("list Azure security group for sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + if len(sgList) == 0 { + break + } + if err := h.syncUsageBizs(kt, sgList); err != nil { + logs.Errorf("sync Azure usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + } + return nil +} +func (h *SGUsageBizRelSyncHandler) nextSGID(kt *kit.Kit) ([]corecloud.BaseSecurityGroup, error) { + + listReq := &cloud.SecurityGroupListReq{ + Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids"}, + Filter: tools.ExpressionAnd( + tools.RuleEqual("account_id", h.accountID), + tools.RuleEqual("extension.resource_group_name", h.resourceGroupName), + ), + Page: &core.BasePage{ + Start: uint32(h.offset), + Limit: core.DefaultMaxPageLimit, + }, + } + sgResp, err := h.dbCli.Global.SecurityGroup.ListSecurityGroup(kt.Ctx, kt.Header(), listReq) + if err != nil { + logs.Errorf("list sg of Azure %s at %s failed, err: %v, rid: %s", err, h.accountID, h.resourceGroupName, kt.Rid) + return nil, err + } + + h.offset += uint(len(sgResp.Details)) + + return sgResp.Details, nil +} + +func (h *SGUsageBizRelSyncHandler) syncUsageBizs(kt *kit.Kit, sgList []corecloud.BaseSecurityGroup) error { + params := &azure.SyncSGUsageBizParams{ + AccountID: h.accountID, + ResourceGroupName: h.resourceGroupName, + SGList: sgList, + } + return h.syncCli.SecurityGroupUsageBiz(kt, params) +} diff --git a/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go b/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go new file mode 100644 index 0000000000..a9e3b9d633 --- /dev/null +++ b/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go @@ -0,0 +1,122 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package huawei + +import ( + "hcm/cmd/hc-service/logics/res-sync/huawei" + "hcm/pkg/api/core" + corecloud "hcm/pkg/api/core/cloud" + "hcm/pkg/api/data-service/cloud" + "hcm/pkg/api/hc-service/sync" + dataservice "hcm/pkg/client/data-service" + "hcm/pkg/criteria/errf" + "hcm/pkg/dal/dao/tools" + "hcm/pkg/kit" + "hcm/pkg/logs" + "hcm/pkg/rest" +) + +// SyncSecurityGroupUsageBiz 同步安全组使用业务id列表,这里的同步和云上资源同步不同,不完全适配ResourceSync的机制(非云资源) +func (svc *service) SyncSecurityGroupUsageBiz(cts *rest.Contexts) (interface{}, error) { + + req := new(sync.HuaWeiSyncReq) + if err := cts.DecodeInto(req); err != nil { + return nil, errf.NewFromErr(errf.DecodeRequestFailed, err) + } + + if err := req.Validate(); err != nil { + return nil, errf.NewFromErr(errf.InvalidParameter, err) + } + syncCli, err := svc.syncCli.HuaWei(cts.Kit, req.AccountID) + if err != nil { + logs.Errorf("init huawei sync client failed for account %s, err: %v, rid: %s", req.AccountID, err, cts.Kit.Rid) + return nil, err + } + hd := &SGUsageBizRelSyncHandler{ + accountID: req.AccountID, + region: req.Region, + offset: 0, + dbCli: svc.dataCli, + syncCli: syncCli, + } + + return nil, hd.Sync(cts.Kit) +} + +// SGUsageBizRelSyncHandler ... +type SGUsageBizRelSyncHandler struct { + accountID string + region string + offset uint + dbCli *dataservice.Client + syncCli huawei.Interface +} + +// Sync 同步安全组使用业务id列表 +func (h *SGUsageBizRelSyncHandler) Sync(kt *kit.Kit) error { + for { + + sgList, err := h.nextSGID(kt) + if err != nil { + logs.Errorf("list security group for sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + if len(sgList) == 0 { + break + } + if err := h.syncUsageBizs(kt, sgList); err != nil { + logs.Errorf("sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + } + return nil +} +func (h *SGUsageBizRelSyncHandler) nextSGID(kt *kit.Kit) ([]corecloud.BaseSecurityGroup, error) { + + listReq := &cloud.SecurityGroupListReq{ + Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids"}, + Filter: tools.ExpressionAnd( + tools.RuleEqual("account_id", h.accountID), + tools.RuleEqual("region", h.region), + ), + Page: &core.BasePage{ + Start: uint32(h.offset), + Limit: core.DefaultMaxPageLimit, + }, + } + sgResp, err := h.dbCli.Global.SecurityGroup.ListSecurityGroup(kt.Ctx, kt.Header(), listReq) + if err != nil { + logs.Errorf("list sg of Huawei %s at %s failed, err: %v, rid: %s", err, h.accountID, h.region, kt.Rid) + return nil, err + } + + h.offset += uint(len(sgResp.Details)) + + return sgResp.Details, nil +} + +func (h *SGUsageBizRelSyncHandler) syncUsageBizs(kt *kit.Kit, sgList []corecloud.BaseSecurityGroup) error { + params := &huawei.SyncSGUsageBizParams{ + AccountID: h.accountID, + Region: h.region, + SGList: sgList, + } + return h.syncCli.SecurityGroupUsageBiz(kt, params) +} diff --git a/cmd/hc-service/service/sync/tcloud/security_group_usage_biz.go b/cmd/hc-service/service/sync/tcloud/security_group_usage_biz.go new file mode 100644 index 0000000000..9b7c57734d --- /dev/null +++ b/cmd/hc-service/service/sync/tcloud/security_group_usage_biz.go @@ -0,0 +1,122 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package tcloud + +import ( + "hcm/cmd/hc-service/logics/res-sync/tcloud" + "hcm/pkg/api/core" + corecloud "hcm/pkg/api/core/cloud" + "hcm/pkg/api/data-service/cloud" + "hcm/pkg/api/hc-service/sync" + dataservice "hcm/pkg/client/data-service" + "hcm/pkg/criteria/errf" + "hcm/pkg/dal/dao/tools" + "hcm/pkg/kit" + "hcm/pkg/logs" + "hcm/pkg/rest" +) + +// SyncSecurityGroupUsageBiz 同步安全组使用业务id列表,这里的同步和云上资源同步不同,不完全适配ResourceSync的机制(非云资源) +func (svc *service) SyncSecurityGroupUsageBiz(cts *rest.Contexts) (interface{}, error) { + + req := new(sync.TCloudSyncReq) + if err := cts.DecodeInto(req); err != nil { + return nil, errf.NewFromErr(errf.DecodeRequestFailed, err) + } + + if err := req.Validate(); err != nil { + return nil, errf.NewFromErr(errf.InvalidParameter, err) + } + syncCli, err := svc.syncCli.TCloud(cts.Kit, req.AccountID) + if err != nil { + logs.Errorf("init tcloud sync client failed for account %s, err: %v, rid: %s", req.AccountID, err, cts.Kit.Rid) + return nil, err + } + hd := &SGUsageBizRelSyncHandler{ + accountID: req.AccountID, + region: req.Region, + offset: 0, + dbCli: svc.dataCli, + syncCli: syncCli, + } + + return nil, hd.Sync(cts.Kit) +} + +// SGUsageBizRelSyncHandler ... +type SGUsageBizRelSyncHandler struct { + accountID string + region string + offset uint + dbCli *dataservice.Client + syncCli tcloud.Interface +} + +// Sync 同步安全组使用业务id列表 +func (h *SGUsageBizRelSyncHandler) Sync(kt *kit.Kit) error { + for { + + sgList, err := h.nextSGID(kt) + if err != nil { + logs.Errorf("list security group for sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + if len(sgList) == 0 { + break + } + if err := h.syncUsageBizs(kt, sgList); err != nil { + logs.Errorf("sync usage bizs failed, err: %v, rid: %s", err, kt.Rid) + return err + } + } + return nil +} +func (h *SGUsageBizRelSyncHandler) nextSGID(kt *kit.Kit) ([]corecloud.BaseSecurityGroup, error) { + + listReq := &cloud.SecurityGroupListReq{ + Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids"}, + Filter: tools.ExpressionAnd( + tools.RuleEqual("account_id", h.accountID), + tools.RuleEqual("region", h.region), + ), + Page: &core.BasePage{ + Start: uint32(h.offset), + Limit: core.DefaultMaxPageLimit, + }, + } + sgResp, err := h.dbCli.Global.SecurityGroup.ListSecurityGroup(kt.Ctx, kt.Header(), listReq) + if err != nil { + logs.Errorf("list sg of TCLoud %s at %s failed, err: %v, rid: %s", err, h.accountID, h.region, kt.Rid) + return nil, err + } + + h.offset += uint(len(sgResp.Details)) + + return sgResp.Details, nil +} + +func (h *SGUsageBizRelSyncHandler) syncUsageBizs(kt *kit.Kit, sgList []corecloud.BaseSecurityGroup) error { + params := &tcloud.SyncSGUsageBizParams{ + AccountID: h.accountID, + Region: h.region, + SGList: sgList, + } + return h.syncCli.SecurityGroupUsageBiz(kt, params) +} diff --git a/pkg/api/core/cloud/security_group.go b/pkg/api/core/cloud/security_group.go index c8eb81460e..527cefd774 100644 --- a/pkg/api/core/cloud/security_group.go +++ b/pkg/api/core/cloud/security_group.go @@ -48,6 +48,11 @@ type BaseSecurityGroup struct { UpdatedAt string `json:"updated_at"` } +// GetID return id +func (sg BaseSecurityGroup) GetID() string { + return sg.ID +} + // SecurityGroup define security group type SecurityGroup[Extension SecurityGroupExtension] struct { BaseSecurityGroup `json:",inline"` From 3ffb3b47b5c45f4cfc5c1586c22aa031fbc3623f Mon Sep 17 00:00:00 2001 From: chenjr15 Date: Wed, 22 Jan 2025 15:21:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20cloud=20server=E5=B1=82=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E5=85=B3=E8=81=94=E8=B5=84=E6=BA=90=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sync/aws/security_group_usage_biz_rel.go | 70 +++++++++++++ .../service/sync/aws/sync_all_resource.go | 3 + .../azure/security_group_usage_biz_rel.go | 93 +++++++++++++++++ .../service/sync/azure/sync_all_resource.go | 4 + .../huawei/security_group_usage_biz_rel.go | 99 +++++++++++++++++++ .../service/sync/huawei/sync_all_resource.go | 4 +- .../sync/tcloud/security_group_biz_rel.go | 72 ++++++++++++++ .../service/sync/tcloud/sync_all_resource.go | 24 ++--- cmd/hc-service/service/sync/aws/service.go | 1 + .../sync/azure/security_group_usage_biz.go | 2 +- cmd/hc-service/service/sync/azure/service.go | 1 + .../sync/huawei/security_group_usage_biz.go | 2 +- cmd/hc-service/service/sync/huawei/service.go | 1 + cmd/hc-service/service/sync/tcloud/service.go | 1 + pkg/client/hc-service/aws/security_group.go | 7 ++ pkg/client/hc-service/azure/security_group.go | 7 ++ .../hc-service/huawei/security_group.go | 7 ++ .../hc-service/tcloud/security_group.go | 6 ++ pkg/criteria/enumor/cloud_resource_type.go | 2 + 19 files changed, 392 insertions(+), 14 deletions(-) create mode 100644 cmd/cloud-server/service/sync/aws/security_group_usage_biz_rel.go create mode 100644 cmd/cloud-server/service/sync/azure/security_group_usage_biz_rel.go create mode 100644 cmd/cloud-server/service/sync/huawei/security_group_usage_biz_rel.go create mode 100644 cmd/cloud-server/service/sync/tcloud/security_group_biz_rel.go diff --git a/cmd/cloud-server/service/sync/aws/security_group_usage_biz_rel.go b/cmd/cloud-server/service/sync/aws/security_group_usage_biz_rel.go new file mode 100644 index 0000000000..6ebe1141d1 --- /dev/null +++ b/cmd/cloud-server/service/sync/aws/security_group_usage_biz_rel.go @@ -0,0 +1,70 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package aws + +import ( + "time" + + "hcm/cmd/cloud-server/service/sync/detail" + "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client" + "hcm/pkg/criteria/enumor" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SyncSGUsageBizRel ... +func SyncSGUsageBizRel(kt *kit.Kit, cliSet *client.ClientSet, accountID string, regions []string, + sd *detail.SyncDetail) error { + + // 重新设置rid方便定位 + kt = kt.NewSubKit() + + start := time.Now() + logs.V(3).Infof("aws account[%s] sync sg usage biz rel start, time: %v, rid: %s", accountID, start, kt.Rid) + + // 同步中 + if err := sd.ResSyncStatusSyncing(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + defer func() { + logs.V(3).Infof("aws account[%s] sync sg usage biz rel end, cost: %v, rid: %s", + accountID, time.Since(start), kt.Rid) + }() + + for _, region := range regions { + req := &sync.AwsSyncReq{ + AccountID: accountID, + Region: region, + } + if err := cliSet.HCService().Aws.SecurityGroup.SyncSecurityGroupUsageBizRel(kt, req); err != nil { + logs.Errorf("sync aws sg usage biz rel failed, err: %v, req: %v, rid: %s", err, req, kt.Rid) + return err + } + } + + // 同步成功 + if err := sd.ResSyncStatusSuccess(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + return nil +} diff --git a/cmd/cloud-server/service/sync/aws/sync_all_resource.go b/cmd/cloud-server/service/sync/aws/sync_all_resource.go index 769e3872dc..132afb80ba 100644 --- a/cmd/cloud-server/service/sync/aws/sync_all_resource.go +++ b/cmd/cloud-server/service/sync/aws/sync_all_resource.go @@ -123,6 +123,9 @@ func SyncAllResource(kt *kit.Kit, cliSet *client.ClientSet, if hitErr = SyncRouteTable(kt, cliSet, opt.AccountID, regions, sd); hitErr != nil { return enumor.SubAccountCloudResType, hitErr } + if hitErr = SyncSGUsageBizRel(kt, cliSet, opt.AccountID, regions, sd); hitErr != nil { + return enumor.SecurityGroupUsageBizRelResType, hitErr + } return "", nil } diff --git a/cmd/cloud-server/service/sync/azure/security_group_usage_biz_rel.go b/cmd/cloud-server/service/sync/azure/security_group_usage_biz_rel.go new file mode 100644 index 0000000000..d864811b5d --- /dev/null +++ b/cmd/cloud-server/service/sync/azure/security_group_usage_biz_rel.go @@ -0,0 +1,93 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package azure + +import ( + gosync "sync" + "time" + + "hcm/cmd/cloud-server/service/sync/detail" + "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client" + "hcm/pkg/criteria/enumor" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SyncSGUsageBizRel ... +func SyncSGUsageBizRel(kt *kit.Kit, cliSet *client.ClientSet, accountID string, resourceGroupNames []string, + sd *detail.SyncDetail) error { + + // 重新设置rid方便定位 + kt = kt.NewSubKit() + + start := time.Now() + logs.V(3).Infof("azure account[%s] sync sg usage biz rel start, time: %v, rid: %s", accountID, start, kt.Rid) + + // 同步中 + if err := sd.ResSyncStatusSyncing(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + defer func() { + logs.V(3).Infof("azure account[%s] sync sg usage biz rel end, cost: %v, rid: %s", accountID, time.Since(start), + kt.Rid) + }() + + pipeline := make(chan bool, syncConcurrencyCount) + var firstErr error + var wg gosync.WaitGroup + for _, name := range resourceGroupNames { + pipeline <- true + wg.Add(1) + + go func(name string) { + defer func() { + wg.Done() + <-pipeline + }() + + req := &sync.AzureSyncReq{ + AccountID: accountID, + ResourceGroupName: name, + } + err := cliSet.HCService().Azure.SecurityGroup.SyncSecurityGroupUsageBizRel(kt, req) + if firstErr == nil && err != nil { + logs.Errorf("sync azure security group usage biz rel failed, err: %v, req: %v, rid: %s", + err, req, kt.Rid) + firstErr = err + return + } + }(name) + } + + wg.Wait() + + if firstErr != nil { + return firstErr + } + + // 同步成功 + if err := sd.ResSyncStatusSuccess(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + return nil +} diff --git a/cmd/cloud-server/service/sync/azure/sync_all_resource.go b/cmd/cloud-server/service/sync/azure/sync_all_resource.go index 9bde4c9b42..914c39106c 100644 --- a/cmd/cloud-server/service/sync/azure/sync_all_resource.go +++ b/cmd/cloud-server/service/sync/azure/sync_all_resource.go @@ -139,5 +139,9 @@ func SyncAllResource(kt *kit.Kit, cliSet *client.ClientSet, return enumor.SubAccountCloudResType, hitErr } + if hitErr = SyncSGUsageBizRel(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { + return enumor.SecurityGroupUsageBizRelResType, hitErr + } + return "", nil } diff --git a/cmd/cloud-server/service/sync/huawei/security_group_usage_biz_rel.go b/cmd/cloud-server/service/sync/huawei/security_group_usage_biz_rel.go new file mode 100644 index 0000000000..e1405d49e9 --- /dev/null +++ b/cmd/cloud-server/service/sync/huawei/security_group_usage_biz_rel.go @@ -0,0 +1,99 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package huawei + +import ( + gosync "sync" + "time" + + "hcm/cmd/cloud-server/service/sync/detail" + "hcm/pkg/adaptor/huawei" + "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client" + "hcm/pkg/criteria/enumor" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SyncSGUsageBizRel ... +func SyncSGUsageBizRel(kt *kit.Kit, cliSet *client.ClientSet, accountID string, sd *detail.SyncDetail) error { + + // 重新设置rid方便定位 + kt = kt.NewSubKit() + + start := time.Now() + logs.V(3).Infof("huawei account[%s] sync sg usage biz rel start, time: %v, rid: %s", accountID, start, kt.Rid) + + // 同步中 + if err := sd.ResSyncStatusSyncing(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + defer func() { + logs.V(3).Infof("huawei account[%s] sync sg usage biz rel end, cost: %v, rid: %s", + accountID, time.Since(start), kt.Rid) + }() + + regions, err := ListRegionByService(kt, cliSet.DataService(), huawei.Vpc) + if err != nil { + logs.Errorf("sync huawei list region failed, err: %v, rid: %s", err, kt.Rid) + return err + } + + pipeline := make(chan bool, syncConcurrencyCount) + var firstErr error + var wg gosync.WaitGroup + for _, region := range regions { + pipeline <- true + wg.Add(1) + + go func(region string) { + defer func() { + wg.Done() + <-pipeline + }() + + req := &sync.HuaWeiSyncReq{ + AccountID: accountID, + Region: region, + } + err = cliSet.HCService().HuaWei.SecurityGroup.SyncSecurityGroupUsageBizRel(kt, req) + if firstErr == nil && Error(err) != nil { + logs.Errorf("sync huawei security group usage biz rel failed, err: %v, req: %v, rid: %s", + err, req, kt.Rid) + firstErr = err + return + } + }(region) + } + + wg.Wait() + + if firstErr != nil { + return firstErr + } + + // 同步成功 + if err := sd.ResSyncStatusSuccess(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + return nil +} diff --git a/cmd/cloud-server/service/sync/huawei/sync_all_resource.go b/cmd/cloud-server/service/sync/huawei/sync_all_resource.go index e2871d0096..b7d098113a 100644 --- a/cmd/cloud-server/service/sync/huawei/sync_all_resource.go +++ b/cmd/cloud-server/service/sync/huawei/sync_all_resource.go @@ -117,6 +117,8 @@ func SyncAllResource(kt *kit.Kit, cliSet *client.ClientSet, if hitErr = SyncSubAccount(kt, cliSet, opt.AccountID, sd); hitErr != nil { return enumor.SubAccountCloudResType, hitErr } - + if hitErr = SyncSGUsageBizRel(kt, cliSet, opt.AccountID, sd); hitErr != nil { + return enumor.SecurityGroupUsageBizRelResType, hitErr + } return "", nil } diff --git a/cmd/cloud-server/service/sync/tcloud/security_group_biz_rel.go b/cmd/cloud-server/service/sync/tcloud/security_group_biz_rel.go new file mode 100644 index 0000000000..914d655faf --- /dev/null +++ b/cmd/cloud-server/service/sync/tcloud/security_group_biz_rel.go @@ -0,0 +1,72 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2024 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package tcloud + +import ( + "time" + + "hcm/cmd/cloud-server/service/sync/detail" + "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client" + "hcm/pkg/criteria/enumor" + "hcm/pkg/kit" + "hcm/pkg/logs" +) + +// SyncSGUsageBizRel ... +func SyncSGUsageBizRel(kt *kit.Kit, cliSet *client.ClientSet, accountID string, regions []string, + sd *detail.SyncDetail) error { + + // 重新设置rid方便定位 + kt = kt.NewSubKit() + + start := time.Now() + logs.V(3).Infof("tcloud account[%s] sync sg usage biz rel start, time: %v, rid: %s", + accountID, start, kt.Rid) + + // 同步中 + if err := sd.ResSyncStatusSyncing(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + defer func() { + logs.V(3).Infof("tcloud account[%s] sync sg usage biz end, cost: %v, rid: %s", + accountID, time.Since(start), kt.Rid) + }() + + for _, region := range regions { + req := &sync.TCloudSyncReq{ + AccountID: accountID, + Region: region, + } + err := cliSet.HCService().TCloud.SecurityGroup.SyncSecurityGroupUsageBizRel(kt, req) + if err != nil { + logs.Errorf("sync tcloud sg usage biz rel failed, err: %v, req: %v, rid: %s", err, req, kt.Rid) + return err + } + } + + // 同步成功 + if err := sd.ResSyncStatusSuccess(enumor.SecurityGroupUsageBizRelResType); err != nil { + return err + } + + return nil +} diff --git a/cmd/cloud-server/service/sync/tcloud/sync_all_resource.go b/cmd/cloud-server/service/sync/tcloud/sync_all_resource.go index 3377d98164..734044640c 100644 --- a/cmd/cloud-server/service/sync/tcloud/sync_all_resource.go +++ b/cmd/cloud-server/service/sync/tcloud/sync_all_resource.go @@ -91,17 +91,18 @@ func SyncAllResource(kt *kit.Kit, cliSet *client.ClientSet, } syncFuncMap := map[enumor.CloudResourceType]ResSyncFunc{ - enumor.DiskCloudResType: SyncDisk, - enumor.VpcCloudResType: SyncVpc, - enumor.SubnetCloudResType: SyncSubnet, - enumor.EipCloudResType: SyncEip, - enumor.ArgumentTemplateResType: SyncArgsTpl, - enumor.SecurityGroupCloudResType: SyncSG, - enumor.CvmCloudResType: SyncCvm, - enumor.CertCloudResType: SyncCert, - enumor.LoadBalancerCloudResType: SyncLoadBalancer, - enumor.RouteTableCloudResType: SyncRouteTable, - enumor.SubAccountCloudResType: SyncSubAccount, + enumor.DiskCloudResType: SyncDisk, + enumor.VpcCloudResType: SyncVpc, + enumor.SubnetCloudResType: SyncSubnet, + enumor.EipCloudResType: SyncEip, + enumor.ArgumentTemplateResType: SyncArgsTpl, + enumor.SecurityGroupCloudResType: SyncSG, + enumor.CvmCloudResType: SyncCvm, + enumor.CertCloudResType: SyncCert, + enumor.LoadBalancerCloudResType: SyncLoadBalancer, + enumor.RouteTableCloudResType: SyncRouteTable, + enumor.SubAccountCloudResType: SyncSubAccount, + enumor.SecurityGroupUsageBizRelResType: SyncSGUsageBizRel, } for _, resType := range getSyncOrder() { @@ -126,5 +127,6 @@ func getSyncOrder() []enumor.CloudResourceType { enumor.LoadBalancerCloudResType, enumor.RouteTableCloudResType, enumor.SubAccountCloudResType, + enumor.SecurityGroupUsageBizRelResType, } } diff --git a/cmd/hc-service/service/sync/aws/service.go b/cmd/hc-service/service/sync/aws/service.go index c8bcb38293..f48343022b 100644 --- a/cmd/hc-service/service/sync/aws/service.go +++ b/cmd/hc-service/service/sync/aws/service.go @@ -44,6 +44,7 @@ func InitService(cap *capability.Capability) { h.Add("SyncSubnet", "POST", "/subnets/sync", v.SyncSubnet) h.Add("SyncDisk", "POST", "/disks/sync", v.SyncDisk) h.Add("SyncSecurityGroup", "POST", "/security_groups/sync", v.SyncSecurityGroup) + h.Add("SyncSecurityGroupUsageBiz", "POST", "/security_groups/usage_biz_rels/sync", v.SyncSecurityGroupUsageBiz) h.Add("SyncCvmWithRelRes", "POST", "/cvms/with/relation_resources/sync", v.SyncCvmWithRelRes) h.Add("SyncEip", "POST", "/eips/sync", v.SyncEip) h.Add("SyncRoute", "POST", "/route_tables/sync", v.SyncRouteTable) diff --git a/cmd/hc-service/service/sync/azure/security_group_usage_biz.go b/cmd/hc-service/service/sync/azure/security_group_usage_biz.go index def37bf6b4..d29fcc8f06 100644 --- a/cmd/hc-service/service/sync/azure/security_group_usage_biz.go +++ b/cmd/hc-service/service/sync/azure/security_group_usage_biz.go @@ -94,7 +94,7 @@ func (h *SGUsageBizRelSyncHandler) nextSGID(kt *kit.Kit) ([]corecloud.BaseSecuri Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids"}, Filter: tools.ExpressionAnd( tools.RuleEqual("account_id", h.accountID), - tools.RuleEqual("extension.resource_group_name", h.resourceGroupName), + tools.RuleJSONEqual("extension.resource_group_name", h.resourceGroupName), ), Page: &core.BasePage{ Start: uint32(h.offset), diff --git a/cmd/hc-service/service/sync/azure/service.go b/cmd/hc-service/service/sync/azure/service.go index 182c1aeff1..3079a5c95a 100644 --- a/cmd/hc-service/service/sync/azure/service.go +++ b/cmd/hc-service/service/sync/azure/service.go @@ -46,6 +46,7 @@ func InitService(cap *capability.Capability) { h.Add("SyncDisk", "POST", "/disks/sync", v.SyncDisk) h.Add("SyncCvmWithRelRes", "POST", "/cvms/with/relation_resources/sync", v.SyncCvmWithRelRes) h.Add("SyncSecurityGroup", "POST", "/security_groups/sync", v.SyncSecurityGroup) + h.Add("SyncSecurityGroupUsageBiz", "POST", "/security_groups/usage_biz_rels/sync", v.SyncSecurityGroupUsageBiz) h.Add("SyncNetworkInterface", "POST", "/network_interfaces/sync", v.SyncNetworkInterface) h.Add("SyncRoute", "POST", "/route_tables/sync", v.SyncRouteTable) h.Add("SyncResourceGroup", "POST", "/resource_groups/sync", v.SyncResourceGroup) diff --git a/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go b/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go index a9e3b9d633..ed5c890711 100644 --- a/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go +++ b/cmd/hc-service/service/sync/huawei/security_group_usage_biz.go @@ -91,7 +91,7 @@ func (h *SGUsageBizRelSyncHandler) Sync(kt *kit.Kit) error { func (h *SGUsageBizRelSyncHandler) nextSGID(kt *kit.Kit) ([]corecloud.BaseSecurityGroup, error) { listReq := &cloud.SecurityGroupListReq{ - Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids"}, + Field: []string{"id", "cloud_id", "region", "vendor", "usage_biz_ids", "bk_biz_id"}, Filter: tools.ExpressionAnd( tools.RuleEqual("account_id", h.accountID), tools.RuleEqual("region", h.region), diff --git a/cmd/hc-service/service/sync/huawei/service.go b/cmd/hc-service/service/sync/huawei/service.go index b849d8677f..c7df70a8bf 100644 --- a/cmd/hc-service/service/sync/huawei/service.go +++ b/cmd/hc-service/service/sync/huawei/service.go @@ -44,6 +44,7 @@ func InitService(cap *capability.Capability) { h.Add("SyncSubnet", "POST", "/subnets/sync", v.SyncSubnet) h.Add("SyncDisk", "POST", "/disks/sync", v.SyncDisk) h.Add("SyncSecurityGroup", "POST", "/security_groups/sync", v.SyncSecurityGroup) + h.Add("SyncSecurityGroupUsageBiz", "POST", "/security_groups/usage_biz_rels/sync", v.SyncSecurityGroupUsageBiz) h.Add("SyncCvmWithRelRes", "POST", "/cvms/with/relation_resources/sync", v.SyncCvmWithRelRes) h.Add("SyncEip", "POST", "/eips/sync", v.SyncEip) h.Add("SyncRoute", "POST", "/route_tables/sync", v.SyncRouteTable) diff --git a/cmd/hc-service/service/sync/tcloud/service.go b/cmd/hc-service/service/sync/tcloud/service.go index 9e8de00ca7..1296bb2184 100644 --- a/cmd/hc-service/service/sync/tcloud/service.go +++ b/cmd/hc-service/service/sync/tcloud/service.go @@ -45,6 +45,7 @@ func InitService(cap *capability.Capability) { h.Add("SyncDisk", "POST", "/disks/sync", v.SyncDisk) h.Add("SyncCvmWithRelRes", "POST", "/cvms/with/relation_resources/sync", v.SyncCvmWithRelRes) h.Add("SyncSecurityGroup", "POST", "/security_groups/sync", v.SyncSecurityGroup) + h.Add("SyncSecurityGroupUsageBiz", "POST", "/security_groups/usage_biz_rels/sync", v.SyncSecurityGroupUsageBiz) h.Add("SyncEip", "POST", "/eips/sync", v.SyncEip) h.Add("SyncRoute", "POST", "/route_tables/sync", v.SyncRouteTable) h.Add("SyncZone", "POST", "/zones/sync", v.SyncZone) diff --git a/pkg/client/hc-service/aws/security_group.go b/pkg/client/hc-service/aws/security_group.go index 3eef92469a..e1c8b8c019 100644 --- a/pkg/client/hc-service/aws/security_group.go +++ b/pkg/client/hc-service/aws/security_group.go @@ -26,6 +26,7 @@ import ( "hcm/pkg/api/core" proto "hcm/pkg/api/hc-service" "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client/common" "hcm/pkg/criteria/errf" "hcm/pkg/kit" "hcm/pkg/rest" @@ -230,3 +231,9 @@ func (cli *SecurityGroupClient) DisassociateCvm(ctx context.Context, h http.Head return nil } + +// SyncSecurityGroupUsageBizRel ... +func (cli *SecurityGroupClient) SyncSecurityGroupUsageBizRel(kt *kit.Kit, req *sync.AwsSyncReq) error { + return common.RequestNoResp[sync.AwsSyncReq](cli.client, rest.POST, kt, req, + "/security_groups/usage_biz_rels/sync") +} diff --git a/pkg/client/hc-service/azure/security_group.go b/pkg/client/hc-service/azure/security_group.go index fb041f3e2b..527b107c61 100644 --- a/pkg/client/hc-service/azure/security_group.go +++ b/pkg/client/hc-service/azure/security_group.go @@ -26,6 +26,7 @@ import ( "hcm/pkg/api/core" proto "hcm/pkg/api/hc-service" "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client/common" "hcm/pkg/criteria/errf" "hcm/pkg/kit" "hcm/pkg/rest" @@ -302,3 +303,9 @@ func (cli *SecurityGroupClient) DisassociateNetworkInterface(ctx context.Context return nil } + +// SyncSecurityGroupUsageBizRel ... +func (cli *SecurityGroupClient) SyncSecurityGroupUsageBizRel(kt *kit.Kit, req *sync.AzureSyncReq) error { + return common.RequestNoResp[sync.AzureSyncReq](cli.client, rest.POST, kt, req, + "/security_groups/usage_biz_rels/sync") +} diff --git a/pkg/client/hc-service/huawei/security_group.go b/pkg/client/hc-service/huawei/security_group.go index 70b9ce7689..aea073ff2a 100644 --- a/pkg/client/hc-service/huawei/security_group.go +++ b/pkg/client/hc-service/huawei/security_group.go @@ -26,6 +26,7 @@ import ( "hcm/pkg/api/core" proto "hcm/pkg/api/hc-service" "hcm/pkg/api/hc-service/sync" + "hcm/pkg/client/common" "hcm/pkg/criteria/errf" "hcm/pkg/kit" "hcm/pkg/rest" @@ -230,3 +231,9 @@ func (cli *SecurityGroupClient) DisassociateCvm(ctx context.Context, h http.Head return nil } + +// SyncSecurityGroupUsageBizRel ... +func (cli *SecurityGroupClient) SyncSecurityGroupUsageBizRel(kt *kit.Kit, req *sync.HuaWeiSyncReq) error { + return common.RequestNoResp[sync.HuaWeiSyncReq](cli.client, rest.POST, kt, req, + "/security_groups/usage_biz_rels/sync") +} diff --git a/pkg/client/hc-service/tcloud/security_group.go b/pkg/client/hc-service/tcloud/security_group.go index c842bbfa97..cb7a823f3d 100644 --- a/pkg/client/hc-service/tcloud/security_group.go +++ b/pkg/client/hc-service/tcloud/security_group.go @@ -334,3 +334,9 @@ func (cli *SecurityGroupClient) BatchDisassociateCloudCvm(kt *kit.Kit, sgID stri return common.RequestNoResp[proto.SecurityGroupBatchAssociateCvmReq](cli.client, rest.POST, kt, req, "/security_groups/disassociate/cvms/batch") } + +// SyncSecurityGroupUsageBizRel ... +func (cli *SecurityGroupClient) SyncSecurityGroupUsageBizRel(kt *kit.Kit, req *sync.TCloudSyncReq) error { + return common.RequestNoResp[sync.TCloudSyncReq](cli.client, rest.POST, kt, req, + "/security_groups/usage_biz_rels/sync") +} diff --git a/pkg/criteria/enumor/cloud_resource_type.go b/pkg/criteria/enumor/cloud_resource_type.go index ab412781c3..1b63c90e21 100644 --- a/pkg/criteria/enumor/cloud_resource_type.go +++ b/pkg/criteria/enumor/cloud_resource_type.go @@ -88,4 +88,6 @@ const ( ListenerCloudResType CloudResourceType = "listener" TargetGroupCloudResType CloudResourceType = "target_group" TCLoudUrlRuleCloudResType CloudResourceType = "tcloud_url_rule" + // SecurityGroupUsageBizRelResType 安全组使用业务关联关系 + SecurityGroupUsageBizRelResType CloudResourceType = "security_group_usage_biz_rel" ) From 9c86bcd1bfee91935f61efa8ddd0b491c2ea2fac Mon Sep 17 00:00:00 2001 From: chenjr15 Date: Wed, 22 Jan 2025 17:18:58 +0800 Subject: [PATCH 4/4] =?UTF-8?q?chore:=20=E5=8E=8B=E7=BC=A9=E8=A1=8C?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/sync/azure/sub_account.go | 3 +- .../service/sync/azure/sync_all_resource.go | 71 +++++++++---------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/cmd/cloud-server/service/sync/azure/sub_account.go b/cmd/cloud-server/service/sync/azure/sub_account.go index 36d21cf2bb..1aba614785 100644 --- a/cmd/cloud-server/service/sync/azure/sub_account.go +++ b/cmd/cloud-server/service/sync/azure/sub_account.go @@ -31,8 +31,7 @@ import ( ) // SyncSubAccount sync sub account -func SyncSubAccount(kt *kit.Kit, cliSet *client.ClientSet, accountID string, - sd *detail.SyncDetail) error { +func SyncSubAccount(kt *kit.Kit, cliSet *client.ClientSet, accountID string, _ []string, sd *detail.SyncDetail) error { // 重新设置rid方便定位 kt = kt.NewSubKit() diff --git a/cmd/cloud-server/service/sync/azure/sync_all_resource.go b/cmd/cloud-server/service/sync/azure/sync_all_resource.go index 914c39106c..e9dbbe7384 100644 --- a/cmd/cloud-server/service/sync/azure/sync_all_resource.go +++ b/cmd/cloud-server/service/sync/azure/sync_all_resource.go @@ -40,6 +40,10 @@ type SyncAllResourceOption struct { SyncPublicResource bool `json:"sync_public_resource" validate:"omitempty"` } +// ResSyncFunc sync resource func +type ResSyncFunc func(kt *kit.Kit, cliSet *client.ClientSet, accountID string, resourceGroupNames []string, + sd *detail.SyncDetail) error + // Validate SyncAllResourceOption func (opt *SyncAllResourceOption) Validate() error { return validator.Validate.Struct(opt) @@ -103,45 +107,36 @@ func SyncAllResource(kt *kit.Kit, cliSet *client.ClientSet, Vendor: string(enumor.Azure), } - if hitErr = SyncDisk(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.DiskCloudResType, hitErr - } - - if hitErr = SyncSG(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.SecurityGroupCloudResType, hitErr - } - - if hitErr = SyncVpc(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.VpcCloudResType, hitErr - } - - if hitErr = SyncSubnet(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.SubnetCloudResType, hitErr - } - - if hitErr = SyncEip(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.EipCloudResType, hitErr - } - - if hitErr = SyncCvm(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.CvmCloudResType, hitErr - } - - if hitErr = SyncRouteTable(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.RouteTableCloudResType, hitErr - } - - if hitErr = SyncNetworkInterface(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.NetworkInterfaceCloudResType, hitErr - } - - if hitErr = SyncSubAccount(kt, cliSet, opt.AccountID, sd); hitErr != nil { - return enumor.SubAccountCloudResType, hitErr - } - - if hitErr = SyncSGUsageBizRel(kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { - return enumor.SecurityGroupUsageBizRelResType, hitErr + for _, resType := range syncOrder { + if hitErr = syncFuncMap[resType](kt, cliSet, opt.AccountID, resourceGroupNames, sd); hitErr != nil { + return resType, hitErr + } } return "", nil } + +var syncOrder = []enumor.CloudResourceType{ + enumor.DiskCloudResType, + enumor.SecurityGroupCloudResType, + enumor.VpcCloudResType, + enumor.SubnetCloudResType, + enumor.EipCloudResType, + enumor.CvmCloudResType, + enumor.RouteTableCloudResType, + enumor.NetworkInterfaceCloudResType, + enumor.SubAccountCloudResType, + enumor.SecurityGroupUsageBizRelResType, +} +var syncFuncMap = map[enumor.CloudResourceType]ResSyncFunc{ + enumor.DiskCloudResType: SyncDisk, + enumor.VpcCloudResType: SyncVpc, + enumor.SubnetCloudResType: SyncSubnet, + enumor.EipCloudResType: SyncEip, + enumor.SecurityGroupCloudResType: SyncSG, + enumor.CvmCloudResType: SyncCvm, + enumor.RouteTableCloudResType: SyncRouteTable, + enumor.SubAccountCloudResType: SyncSubAccount, + enumor.SecurityGroupUsageBizRelResType: SyncSGUsageBizRel, + enumor.NetworkInterfaceCloudResType: SyncNetworkInterface, +}