Skip to content

Commit

Permalink
feat: 安全组同步支持可见范围
Browse files Browse the repository at this point in the history
--story=121678806
--story=121678900
--story=121727886
--story=121727880
--story=121727876
--story=121727875
  • Loading branch information
chenjr15 committed Jan 22, 2025
1 parent cda0fe8 commit 37504ba
Show file tree
Hide file tree
Showing 15 changed files with 872 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/hc-service/logics/res-sync/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions cmd/hc-service/logics/res-sync/aws/security_group_usage_biz.go
Original file line number Diff line number Diff line change
@@ -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 := &params.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
}
1 change: 1 addition & 0 deletions cmd/hc-service/logics/res-sync/azure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions cmd/hc-service/logics/res-sync/azure/security_group_usage_biz.go
Original file line number Diff line number Diff line change
@@ -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 := &params.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
}
1 change: 1 addition & 0 deletions cmd/hc-service/logics/res-sync/huawei/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions cmd/hc-service/logics/res-sync/huawei/security_group_usage_biz.go
Original file line number Diff line number Diff line change
@@ -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 := &params.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
}
1 change: 1 addition & 0 deletions cmd/hc-service/logics/res-sync/tcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions cmd/hc-service/logics/res-sync/tcloud/security_group_usage_biz.go
Original file line number Diff line number Diff line change
@@ -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 := &params.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
}
Loading

0 comments on commit 37504ba

Please sign in to comment.