feat(notify): support db_dispatcher notify

This commit is contained in:
马鸿飞
2023-10-12 15:30:00 +08:00
parent 990307c3bf
commit 2d7766dc4c
7 changed files with 69 additions and 4 deletions
+3 -3
View File
@@ -226,7 +226,7 @@ const (
,请尽快前往控制台进行处理
{{- end -}}`
COMMON_CONTENT_EN = `{{- $d := .resource_details -}}
Your {{ if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type_display }} {{ $d.name }} {{ if $d.project -}} in project {{ $d.project }} {{ end -}} has been {{ .action_display }} {{ .result_display }}
Your {{- if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type_display }} {{ $d.name }} {{ if $d.project -}} in project {{ $d.project }} {{ end -}} has been {{ .action_display }} {{ .result_display }}
{{- if eq .result "failed" -}}
. And please go to the console as soon as possible to process.
{{- end -}}`
@@ -432,7 +432,7 @@ const (
// 资源变更通知
const (
UPDATE_TITLE_CN = `{{- $d := .resource_details -}}
if {{ $d.project }}
{{- if $d.project -}}
{{ $d.project }}项目的
{{- end -}}
{{ .resource_type_display }}{{ $d.name }}{{ .action_display }}成功`
@@ -440,7 +440,7 @@ const (
The {{ .resource_type }} {{ $d.name }} {{ if $d.project -}} in project {{ $d.project }} {{ end -}} {{ .action_display }} successfully`
UPDATE_CONTENT_CN = `{{- $d := .resource_details -}}
if {{$d.project }}
{{- if $d.project -}}
{{ $d.project }}项目
{{- end -}}
{{- if $d.brand -}}
+2
View File
@@ -1427,6 +1427,7 @@ func (dispatcher *DBModelDispatcher) Create(ctx context.Context, query jsonutils
notes := model.GetShortDesc(ctx)
OpsLog.LogEvent(model, ACT_CREATE, notes, userCred)
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_CREATE, notes, userCred, true)
CallCreateNotifyHook(ctx, userCred, model)
}
manager.OnCreateComplete(ctx, []IModel{model}, userCred, ownerId, query, []jsonutils.JSONObject{data})
return getItemDetails(manager, model, ctx, userCred, query)
@@ -1877,6 +1878,7 @@ func DeleteModel(ctx context.Context, userCred mcclient.TokenCredential, item IM
if userCred != nil {
OpsLog.LogEvent(item, ACT_DELETE, item.GetShortDesc(ctx), userCred)
logclient.AddSimpleActionLog(item, logclient.ACT_DELETE, item.GetShortDesc(ctx), userCred, true)
CallDeleteNotifyHook(ctx, userCred, item)
}
if _, ok := item.(IStandaloneModel); ok && len(item.GetId()) > 0 {
err := Metadata.RemoveAll(ctx, item, userCred)
+32
View File
@@ -22,9 +22,13 @@ import (
var (
updateNotifyHook updateNotifyHookFunc
createNotifyHook createNotifyHookFunc
deleteNotifyHook deleteNotifyHookFunc
)
type updateNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel)
type createNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel)
type deleteNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel)
func SetUpdateNotifyHook(f updateNotifyHookFunc) {
if updateNotifyHook != nil {
@@ -33,9 +37,37 @@ func SetUpdateNotifyHook(f updateNotifyHookFunc) {
updateNotifyHook = f
}
func SetCreateNotifyHook(f createNotifyHookFunc) {
if createNotifyHook != nil {
panic("createNotifyHook already set")
}
createNotifyHook = f
}
func SetDeleteNotifyHook(f deleteNotifyHookFunc) {
if deleteNotifyHook != nil {
panic("deleteNotifyHook already set")
}
deleteNotifyHook = f
}
func CallUpdateNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) {
if updateNotifyHook == nil {
return
}
updateNotifyHook(ctx, userCred, obj)
}
func CallCreateNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) {
if createNotifyHook == nil {
return
}
createNotifyHook(ctx, userCred, obj)
}
func CallDeleteNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) {
if deleteNotifyHook == nil {
return
}
deleteNotifyHook(ctx, userCred, obj)
}
+22
View File
@@ -55,6 +55,28 @@ func init() {
Action: ActionUpdate,
})
})
db.SetCreateNotifyHook(func(ctx context.Context, userCred mcclient.TokenCredential, obj db.IModel) {
_, ok := notifyDBHookResources.Load(obj.KeywordPlural())
if !ok {
return
}
EventNotify(ctx, userCred, SEventNotifyParam{
Obj: obj,
Action: ActionCreate,
})
})
db.SetDeleteNotifyHook(func(ctx context.Context, userCred mcclient.TokenCredential, obj db.IModel) {
_, ok := notifyDBHookResources.Load(obj.KeywordPlural())
if !ok {
return
}
EventNotify(ctx, userCred, SEventNotifyParam{
Obj: obj,
Action: ActionDelete,
})
})
}
func AddNotifyDBHookResources(keywordPlurals ...string) {
+2 -1
View File
@@ -90,7 +90,8 @@ func init() {
}
HostManager.SetVirtualObject(HostManager)
HostManager.SetAlias("baremetal", "baremetals")
HostManager.NameRequireAscii = false
notifyclient.AddNotifyDBHookResources(HostManager.KeywordPlural(), GuestManager.AliasPlural())
GuestManager.NameRequireAscii = false
}
type SHost struct {
+5
View File
@@ -114,6 +114,11 @@ func init() {
api.TEMPLATE_LANG_EN,
api.TEMPLATE_LANG_CN,
},
sI18nElme{
api.TOPIC_RESOURCE_HOST,
"host",
"宿主机",
},
sI18nElme{
api.TOPIC_RESOURCE_SERVER,
"virtual machine",
+3
View File
@@ -159,6 +159,7 @@ func (sm *STopicManager) InitializeData() error {
switch name {
case DefaultResourceCreateDelete:
t.addResources(
notify.TOPIC_RESOURCE_HOST,
notify.TOPIC_RESOURCE_SERVER,
notify.TOPIC_RESOURCE_SCALINGGROUP,
notify.TOPIC_RESOURCE_IMAGE,
@@ -202,6 +203,7 @@ func (sm *STopicManager) InitializeData() error {
t.TitleEn = api.COMMON_TITLE_EN
case DefaultResourceChangeConfig:
t.addResources(
notify.TOPIC_RESOURCE_HOST,
notify.TOPIC_RESOURCE_SERVER,
notify.TOPIC_RESOURCE_DBINSTANCE,
notify.TOPIC_RESOURCE_ELASTICCACHE,
@@ -219,6 +221,7 @@ func (sm *STopicManager) InitializeData() error {
notify.TOPIC_RESOURCE_DBINSTANCE,
notify.TOPIC_RESOURCE_ELASTICCACHE,
notify.TOPIC_RESOURCE_USER,
notify.TOPIC_RESOURCE_HOST,
)
t.addAction(notify.ActionUpdate)
t.addAction(notify.ActionRebuildRoot)