mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
feat(cloudcommon): support extra hook for db model
This commit is contained in:
@@ -1423,6 +1423,9 @@ func (dispatcher *DBModelDispatcher) Create(ctx context.Context, query jsonutils
|
||||
defer lockman.ReleaseObject(ctx, model)
|
||||
|
||||
model.PostCreate(ctx, userCred, ownerId, query, data)
|
||||
if err := manager.GetExtraHook().AfterPostCreate(ctx, userCred, ownerId, model, query, data); err != nil {
|
||||
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_POST_CREATE_HOOK, err, userCred, false)
|
||||
}
|
||||
}()
|
||||
|
||||
// 添加操作日志与消息通知
|
||||
|
||||
@@ -144,6 +144,13 @@ type IModelManager interface {
|
||||
CustomizedTotalCount(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, totalQ *sqlchemy.SQuery) (int, jsonutils.JSONObject, error)
|
||||
|
||||
PrepareQueryContext(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) context.Context
|
||||
|
||||
RegisterExtraHook(eh IModelManagerExtraHook)
|
||||
GetExtraHook() IModelManagerExtraHook
|
||||
}
|
||||
|
||||
type IModelManagerExtraHook interface {
|
||||
AfterPostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, model IModel, query jsonutils.JSONObject, data jsonutils.JSONObject) error
|
||||
}
|
||||
|
||||
type IModel interface {
|
||||
|
||||
@@ -62,6 +62,7 @@ type SModelBaseManager struct {
|
||||
keywordPlural string
|
||||
alias string
|
||||
aliasPlural string
|
||||
extraHook IModelManagerExtraHook
|
||||
}
|
||||
|
||||
func NewModelBaseManager(model interface{}, tableName string, keyword string, keywordPlural string) SModelBaseManager {
|
||||
@@ -82,6 +83,7 @@ func NewModelBaseManagerWithSplitableDBName(model interface{}, tableName string,
|
||||
tableSpec: ts,
|
||||
keyword: keyword,
|
||||
keywordPlural: keywordPlural,
|
||||
extraHook: NewEmptyExtraHook(),
|
||||
}
|
||||
return modelMan
|
||||
}
|
||||
@@ -585,6 +587,14 @@ func (manager *SModelBaseManager) CustomizedTotalCount(ctx context.Context, user
|
||||
return ret.Count, nil, nil
|
||||
}
|
||||
|
||||
func (manager *SModelBaseManager) RegisterExtraHook(eh IModelManagerExtraHook) {
|
||||
manager.extraHook = eh
|
||||
}
|
||||
|
||||
func (manager *SModelBaseManager) GetExtraHook() IModelManagerExtraHook {
|
||||
return manager.extraHook
|
||||
}
|
||||
|
||||
func (model SModelBase) GetId() string {
|
||||
return ""
|
||||
}
|
||||
@@ -767,3 +777,13 @@ func (model *SModelBase) GetUsages() []IUsage {
|
||||
func (model *SModelBase) GetI18N(ctx context.Context) *jsonutils.JSONDict {
|
||||
return nil
|
||||
}
|
||||
|
||||
type SEmptyExtraHook struct{}
|
||||
|
||||
func NewEmptyExtraHook() *SEmptyExtraHook {
|
||||
return new(SEmptyExtraHook)
|
||||
}
|
||||
|
||||
func (e SEmptyExtraHook) AfterPostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, model IModel, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ const (
|
||||
ACT_CLOUD_FULLSYNC = "cloud_fullsync"
|
||||
ACT_CLOUD_SYNC = "cloud_sync"
|
||||
ACT_CREATE = "create"
|
||||
ACT_POST_CREATE_HOOK = "post_create_hook"
|
||||
ACT_DELETE = "delete"
|
||||
ACT_PENDING_DELETE = "pending_delete"
|
||||
ACT_DISABLE = "disable"
|
||||
|
||||
Reference in New Issue
Block a user