From 2a3dccfe15018b2bd92300bbe987e6fbbf1651ea Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 29 Oct 2021 19:33:15 +0800 Subject: [PATCH] feat(notify): add WebconsoleDisable for topic --- pkg/notify/models/notification.go | 26 +++++++++++++++++++------- pkg/notify/models/topic.go | 13 ++++++++----- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pkg/notify/models/notification.go b/pkg/notify/models/notification.go index 2e9ed39fe6..4ffa4d182a 100644 --- a/pkg/notify/models/notification.go +++ b/pkg/notify/models/notification.go @@ -308,13 +308,16 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred return output, errors.Wrap(err, "unable to create Event") } - // webconsole - err = nm.create(ctx, userCred, api.WEBCONSOLE, receiverIds, webconsoleContacts.UnsortedList(), input.Priority, event.Id) - if err != nil { - output.FailedList = append(output.FailedList, api.FailedElem{ - ContactType: api.WEBCONSOLE, - Reason: err.Error(), - }) + if nm.needWebconsole(topics) { + + // webconsole + err = nm.create(ctx, userCred, api.WEBCONSOLE, receiverIds, webconsoleContacts.UnsortedList(), input.Priority, event.Id) + if err != nil { + output.FailedList = append(output.FailedList, api.FailedElem{ + ContactType: api.WEBCONSOLE, + Reason: err.Error(), + }) + } } // normal contact type for _, ct := range contactTypes { @@ -347,6 +350,15 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred return output, nil } +func (nm *SNotificationManager) needWebconsole(topics []STopic) bool { + for i := range topics { + if topics[i].WebconsoleDisable.IsFalse() { + return true + } + } + return false +} + func (nm *SNotificationManager) createWithWebhookRobots(ctx context.Context, userCred mcclient.TokenCredential, webhookRobotIds []string, priority, eventId string) error { if len(webhookRobotIds) == 0 { return nil diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index c7ad16aaf8..8ef5a82fad 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -71,11 +71,12 @@ type STopic struct { db.SStandaloneResourceBase db.SEnabledResourceBase - Type string `width:"20" nullable:"false" create:"required" update:"user" list:"user"` - Resources uint64 `nullable:"false"` - Actions uint32 `nullable:"false"` - Results uint8 `nullable:"false"` - AdvanceDays int `nullable:"false"` + Type string `width:"20" nullable:"false" create:"required" update:"user" list:"user"` + Resources uint64 `nullable:"false"` + Actions uint32 `nullable:"false"` + Results uint8 `nullable:"false"` + AdvanceDays int `nullable:"false"` + WebconsoleDisable tristate.TriState } const ( @@ -260,6 +261,7 @@ func (sm *STopicManager) InitializeData() error { notify.ActionSyncDelete, ) t.Type = notify.TOPIC_TYPE_RESOURCE + t.WebconsoleDisable = tristate.True } if topic == nil { err := sm.TableSpec().Insert(ctx, t) @@ -271,6 +273,7 @@ func (sm *STopicManager) InitializeData() error { topic.Resources = t.Resources topic.Actions = t.Actions topic.Type = t.Type + topic.WebconsoleDisable = t.WebconsoleDisable return nil }) if err != nil {