From 7ed99506a5f2ce6d0d88260aa38937f9222be58e Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 17 Nov 2020 16:15:06 +0800 Subject: [PATCH 1/3] fix(notify): be compatible with keystone's UsersV3.GetById Fill UsersV3.GetById with user's name, it will return user's info. --- pkg/notify/models/receiver.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/notify/models/receiver.go b/pkg/notify/models/receiver.go index 49156ee76d..6e421fa984 100644 --- a/pkg/notify/models/receiver.go +++ b/pkg/notify/models/receiver.go @@ -243,6 +243,8 @@ func (rm *SReceiverManager) ValidateCreateData(ctx context.Context, userCred mcc return input, err } uname, _ := userObj.GetString("name") + uid, _ := userObj.GetString("id") + input.UID = uid input.UName = uname domainId, _ := userObj.GetString("domain_id") input.ProjectDomainId = domainId @@ -260,7 +262,9 @@ func (rm *SReceiverManager) ValidateCreateData(ctx context.Context, userCred mcc return input, err } uid, _ := userObj.GetString("id") + uname, _ := userObj.GetString("name") input.UID = uid + input.UName = uname domainId, _ := userObj.GetString("domain_id") input.ProjectDomainId = domainId } From a4dc42e69a3ccb6db0970a605fcbd747c5f73f89 Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 17 Nov 2020 16:19:41 +0800 Subject: [PATCH 2/3] feat(notify): enable watching user in keystone --- pkg/notify/models/receiver.go | 6 ++++++ pkg/notify/service/service.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/pkg/notify/models/receiver.go b/pkg/notify/models/receiver.go index 6e421fa984..f53d97a141 100644 --- a/pkg/notify/models/receiver.go +++ b/pkg/notify/models/receiver.go @@ -866,6 +866,9 @@ func (rm *SReceiverManager) OnUpdate(oldObj, newObj *jsonutils.JSONDict) { log.Errorf("fail to FetchByIDs: %v", err) return } + if len(receivers) == 0 { + return + } receiver := &receivers[0] uname, _ := newObj.GetString("name") domainId, _ := newObj.GetString("domain_id") @@ -889,6 +892,9 @@ func (rm *SReceiverManager) OnDelete(obj *jsonutils.JSONDict) { log.Errorf("fail to FetchByIDs: %v", err) return } + if len(receivers) == 0 { + return + } receiver := &receivers[0] err = receiver.Delete(context.Background(), auth.GetAdminSession(context.Background(), "", "").GetToken()) if err != nil { diff --git a/pkg/notify/service/service.go b/pkg/notify/service/service.go index d2b6cf9a20..353953eab2 100644 --- a/pkg/notify/service/service.go +++ b/pkg/notify/service/service.go @@ -58,6 +58,11 @@ func StartService() { db.EnsureAppInitSyncDB(applicaion, dbOpts, models.InitDB) defer cloudcommon.CloseDB() + err := models.ReceiverManager.StartWatchUserInKeystone() + if err != nil { + log.Logger().Panic(err.Error()) + } + // init notify service models.NotifyService = rpc.NewSRpcService(opts.SocketFileDir, models.ConfigManager, models.TemplateManager) models.NotifyService.InitAll() From 36dec7baa089b4314bbdb895dec8ddcb8c3aecdf Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 16 Nov 2020 10:13:08 +0000 Subject: [PATCH 3/3] fix: add err handler for notifyRobot --- pkg/cloudcommon/notifyclient/notify.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/cloudcommon/notifyclient/notify.go b/pkg/cloudcommon/notifyclient/notify.go index 3d2c8e060a..d5ec46313d 100644 --- a/pkg/cloudcommon/notifyclient/notify.go +++ b/pkg/cloudcommon/notifyclient/notify.go @@ -333,7 +333,13 @@ func notifyRobot(ctx context.Context, robot string, recipientId []string, isGrou if err != nil { return err } - jarray, _ := result.Get("types") + if result == nil { + return nil + } + jarray, err := result.Get("types") + if err != nil { + return err + } cTypes := jarray.(*jsonutils.JSONArray).GetStringArray() for _, ct := range cTypes { RawNotifyWithCtx(ctx, recipientId, isGroup, npk.TNotifyChannel(ct), priority, event, data)