From 51f0e6f10a20081e9e9ffda18d55200d0be599d8 Mon Sep 17 00:00:00 2001 From: rainzm Date: Wed, 13 Jan 2021 19:36:49 +0800 Subject: [PATCH] feat(notify): add params for create notification 1. tag, usually used to filter 2. metadata 3. ignore_nonexistent_receiver --- pkg/apis/notify/const.go | 2 ++ pkg/apis/notify/notification.go | 7 +++++++ pkg/notify/models/notification.go | 23 ++++++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/apis/notify/const.go b/pkg/apis/notify/const.go index dd34a96246..7667d60091 100644 --- a/pkg/apis/notify/const.go +++ b/pkg/apis/notify/const.go @@ -58,6 +58,8 @@ const ( NOTIFICATION_STATUS_OK = "ok" NOTIFICATION_STATUS_PART_OK = "part_ok" + NOTIFICATION_TAG_ALERT = "alert" + TEMPLATE_TYPE_TITLE = "title" TEMPLATE_TYPE_CONTENT = "content" TEMPLATE_TYPE_REMOTE = "remote" diff --git a/pkg/apis/notify/notification.go b/pkg/apis/notify/notification.go index c3dead2c70..93733534dd 100644 --- a/pkg/apis/notify/notification.go +++ b/pkg/apis/notify/notification.go @@ -46,6 +46,12 @@ type NotificationCreateInput struct { // description: message content or jsonobject // required: ture Message string `json:"message"` + // description: notification tag + // required: false + // example: alert + Tag string `json:"tag"` + Metadata map[string]interface{} `json:"metadata"` + IgnoreNonexistentReceiver bool `json:"ignore_nonexistent_receiver"` } type ReceiveDetail struct { @@ -73,4 +79,5 @@ type NotificationListInput struct { ContactType string ReceiverId string + Tag string } diff --git a/pkg/notify/models/notification.go b/pkg/notify/models/notification.go index 9dafa0e853..92b0838614 100644 --- a/pkg/notify/models/notification.go +++ b/pkg/notify/models/notification.go @@ -70,6 +70,7 @@ type SNotification struct { Message string `create:"required"` ReceivedAt time.Time `nullable:"true" list:"user" get:"user"` SendTimes int + Tag string `width:"16" nullable:"true" index:"true" create:"optional"` } const ( @@ -77,6 +78,9 @@ const ( ) func (nm *SNotificationManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.NotificationCreateInput) (api.NotificationCreateInput, error) { + if len(input.Tag) == 0 && utils.IsInStringArray(input.Tag, []string{api.NOTIFICATION_TAG_ALERT}) { + return input, httperrors.NewInputParameterError("invalid tag") + } // compatible if len(input.Receivers) != 0 && input.ContactType == api.WEBCONSOLE { input.Contacts = input.Receivers @@ -115,9 +119,14 @@ func (nm *SNotificationManager) ValidateCreateData(ctx context.Context, userCred if idSet.Has(re) || nameSet.Has(re) { continue } - return input, httperrors.NewInputParameterError("no such receiver whose uid is %q", re) + if !input.IgnoreNonexistentReceiver { + return input, httperrors.NewInputParameterError("no such receiver whose uid is %q", re) + } } input.Receivers = idSet.UnsortedList() + if len(input.Receivers)+len(input.Contacts) == 0 { + return input, httperrors.NewInputParameterError("no valid receiver or contact") + } } nowStr := time.Now().Format("2006-01-02 15:04:05") if len(input.Priority) == 0 { @@ -160,6 +169,15 @@ func (n *SNotification) CustomizeCreate(ctx context.Context, userCred mcclient.T } func (n *SNotification) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) { + if data.Contains("metadata") { + metadata := make(map[string]interface{}) + err := data.Unmarshal(&metadata, "metadata") + if err != nil { + log.Errorf("unable to unmarshal to metadata: %v", err) + } else { + n.SetAllMetadata(ctx, metadata, userCred) + } + } n.SetStatus(userCred, api.NOTIFICATION_STATUS_RECEIVED, "") task, err := taskman.TaskManager.NewTask(ctx, "NotificationSendTask", n, userCred, nil, "", "") if err != nil { @@ -442,6 +460,9 @@ func (nm *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchemy. subq := ReceiverNotificationManager.Query("notification_id").Equals("receiver_id", input.ReceiverId).SubQuery() q = q.Join(subq, sqlchemy.Equals(q.Field("id"), subq.Field("notification_id"))) } + if len(input.Tag) > 0 { + q = q.Equals("tag", input.Tag) + } return q, nil }