Skip to content

Commit

Permalink
feat: 操作记录列表新增其他模块
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Dec 13, 2024
1 parent cdd3a57 commit af46079
Show file tree
Hide file tree
Showing 66 changed files with 1,349 additions and 1,210 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 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.
*/

package migrations

import (
"gorm.io/gorm"

"github.com/TencentBlueKing/bk-bscp/cmd/data-service/db-migration/migrator"
)

func init() {
// add current migration to migrator
migrator.GetMigrator().AddMigration(&migrator.Migration{
Version: "20241128111704",
Name: "20241128111704_modify_audit",
Mode: migrator.GormMode,
Up: mig20241128111704Up,
Down: mig20241128111704Down,
})
}

// mig20241128111704Up for up migration
func mig20241128111704Up(tx *gorm.DB) error {
// Audits : audits
type Audits struct {
Detail string `gorm:"type:text"`
}
// Audits add new column
if tx.Migrator().HasColumn(&Audits{}, "detail") {
if err := tx.Migrator().AlterColumn(&Audits{}, "detail"); err != nil {
return err
}
}

return nil
}

// mig20241128111704Down for down migration
func mig20241128111704Down(tx *gorm.DB) error {
// Audits : audits
type Audits struct {
Detail string `gorm:"type:json"`
}
// Strategies add new column
if tx.Migrator().HasColumn(&Audits{}, "text") {
if err := tx.Migrator().AlterColumn(&Audits{}, "detail"); err != nil {
return err
}
}

return nil
}
14 changes: 13 additions & 1 deletion cmd/data-service/service/app_template_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"strings"
"time"

"github.com/TencentBlueKing/bk-bscp/internal/criteria/constant"
"github.com/TencentBlueKing/bk-bscp/internal/dal/gen"
"github.com/TencentBlueKing/bk-bscp/internal/search"
"github.com/TencentBlueKing/bk-bscp/pkg/criteria/constant"
"github.com/TencentBlueKing/bk-bscp/pkg/criteria/errf"
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bscp/pkg/i18n"
Expand Down Expand Up @@ -129,6 +129,9 @@ func (s *Service) UpdateAppTemplateBinding(ctx context.Context, req *pbds.Update
tx := s.dao.GenQuery().Begin()

if err := s.dao.AppTemplateBinding().UpdateWithTx(kt, tx, appTemplateBinding); err != nil {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
logs.Errorf("update app template binding failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
}
Expand Down Expand Up @@ -1078,7 +1081,15 @@ func (s *Service) ImportFromTemplateSetToApp(ctx context.Context, req *pbds.Impo
return nil, err
}

isRollback := true
tx := s.dao.GenQuery().Begin()
defer func() {
if isRollback {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kit.Rid)
}
}
}()
appTemplateBinding := &table.AppTemplateBinding{
Spec: &table.AppTemplateBindingSpec{
TemplateSpaceIDs: tools.RemoveDuplicates(templateSpaceIds),
Expand Down Expand Up @@ -1109,6 +1120,7 @@ func (s *Service) ImportFromTemplateSetToApp(ctx context.Context, req *pbds.Impo
logs.Errorf("commit transaction failed, err: %v, rid: %s", err, kit.Rid)
return nil, err
}
isRollback = false

return &pbbase.EmptyResp{}, nil
}
Expand Down
20 changes: 15 additions & 5 deletions cmd/data-service/service/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
"fmt"
"time"

"github.com/TencentBlueKing/bk-bscp/pkg/criteria/enumor"
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
pbapp "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/app"
pbaudit "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/audit"
pbstrategy "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/strategy"
pbds "github.com/TencentBlueKing/bk-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
)

// ListAudits list audits.
Expand All @@ -46,19 +48,27 @@ func (s *Service) ListAudits(ctx context.Context, req *pbds.ListAuditsReq) (*pbd

var details []*pbaudit.ListAuditsAppStrategy
for _, value := range aas {
// client的状态比较特殊,不能实时同步
status := value.Audit.Status
detail := value.Audit.Detail
if value.Audit.ResourceType == string(enumor.Instance) &&
value.Client.ReleaseChangeStatus == string(table.Failed) {
status = string(enumor.Failure)
detail = value.Client.FailedDetailReason
}
details = append(details, &pbaudit.ListAuditsAppStrategy{
Audit: &pbaudit.Audit{
Id: value.Audit.ID,
Spec: &pbaudit.AuditSpec{
ResType: value.Audit.ResourceType,
Action: value.Audit.Action,
Rid: "", // 暂时用不到
AppCode: "", // 暂时用不到
Detail: "", // 暂时用不到
Rid: "", // 暂时用不到
AppCode: "", // 暂时用不到
Detail: detail, // 暂时用不到
Operator: value.Audit.Operator,
ResInstance: value.Audit.ResInstance,
OperateWay: value.Audit.OperateWay,
Status: value.Audit.Status,
Status: status,
IsCompare: value.Audit.IsCompare,
},
Attachment: &pbaudit.AuditAttachment{
Expand Down
13 changes: 11 additions & 2 deletions cmd/data-service/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bscp/pkg/logs"
sfs "github.com/TencentBlueKing/bk-bscp/pkg/sf-share"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
pbbase "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/base"
pbclient "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/client"
pbds "github.com/TencentBlueKing/bk-bscp/pkg/protocol/data-service"
sfs "github.com/TencentBlueKing/bk-bscp/pkg/sf-share"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
)

// BatchUpsertClientMetrics 批量操作client metrics
Expand Down Expand Up @@ -913,7 +913,15 @@ func formatCpu(number float64) string {
func (s *Service) RetryClients(ctx context.Context, req *pbds.RetryClientsReq) (*pbbase.EmptyResp, error) {
kit := kit.FromGrpcContext(ctx)

isRollback := true
tx := s.dao.GenQuery().Begin()
defer func() {
if isRollback {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kit.Rid)
}
}
}()

if req.All {
event := types.Event{
Expand Down Expand Up @@ -970,6 +978,7 @@ func (s *Service) RetryClients(ctx context.Context, req *pbds.RetryClientsReq) (
logs.Errorf("commit retry clients transaction failed, err: %v, rid: %s", err, kit.Rid)
return nil, err
}
isRollback = false

return &pbbase.EmptyResp{}, nil
}
3 changes: 3 additions & 0 deletions cmd/data-service/service/config_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func (s *Service) UpdateConfigHook(ctx context.Context, req *pbds.UpdateConfigHo
hook, err := s.getReleasedHook(kt, preHook)
if err != nil {
logs.Errorf("no released releases of the pre-hook, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, errors.New("no released releases of the pre-hook")
}

Expand Down
23 changes: 10 additions & 13 deletions cmd/data-service/service/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,15 @@ func (s *Service) UnDeleteConfigItem(ctx context.Context, req *pbds.UnDeleteConf
}

commitID, contentID := []uint32{}, []uint32{}
isRollback := true
tx := s.dao.GenQuery().Begin()
defer func() {
if isRollback {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, grpcKit.Rid)
}
}
}()
// 判断是不是新增的数据
if ci != nil && ci.ID != 0 {
rci, errCi := s.dao.ReleasedCI().Get(grpcKit, req.Attachment.BizId,
Expand All @@ -1260,9 +1268,6 @@ func (s *Service) UnDeleteConfigItem(ctx context.Context, req *pbds.UnDeleteConf
}
if err = s.dao.ConfigItem().DeleteWithTx(grpcKit, tx, ci); err != nil {
logs.Errorf("recover config item failed, err: %v, rid: %s", err, grpcKit.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, grpcKit.Rid)
}
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(grpcKit, "recover config item failed, err: %v", err))
}
Expand All @@ -1284,18 +1289,12 @@ func (s *Service) UnDeleteConfigItem(ctx context.Context, req *pbds.UnDeleteConf

if err = s.dao.Commit().BatchDeleteWithTx(grpcKit, tx, commitID); err != nil {
logs.Errorf("undo commit failed, err: %v, rid: %s", err, grpcKit.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, grpcKit.Rid)
}
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(grpcKit, "recover config item failed, err: %v", err))
}

if err = s.dao.Content().BatchDeleteWithTx(grpcKit, tx, contentID); err != nil {
logs.Errorf("undo content failed, err: %v, rid: %s", err, grpcKit.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, grpcKit.Rid)
}
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(grpcKit, "recover config item failed, err: %v", err))
}
Expand All @@ -1308,9 +1307,6 @@ func (s *Service) UnDeleteConfigItem(ctx context.Context, req *pbds.UnDeleteConf
}
if err = s.dao.ConfigItem().RecoverConfigItem(grpcKit, tx, data); err != nil {
logs.Errorf("recover config item failed, err: %v, rid: %s", err, grpcKit.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, grpcKit.Rid)
}
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(grpcKit, "recover config item failed, err: %v", err))
}
Expand All @@ -1319,6 +1315,7 @@ func (s *Service) UnDeleteConfigItem(ctx context.Context, req *pbds.UnDeleteConf
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(grpcKit, "recover config item failed, err: %v", e))
}
isRollback = false

return new(pbbase.EmptyResp), nil
}
Expand Down Expand Up @@ -1946,7 +1943,7 @@ func (s *Service) RemoveAppBoundTmplSet(ctx context.Context, req *pbds.RemoveApp
},
}

if err = s.dao.AppTemplateBinding().Update(kit, appTemplateBinding); err != nil {
if err = s.dao.AppTemplateBinding().Update(kit, appTemplateBinding, req.TemplateSetId); err != nil {
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kit, "remove the template set bound to the app failed, err: %s", err))
}
Expand Down
22 changes: 21 additions & 1 deletion cmd/data-service/service/credential_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ package service

import (
"context"
"fmt"
"time"

"github.com/TencentBlueKing/bk-bscp/internal/criteria/constant"
"github.com/TencentBlueKing/bk-bscp/pkg/criteria/enumor"
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bscp/pkg/logs"
"github.com/TencentBlueKing/bk-bscp/pkg/runtime/credential"
pbcrs "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/credential-scope"
pbds "github.com/TencentBlueKing/bk-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bscp/pkg/runtime/credential"
)

// ListCredentialScopes get credential scopes
Expand Down Expand Up @@ -54,6 +57,11 @@ func (s *Service) UpdateCredentialScopes(ctx context.Context,

kt := kit.FromGrpcContext(ctx)

credentialRecord, err := s.dao.Credential().Get(kt, req.BizId, req.CredentialId)
if err != nil {
return nil, err
}

tx := s.dao.GenQuery().Begin()

for _, updated := range req.Updated {
Expand Down Expand Up @@ -130,6 +138,18 @@ func (s *Service) UpdateCredentialScopes(ctx context.Context,
}
return nil, err
}

ad := s.dao.AuditDao().Decorator(kt, req.BizId, &table.AuditField{
ResourceInstance: fmt.Sprintf(constant.AssociatedAppConfigCredentialName, credentialRecord.Spec.Name),
Status: enumor.Success,
}).PrepareUpdate(&table.Credential{ID: req.CredentialId})
if err := ad.Do(tx.Query); err != nil {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}

if err := tx.Commit(); err != nil {
logs.Errorf("commit transaction failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
Expand Down
20 changes: 18 additions & 2 deletions cmd/data-service/service/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ import (

"gorm.io/gorm"

"github.com/TencentBlueKing/bk-bscp/internal/criteria/constant"
"github.com/TencentBlueKing/bk-bscp/pkg/criteria/enumor"
"github.com/TencentBlueKing/bk-bscp/pkg/criteria/errf"
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bscp/pkg/i18n"
"github.com/TencentBlueKing/bk-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bscp/pkg/logs"
"github.com/TencentBlueKing/bk-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
pbbase "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/base"
pbhook "github.com/TencentBlueKing/bk-bscp/pkg/protocol/core/hook"
pbds "github.com/TencentBlueKing/bk-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
)

// CreateHook create hook.
Expand Down Expand Up @@ -92,6 +94,20 @@ func (s *Service) CreateHook(ctx context.Context, req *pbds.CreateHookReq) (*pbd
return nil, err
}

err = s.dao.AuditDao().Decorator(kt, req.Attachment.BizId, &table.AuditField{
ResourceInstance: fmt.Sprintf(constant.HookName+constant.ResSeparator+constant.HookRevisionName,
hook.Spec.Name, revision.Spec.Name),
Status: enumor.Success,
Detail: revision.Spec.Memo,
}).PrepareCreate(revision).Do(tx.Query)
if err != nil {
logs.Errorf("PrepareCreate HookRevision failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}

if e := tx.Commit(); e != nil {
logs.Errorf("commit transaction failed, err: %v, rid: %s", e, kt.Rid)
return nil, e
Expand Down
Loading

0 comments on commit af46079

Please sign in to comment.