Skip to content

Commit

Permalink
feat: 安全组-资源 关联关系迁移到common表 --story=121678936
Browse files Browse the repository at this point in the history
  • Loading branch information
KooKouse committed Jan 16, 2025
1 parent 4875605 commit 75c957a
Show file tree
Hide file tree
Showing 16 changed files with 576 additions and 236 deletions.
8 changes: 6 additions & 2 deletions cmd/cloud-server/service/cvm/batch_associate.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ func (svc *cvmSvc) batchAssociateSecurityGroups(cts *rest.Contexts, validHandler

func (svc *cvmSvc) deleteSecurityGroupAndCvmRelationship(kt *kit.Kit, cvmID string, sgIDs []string) error {
batchDeleteReq := &proto.BatchDeleteReq{
Filter: tools.ExpressionAnd(tools.RuleEqual("cvm_id", cvmID), tools.RuleIn("security_group_id", sgIDs)),
Filter: tools.ExpressionAnd(
tools.RuleEqual("res_id", cvmID),
tools.RuleEqual("res_type", enumor.CvmCloudResType),
tools.RuleIn("security_group_id", sgIDs),
),
}
err := svc.client.DataService().Global.SGCvmRel.BatchDeleteSgCvmRels(kt.Ctx, kt.Header(), batchDeleteReq)
err := svc.client.DataService().Global.SGCommonRel.BatchDeleteSgCommonRels(kt, batchDeleteReq)
if err != nil {
logs.Errorf("delete security group and cvm relationship failed, err: %v, req: %+v, rid: %s",
err, batchDeleteReq, kt.Rid)
Expand Down
19 changes: 10 additions & 9 deletions cmd/cloud-server/service/cvm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (svc *cvmSvc) listCvmSecurityGroups(cts *rest.Contexts, authHandler handler
itemMap := convertToBatchListCvmSecurityGroupsRespItem(securityGroupsMap)
cvmToSgMap := make(map[string][]cscvm.BatchListCvmSecurityGroupsRespItem, len(req.CvmIDs))
for _, rel := range rels {
cvmToSgMap[rel.CvmID] = append(cvmToSgMap[rel.CvmID], itemMap[rel.SecurityGroupID])
cvmToSgMap[rel.ResID] = append(cvmToSgMap[rel.ResID], itemMap[rel.SecurityGroupID])
}

result := make([]cscvm.BatchListCvmSecurityGroupsResp, len(req.CvmIDs))
Expand Down Expand Up @@ -346,26 +346,27 @@ func (svc *cvmSvc) getSecurityGroupsMap(kt *kit.Kit, sgIDs []string) (map[string
return result, nil
}

func (svc *cvmSvc) listCvmSecurityGroupRels(kt *kit.Kit, cvmIDs []string) ([]cloud.SecurityGroupCvmRel, error) {
func (svc *cvmSvc) listCvmSecurityGroupRels(kt *kit.Kit, cvmIDs []string) ([]cloud.SecurityGroupCommonRel, error) {

result := make([]cloud.SecurityGroupCvmRel, 0)
result := make([]cloud.SecurityGroupCommonRel, 0)

req := &core.ListReq{
Filter: tools.ContainersExpression("cvm_id", cvmIDs),
Page: core.NewDefaultBasePage(),
Filter: tools.ExpressionAnd(
tools.RuleIn("res_id", cvmIDs),
tools.RuleEqual("res_type", enumor.CvmCloudResType),
),
Page: core.NewDefaultBasePage(),
}

for {
sgCvmRels, err := svc.client.DataService().Global.SGCvmRel.ListSgCvmRels(kt.Ctx, kt.Header(), req)
sgCvmRels, err := svc.client.DataService().Global.SGCommonRel.ListSgCommonRels(kt, req)
if err != nil {
return nil, err
}
if len(sgCvmRels.Details) == 0 {
break
}
for _, detail := range sgCvmRels.Details {
result = append(result, detail)
}
result = append(result, sgCvmRels.Details...)
req.Page.Start += uint32(core.DefaultMaxPageLimit)
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/cloud-server/service/cvm/security_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ func (svc *cvmSvc) listCvmSecurityGroupRules(kt *kit.Kit, vendor enumor.Vendor,
func (svc *cvmSvc) checkCvmAndSecurityGroupRel(kt *kit.Kit, cvmID, sgID string) error {
checkReq := &core.ListReq{
Filter: tools.ExpressionAnd(
tools.RuleEqual("cvm_id", cvmID),
tools.RuleEqual("res_id", cvmID),
tools.RuleEqual("security_group_id", sgID),
tools.RuleEqual("res_type", enumor.CvmCloudResType),
),
Page: core.NewCountPage(),
}
rels, err := svc.client.DataService().Global.SGCvmRel.ListSgCvmRels(kt.Ctx, kt.Header(), checkReq)
rels, err := svc.client.DataService().Global.SGCommonRel.ListSgCommonRels(kt, checkReq)
if err != nil {
logs.Errorf("check cvm and security group relation failed, err: %v, rid: %s", err, kt.Rid)
return err
Expand Down
11 changes: 8 additions & 3 deletions cmd/cloud-server/service/security-group/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ func (svc *securityGroupSvc) getSecurityGroup(cts *rest.Contexts, validHandler h

func (svc *securityGroupSvc) queryAssociateCvmCount(kt *kit.Kit, id string) (uint64, error) {
cvmRelOpt := &core.ListReq{
Filter: tools.EqualExpression("security_group_id", id),
Page: core.NewCountPage(),
Filter: tools.ExpressionAnd(
tools.RuleEqual("security_group_id", id),
tools.RuleEqual("res_type", enumor.CvmCloudResType),
),
Page: core.NewCountPage(),
}
cvmRelResult, err := svc.client.DataService().Global.SGCvmRel.ListSgCvmRels(kt.Ctx, kt.Header(), cvmRelOpt)
cvmRelResult, err := svc.client.DataService().Global.SGCommonRel.ListSgCommonRels(kt, cvmRelOpt)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -231,11 +234,13 @@ func (svc *securityGroupSvc) listSecurityGroup(cts *rest.Contexts, authHandler h
}

// ListSecurityGroupsByCvmID list security groups by cvm_id.
// Deprecated: use ListSecurityGroupsByResID instead.
func (svc *securityGroupSvc) ListSecurityGroupsByCvmID(cts *rest.Contexts) (interface{}, error) {
return svc.listSGByCvmID(cts, handler.ResOperateAuth)
}

// ListBizSecurityGroupsByCvmID list biz security groups by cvm_id.
// Deprecated: use ListBizSecurityGroupsByResID instead.
func (svc *securityGroupSvc) ListBizSecurityGroupsByCvmID(cts *rest.Contexts) (interface{}, error) {
return svc.listSGByCvmID(cts, handler.BizOperateAuth)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/cloud-server/service/security-group/related_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ func (svc *securityGroupSvc) listResourceIdBySecurityGroup(cts *rest.Contexts, v
}

// ListCvmIdBySecurityGroup list cvm id by security group
// Deprecated: table[security_group_cvm_rel] is deprecated. Use ListResourceIdBySecurityGroup instead.
func (svc *securityGroupSvc) ListCvmIdBySecurityGroup(cts *rest.Contexts) (interface{}, error) {
return svc.listCvmIDBySecurityGroup(cts, handler.ResOperateAuth)
}

// ListBizCvmIdBySecurityGroup list biz cvm id by security group
// Deprecated: table[security_group_cvm_rel] is deprecated. Use ListBizResourceIDBySecurityGroup instead.
func (svc *securityGroupSvc) ListBizCvmIdBySecurityGroup(cts *rest.Contexts) (interface{}, error) {
return svc.listCvmIDBySecurityGroup(cts, handler.BizOperateAuth)
}

// Deprecated: table[security_group_cvm_rel] is deprecated.
func (svc *securityGroupSvc) listCvmIDBySecurityGroup(cts *rest.Contexts, validHandler handler.ValidWithAuthHandler) (interface{}, error) {
id := cts.PathParameter("id").String()
if len(id) == 0 {
Expand Down
63 changes: 63 additions & 0 deletions cmd/data-service/service/cloud/security-group-common-rel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import (

"hcm/pkg/api/core"
protocloud "hcm/pkg/api/data-service/cloud"
"hcm/pkg/criteria/enumor"
"hcm/pkg/criteria/errf"
"hcm/pkg/dal/dao/orm"
"hcm/pkg/dal/dao/tools"
"hcm/pkg/dal/dao/types"
tablecloud "hcm/pkg/dal/table/cloud"
"hcm/pkg/kit"
"hcm/pkg/logs"
"hcm/pkg/rest"
"hcm/pkg/tools/converter"
"hcm/pkg/tools/slice"

"github.com/jmoiron/sqlx"
)
Expand Down Expand Up @@ -59,6 +63,11 @@ func (svc *sgComRelSvc) BatchCreateSgCommonRels(cts *rest.Contexts) (interface{}
})
}

// check relation resource is existed
if err := svc.checkRelationResourceExist(cts.Kit, req.Rels); err != nil {
logs.Errorf("check relation resource exist failed, err: %v, rid: %s", err, cts.Kit.Rid)
return nil, nil
}
if err := svc.dao.SGCommonRel().BatchCreateWithTx(cts.Kit, txn, models); err != nil {
return nil, fmt.Errorf("batch create sg common rels failed, err: %v", err)
}
Expand Down Expand Up @@ -125,6 +134,12 @@ func (svc *sgComRelSvc) BatchUpsertSgCommonRels(cts *rest.Contexts) (interface{}
Creator: cts.Kit.User,
})
}
// check relation resource is existed
if err := svc.checkRelationResourceExist(cts.Kit, req.Rels); err != nil {
logs.Errorf("check relation resource exist failed, err: %v, rid: %s", err, cts.Kit.Rid)
return nil, nil
}

if err := svc.dao.SGCommonRel().BatchCreateWithTx(cts.Kit, txn, models); err != nil {
return nil, fmt.Errorf("batch create sg common rels failed, err: %v", err)
}
Expand All @@ -137,3 +152,51 @@ func (svc *sgComRelSvc) BatchUpsertSgCommonRels(cts *rest.Contexts) (interface{}

return nil, nil
}

func (svc *sgComRelSvc) checkRelationResourceExist(kt *kit.Kit, rels []protocloud.SGCommonRelCreate) error {
// check relation resource is existed
// 校验关联资源是否存在
sgIDs := make([]string, 0)
resTypeToResIDsMap := make(map[enumor.CloudResourceType][]string)
for _, rel := range rels {
sgIDs = append(sgIDs, rel.SecurityGroupID)
resTypeToResIDsMap[rel.ResType] = append(resTypeToResIDsMap[rel.ResType], rel.ResID)
}

sgMap := make(map[string]tablecloud.SecurityGroupTable)
for _, ids := range slice.Split(sgIDs, int(core.DefaultMaxPageLimit)) {
listOpt := &types.ListOption{
Filter: tools.ContainersExpression("id", ids),
Page: core.NewDefaultBasePage(),
}
resp, err := svc.dao.SecurityGroup().List(kt, listOpt)
if err != nil {
logs.Errorf("list security group failed, err: %v, ids: %v, rid: %s", err, ids, kt.Rid)
return err
}
for _, detail := range resp.Details {
sgMap[detail.ID] = detail
}
}

if len(sgMap) != len(converter.StringSliceToMap(sgIDs)) {
logs.Errorf("get security group count not right, ids: %v, count: %d, rid: %s", sgIDs, len(sgMap), kt.Rid)
return fmt.Errorf("get security group count not right")
}

for resType, resIDs := range resTypeToResIDsMap {
dbResp, err := svc.dao.Cloud().ListResourceIDs(kt, resType, tools.ContainersExpression("id", resIDs))
if err != nil {
logs.Errorf("list resource ids failed, err: %v, resType: %s, resIDs: %v, rid: %s",
err, resType, resIDs, kt.Rid)
return err
}
if len(dbResp) != len(converter.StringSliceToMap(resIDs)) {
logs.Errorf("get resource count not right, err: %v, resType: %s, resIDs: %v, rid: %s",
err, resType, resIDs, kt.Rid)
return fmt.Errorf("get resource count not right")
}
}

return nil
}
Loading

0 comments on commit 75c957a

Please sign in to comment.