From 4b2509d72b58f1c2d2e6762eedcdd23d3a2ed1e4 Mon Sep 17 00:00:00 2001 From: gouqi11 <66834753+gouqi11@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:35:07 +0800 Subject: [PATCH] feat(notify): support project notify (#19259) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 马鸿飞 --- pkg/apis/notify/const.go | 1 + pkg/compute/models/hosts.go | 4 ++++ pkg/keystone/models/projects.go | 16 +++++++++++++++- pkg/notify/models/event_template.go | 5 +++++ pkg/notify/models/topic.go | 3 +++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/apis/notify/const.go b/pkg/apis/notify/const.go index fac3795ed1..ef94534f14 100644 --- a/pkg/apis/notify/const.go +++ b/pkg/apis/notify/const.go @@ -137,6 +137,7 @@ const ( TOPIC_RESOURCE_NET = "net" TOPIC_RESOURCE_SERVICE = "service" TOPIC_RESOURCE_VM_INTEGRITY_CHECK = "vm_integrity" + TOPIC_RESOURCE_PROJECT = "project" SUBSCRIBER_TYPE_ROLE = "role" SUBSCRIBER_TYPE_ROBOT = "robot" diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index df129df359..8e05b8627b 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -6033,6 +6033,10 @@ func (host *SHost) SetStatus(userCred mcclient.TokenCredential, status string, r return err } host.ClearSchedDescCache() + notifyclient.EventNotify(context.Background(), userCred, notifyclient.SEventNotifyParam{ + Obj: host, + Action: notifyclient.ActionUpdate, + }) return nil } diff --git a/pkg/keystone/models/projects.go b/pkg/keystone/models/projects.go index 5c1d97c5e8..fd63393347 100644 --- a/pkg/keystone/models/projects.go +++ b/pkg/keystone/models/projects.go @@ -34,6 +34,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/keystone/options" "yunion.io/x/onecloud/pkg/mcclient" @@ -59,6 +60,7 @@ func init() { ), } ProjectManager.SetVirtualObject(ProjectManager) + notifyclient.AddNotifyDBHookResources(ProjectManager.KeywordPlural(), ProjectManager.AliasPlural()) } /* @@ -384,7 +386,15 @@ func (proj *SProject) ValidateDeleteCondition(ctx context.Context, info *api.Pro } func (proj *SProject) Delete(ctx context.Context, userCred mcclient.TokenCredential) error { - return proj.SIdentityBaseResource.Delete(ctx, userCred) + err := proj.SIdentityBaseResource.Delete(ctx, userCred) + if err != nil { + return errors.Wrap(err, "project delete") + } + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: proj, + Action: notifyclient.ActionDelete, + }) + return nil } func (proj *SProject) IsAdminProject() bool { @@ -556,6 +566,10 @@ func (project *SProject) PostCreate( if err != nil { log.Errorf("CancelPendingUsage fail %s", err) } + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: project, + Action: notifyclient.ActionCreate, + }) } func threeMemberSystemValidatePolicies(userCred mcclient.TokenCredential, projectId string, assignPolicies rbacutils.TPolicyGroup) error { diff --git a/pkg/notify/models/event_template.go b/pkg/notify/models/event_template.go index 82c52fed32..f505f34e21 100644 --- a/pkg/notify/models/event_template.go +++ b/pkg/notify/models/event_template.go @@ -294,6 +294,11 @@ func init() { "action log", "操作日志", }, + sI18nElme{ + api.TOPIC_RESOURCE_PROJECT, + "project", + "项目", + }, sI18nElme{ api.TOPIC_RESOURCE_CLOUDPODS_COMPONENT, "cloudpods component", diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index d1909f9724..a56db8ed89 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -195,6 +195,7 @@ func (sm *STopicManager) InitializeData() error { notify.TOPIC_RESOURCE_DNSRECORDSET, notify.TOPIC_RESOURCE_LOADBALANCERLISTENER, notify.TOPIC_RESOURCE_LOADBALANCERBACKEDNGROUP, + notify.TOPIC_RESOURCE_PROJECT, ) t.addAction( notify.ActionCreate, @@ -228,6 +229,7 @@ func (sm *STopicManager) InitializeData() error { notify.TOPIC_RESOURCE_ELASTICCACHE, notify.TOPIC_RESOURCE_USER, notify.TOPIC_RESOURCE_HOST, + notify.TOPIC_RESOURCE_PROJECT, ) t.addAction(notify.ActionUpdate) t.addAction(notify.ActionRebuildRoot) @@ -820,6 +822,7 @@ func init() { notify.TOPIC_RESOURCE_NET: 39, notify.TOPIC_RESOURCE_SERVICE: 40, notify.TOPIC_RESOURCE_VM_INTEGRITY_CHECK: 41, + notify.TOPIC_RESOURCE_PROJECT: 42, }, ) converter.registerAction(