diff --git a/bcs-services/bcs-bscp/.gitignore b/bcs-services/bcs-bscp/.gitignore index c232cce235..e532c83fc5 100644 --- a/bcs-services/bcs-bscp/.gitignore +++ b/bcs-services/bcs-bscp/.gitignore @@ -4,4 +4,5 @@ bcs-bscp bin .codecc .idea -.vscode \ No newline at end of file +.vscode +__debug_bin \ No newline at end of file diff --git a/bcs-services/bcs-bscp/cmd/data-service/service/kv.go b/bcs-services/bcs-bscp/cmd/data-service/service/kv.go index d046591f8f..afeea951d3 100644 --- a/bcs-services/bcs-bscp/cmd/data-service/service/kv.go +++ b/bcs-services/bcs-bscp/cmd/data-service/service/kv.go @@ -465,10 +465,7 @@ func (s *Service) UnDeleteKv(ctx context.Context, req *pbds.UnDeleteKvReq) (*pbb } } - kvState = []string{ - string(table.KvStateDelete), - } - if err = s.dao.Kv().UpdateSelectedKVStates(kt, tx, req.Attachment.BizId, req.Attachment.AppId, kvState, + if err = s.dao.Kv().UpdateStateWithTx(kt, tx, req.Attachment.BizId, req.Attachment.AppId, req.Spec.Key, table.KvStateUnchange); err != nil { if rErr := tx.Rollback(); rErr != nil { logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid) diff --git a/bcs-services/bcs-bscp/pkg/dal/dao/kv.go b/bcs-services/bcs-bscp/pkg/dal/dao/kv.go index b982337ff1..b2b7b10e6f 100644 --- a/bcs-services/bcs-bscp/pkg/dal/dao/kv.go +++ b/bcs-services/bcs-bscp/pkg/dal/dao/kv.go @@ -53,6 +53,8 @@ type Kv interface { // UpdateSelectedKVStates updates the states of selected kv pairs using a transaction UpdateSelectedKVStates(kit *kit.Kit, tx *gen.QueryTx, bizID, appID uint32, targetKVStates []string, newKVStates table.KvState) error + // UpdateStateWithTx updates the state of a kv pair with transaction + UpdateStateWithTx(kit *kit.Kit, tx *gen.QueryTx, bizID, appID uint32, key string, state table.KvState) error // DeleteByStateWithTx deletes kv pairs with a specific state using a transaction DeleteByStateWithTx(kit *kit.Kit, tx *gen.QueryTx, kv *table.Kv) error } @@ -415,6 +417,30 @@ func (dao *kvDao) UpdateSelectedKVStates(kit *kit.Kit, tx *gen.QueryTx, bizID, a return nil } +// UpdateStateWithTx updates the state of a kv pair with transaction +func (dao *kvDao) UpdateStateWithTx(kit *kit.Kit, tx *gen.QueryTx, bizID, appID uint32, key string, + state table.KvState) error { + if bizID <= 0 { + return errors.New("biz id should be set") + } + + if appID <= 0 { + return errors.New("app id should be set") + } + + if key == "" { + return errors.New("key cannot be empty") + } + + if state.String() == "" { + return errors.New("state cannot be empty") + } + + m := tx.Kv + _, err := m.WithContext(kit.Ctx).Where(m.BizID.Eq(bizID), m.AppID.Eq(appID), m.Key.Eq(key)).Update(m.KvState, state) + return err +} + // ListAllByAppID list all Kv by appID func (dao *kvDao) ListAllByAppID(kit *kit.Kit, appID uint32, bizID uint32, kvState []string) ([]*table.Kv, error) { if appID == 0 { diff --git a/bcs-services/bcs-bscp/pkg/dal/table/kv.go b/bcs-services/bcs-bscp/pkg/dal/table/kv.go index f1c6612fe6..14367b6fe6 100644 --- a/bcs-services/bcs-bscp/pkg/dal/table/kv.go +++ b/bcs-services/bcs-bscp/pkg/dal/table/kv.go @@ -265,3 +265,18 @@ const ( // KvStateUnchange 不变 KvStateUnchange KvState = "UNCHANGE" ) + +// String get string value of KvState +func (k KvState) String() string { + return string(k) +} + +// Validate validate kv state is valid or not. +func (k KvState) Validate() error { + switch k { + case KvStateAdd, KvStateDelete, KvStateRevise, KvStateUnchange: + return nil + default: + return errors.New("invalid kv state") + } +} diff --git a/bcs-services/bcs-bscp/pkg/protocol/config-server/config_service_grpc.pb.go b/bcs-services/bcs-bscp/pkg/protocol/config-server/config_service_grpc.pb.go index abaabb692d..7576c0e290 100644 --- a/bcs-services/bcs-bscp/pkg/protocol/config-server/config_service_grpc.pb.go +++ b/bcs-services/bcs-bscp/pkg/protocol/config-server/config_service_grpc.pb.go @@ -222,12 +222,12 @@ type ConfigClient interface { CreateTemplateRevision(ctx context.Context, in *CreateTemplateRevisionReq, opts ...grpc.CallOption) (*CreateTemplateRevisionResp, error) ListTemplateRevisions(ctx context.Context, in *ListTemplateRevisionsReq, opts ...grpc.CallOption) (*ListTemplateRevisionsResp, error) // 暂时不对外开发(删除模版后,服务引用的latest版本会回退到上一个老版本) - //rpc DeleteTemplateRevision(DeleteTemplateRevisionReq) returns (DeleteTemplateRevisionResp) { - //option (google.api.http) = { - //delete : - //"/api/v1/config/biz/{biz_id}/template_spaces/{template_space_id}/templates/{template_id}/template_revisions/{template_revision_id}" - //}; - //} + // rpc DeleteTemplateRevision(DeleteTemplateRevisionReq) returns (DeleteTemplateRevisionResp) { + // option (google.api.http) = { + // delete : + // "/api/v1/config/biz/{biz_id}/template_spaces/{template_space_id}/templates/{template_id}/template_revisions/{template_revision_id}" + // }; + // } ListTemplateRevisionsByIDs(ctx context.Context, in *ListTemplateRevisionsByIDsReq, opts ...grpc.CallOption) (*ListTemplateRevisionsByIDsResp, error) ListTmplRevisionNamesByTmplIDs(ctx context.Context, in *ListTmplRevisionNamesByTmplIDsReq, opts ...grpc.CallOption) (*ListTmplRevisionNamesByTmplIDsResp, error) CreateTemplateSet(ctx context.Context, in *CreateTemplateSetReq, opts ...grpc.CallOption) (*CreateTemplateSetResp, error) @@ -1498,12 +1498,12 @@ type ConfigServer interface { CreateTemplateRevision(context.Context, *CreateTemplateRevisionReq) (*CreateTemplateRevisionResp, error) ListTemplateRevisions(context.Context, *ListTemplateRevisionsReq) (*ListTemplateRevisionsResp, error) // 暂时不对外开发(删除模版后,服务引用的latest版本会回退到上一个老版本) - //rpc DeleteTemplateRevision(DeleteTemplateRevisionReq) returns (DeleteTemplateRevisionResp) { - //option (google.api.http) = { - //delete : - //"/api/v1/config/biz/{biz_id}/template_spaces/{template_space_id}/templates/{template_id}/template_revisions/{template_revision_id}" - //}; - //} + // rpc DeleteTemplateRevision(DeleteTemplateRevisionReq) returns (DeleteTemplateRevisionResp) { + // option (google.api.http) = { + // delete : + // "/api/v1/config/biz/{biz_id}/template_spaces/{template_space_id}/templates/{template_id}/template_revisions/{template_revision_id}" + // }; + // } ListTemplateRevisionsByIDs(context.Context, *ListTemplateRevisionsByIDsReq) (*ListTemplateRevisionsByIDsResp, error) ListTmplRevisionNamesByTmplIDs(context.Context, *ListTmplRevisionNamesByTmplIDsReq) (*ListTmplRevisionNamesByTmplIDsResp, error) CreateTemplateSet(context.Context, *CreateTemplateSetReq) (*CreateTemplateSetResp, error)