Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 安全组-同步支持使用业务 #1254

Open
wants to merge 4 commits into
base: feat-sg-mgmt
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions cmd/cloud-server/service/sync/aws/security_group_usage_biz_rel.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions cmd/cloud-server/service/sync/aws/sync_all_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 1 addition & 2 deletions cmd/cloud-server/service/sync/azure/sub_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
67 changes: 33 additions & 34 deletions cmd/cloud-server/service/sync/azure/sync_all_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -103,41 +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
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,
}
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion cmd/cloud-server/service/sync/huawei/sync_all_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading