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

fix: 修复批量更新模板权限时更新对应引用的服务 #3303

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ func (s *Service) BatchUpdateTemplatePermissions(ctx context.Context, req *pbcs.
User: req.User,
UserGroup: req.UserGroup,
Privilege: req.Privilege,
AppIds: req.AppIds,
})

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ func (s *Service) ListTmplRevisionNamesByTmplIDs(ctx context.Context, req *pbcs.
if len(ids) > 0 {
return nil, fmt.Errorf("repeated ids: %v, id must be unique", ids)
}
idsLen := len(req.TemplateIds)
if idsLen == 0 || idsLen > constant.ArrayInputLenLimit {
return nil, fmt.Errorf("the length of ids is %d, it must be within the range of [1,%d]",
idsLen, constant.ArrayInputLenLimit)
}

res := []*meta.ResourceAttribute{
{Basic: meta.Basic{Type: meta.Biz, Action: meta.FindBusinessResource}, BizID: req.BizId},
Expand Down
63 changes: 57 additions & 6 deletions bcs-services/bcs-bscp/cmd/data-service/service/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,9 @@ func (s *Service) validateBatchUpsertTemplates(grpcKit *kit.Kit, updateId []uint
}

// BatchUpdateTemplatePermissions 批量更新模板权限
// nolint:funlen
func (s *Service) BatchUpdateTemplatePermissions(ctx context.Context, req *pbds.BatchUpdateTemplatePermissionsReq) (
*pbds.BatchUpdateTemplatePermissionsResp, error) {

kt := kit.FromGrpcContext(ctx)

// 获取最新的模板配置
Expand All @@ -969,6 +969,7 @@ func (s *Service) BatchUpdateTemplatePermissions(ctx context.Context, req *pbds.
i18n.T(kt, fmt.Sprintf("lists the latest version by template ids failed, err: %s", err.Error())))
}

now := time.Now().UTC()
toCreate := make([]*table.TemplateRevision, 0)
for _, v := range tmps {
v.Spec.RevisionName = tools.GenerateRevisionName()
Expand All @@ -981,24 +982,74 @@ func (s *Service) BatchUpdateTemplatePermissions(ctx context.Context, req *pbds.
if req.Privilege != "" {
v.Spec.Permission.Privilege = req.Privilege
}
v.Revision = &table.CreatedRevision{
Creator: kt.User,
CreatedAt: time.Now().UTC(),
}
v.Revision = &table.CreatedRevision{Creator: kt.User, CreatedAt: now}
toCreate = append(toCreate, &table.TemplateRevision{
Spec: v.Spec,
Attachment: v.Attachment,
Revision: v.Revision,
})
}
if err := s.dao.TemplateRevision().BatchCreate(kt, toCreate); err != nil {

tx := s.dao.GenQuery().Begin()
if err := s.dao.TemplateRevision().BatchCreateWithTx(kt, tx, toCreate); err != nil {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kt, fmt.Sprintf("batch update of template permissions failed, err: %s", err.Error())))
}

ids := []uint32{}
templateIds := []uint32{}
templateRevisionID := map[uint32]uint32{}
for _, v := range toCreate {
ids = append(ids, v.ID)
templateIds = append(templateIds, v.Attachment.TemplateID)
templateRevisionID[v.Attachment.TemplateID] = v.ID
}

if len(req.GetAppIds()) > 0 {
// 更新引用的服务
items, err := s.dao.AppTemplateBinding().ListAppTemplateBindingByAppIds(kt, req.GetBizId(), req.GetAppIds())
if err != nil {
return nil, err
}
for _, item := range items {
templateRevisionIDs := make([]uint32, 0, len(item.Spec.Bindings))
for _, binding := range item.Spec.Bindings {
for _, revision := range binding.TemplateRevisions {
// 如果存在更新TemplateRevisionID
if id, exists := templateRevisionID[revision.TemplateID]; exists && id > 0 {
revision.TemplateRevisionID = id
revision.IsLatest = true
templateRevisionIDs = append(templateRevisionIDs, id)
} else {
templateRevisionIDs = append(templateRevisionIDs, revision.TemplateRevisionID)
}
}
}
item.Revision.Reviser = kt.User
item.Revision.UpdatedAt = now
item.Spec.TemplateRevisionIDs = tools.RemoveDuplicates(templateRevisionIDs)
item.Spec.LatestTemplateIDs = tools.RemoveDuplicates(tools.MergeAndDeduplicate(templateIds,
item.Spec.LatestTemplateIDs))
}

// 更新未命名版本绑定关系
if err := s.dao.AppTemplateBinding().BatchUpdateWithTx(kt, tx, items); err != nil {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kt, fmt.Sprintf("batch update of template permissions failed, err: %s", err.Error())))
}
}

if e := tx.Commit(); e != nil {
logs.Errorf("commit transaction failed, err: %v, rid: %s", e, kt.Rid)
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kt, fmt.Sprintf("batch update of template permissions failed, err: %s", e.Error())))
}

return &pbds.BatchUpdateTemplatePermissionsResp{Ids: ids}, nil
}
31 changes: 31 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/dao/app_template_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ type AppTemplateBinding interface {
Update(kit *kit.Kit, atb *table.AppTemplateBinding) error
// UpdateWithTx Update one app template binding's info with transaction.
UpdateWithTx(kit *kit.Kit, tx *gen.QueryTx, atb *table.AppTemplateBinding) error
// BatchUpdateWithTx batch update app template binding's instances with transaction.
BatchUpdateWithTx(kit *kit.Kit, tx *gen.QueryTx, data []*table.AppTemplateBinding) error
// List app template bindings with options.
List(kit *kit.Kit, bizID, appID uint32, opt *types.BasePage) ([]*table.AppTemplateBinding, int64, error)
// Delete one app template binding instance.
Delete(kit *kit.Kit, atb *table.AppTemplateBinding) error
// DeleteByAppIDWithTx delete one app template binding instance by app id with transaction.
DeleteByAppIDWithTx(kit *kit.Kit, tx *gen.QueryTx, appID uint32) error
// ListAppTemplateBindingByAppIds 按 AppId 列出应用模板绑定
ListAppTemplateBindingByAppIds(kit *kit.Kit, bizID uint32, appID []uint32) ([]*table.AppTemplateBinding, error)
}

var _ AppTemplateBinding = new(appTemplateBindingDao)
Expand All @@ -48,6 +52,33 @@ type appTemplateBindingDao struct {
auditDao AuditDao
}

// BatchUpdateWithTx batch update app template binding's instances with transaction.
func (dao *appTemplateBindingDao) BatchUpdateWithTx(kit *kit.Kit, tx *gen.QueryTx,
data []*table.AppTemplateBinding) error {
if len(data) == 0 {
return nil
}
for _, g := range data {
if err := g.ValidateUpdate(); err != nil {
return err
}
if err := dao.validateAttachmentExist(kit, g.Attachment); err != nil {
return err
}
}
return tx.AppTemplateBinding.WithContext(kit.Ctx).Save(data...)
}

// ListAppTemplateBindingByAppIds 按 AppId 列出应用模板绑定
func (dao *appTemplateBindingDao) ListAppTemplateBindingByAppIds(kit *kit.Kit, bizID uint32, appIDs []uint32) (
[]*table.AppTemplateBinding, error) {

m := dao.genQ.AppTemplateBinding
return dao.genQ.AppTemplateBinding.WithContext(kit.Ctx).
Where(m.BizID.Eq(bizID), m.AppID.In(appIDs...)).
Find()
}

// Create one app template binding instance.
func (dao *appTemplateBindingDao) Create(kit *kit.Kit, g *table.AppTemplateBinding) (uint32, error) {
if err := g.ValidateCreate(); err != nil {
Expand Down
21 changes: 0 additions & 21 deletions bcs-services/bcs-bscp/pkg/dal/dao/template_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ type TemplateRevision interface {
BatchCreateWithTx(kit *kit.Kit, tx *gen.QueryTx, revisions []*table.TemplateRevision) error
// ListLatestRevisionsGroupByTemplateIds Lists the latest version groups by template ids
ListLatestRevisionsGroupByTemplateIds(kit *kit.Kit, templateIDs []uint32) ([]*table.TemplateRevision, error)
// BatchCreate batch create template revisions.
BatchCreate(kit *kit.Kit, revisions []*table.TemplateRevision) error
}

var _ TemplateRevision = new(templateRevisionDao)
Expand All @@ -66,25 +64,6 @@ type templateRevisionDao struct {
auditDao AuditDao
}

// BatchCreate batch create template revisions.
func (dao *templateRevisionDao) BatchCreate(kit *kit.Kit, revisions []*table.TemplateRevision) error {
if len(revisions) == 0 {
return nil
}
ids, err := dao.idGen.Batch(kit, table.TemplateRevisionsTable, len(revisions))
if err != nil {
return err
}
for i, item := range revisions {
if err := item.ValidateCreate(); err != nil {
return err
}
item.ID = ids[i]
}

return dao.genQ.TemplateRevision.WithContext(kit.Ctx).CreateInBatches(revisions, 200)
}

// Create one template revision instance.
func (dao *templateRevisionDao) Create(kit *kit.Kit, g *table.TemplateRevision) (uint32, error) {
if err := g.ValidateCreate(); err != nil {
Expand Down
Loading
Loading