From 37504ba3aa71bfc1c699331a53546ea60a84336b Mon Sep 17 00:00:00 2001 From: chenjr15 Date: Wed, 22 Jan 2025 11:49:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=89=E5=85=A8=E7=BB=84=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E6=94=AF=E6=8C=81=E5=8F=AF=E8=A7=81=E8=8C=83=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"`