feat(notify): allow empty receivers for robot or webhook contact type

This commit is contained in:
rainzm
2020-10-17 18:04:34 +08:00
parent 012d9b1588
commit 2e094a310a
3 changed files with 31 additions and 21 deletions
+17 -12
View File
@@ -139,19 +139,15 @@ func (cm *SConfigManager) AllowPerformGetTypes(ctx context.Context, userCred mcc
return true
}
func (cm *SConfigManager) PerformGetTypes(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ConfigManagerGetTypesInput) (api.ConfigManagerGetTypesOutput, error) {
output := api.ConfigManagerGetTypesOutput{}
allContactType, err := cm.allContactType()
if err != nil {
return output, err
}
func (cm *SConfigManager) filterContactType(cTypes []string, robot string) []string {
var judge func(string) bool
switch input.Robot {
case "only":
ret := make([]string, 0, len(cTypes)/2)
switch robot {
case api.CTYPE_ROBOT_ONLY:
judge = func(ctype string) bool {
return strings.Contains(ctype, "robot")
}
case "yes":
case api.CTYPE_ROBOT_YES:
judge = func(ctype string) bool {
return true
}
@@ -160,12 +156,21 @@ func (cm *SConfigManager) PerformGetTypes(ctx context.Context, userCred mcclient
return !strings.Contains(ctype, "robot")
}
}
for _, ctype := range allContactType {
for _, ctype := range cTypes {
if judge(ctype) {
output.Types = append(output.Types, ctype)
ret = append(ret, ctype)
}
}
output.Types = sortContactType(output.Types)
return ret
}
func (cm *SConfigManager) PerformGetTypes(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ConfigManagerGetTypesInput) (api.ConfigManagerGetTypesOutput, error) {
output := api.ConfigManagerGetTypesOutput{}
allContactType, err := cm.allContactType()
if err != nil {
return output, err
}
output.Types = sortContactType(cm.filterContactType(allContactType, input.Robot))
return output, nil
}
+13 -4
View File
@@ -77,12 +77,18 @@ func (nm *SNotificationManager) ValidateCreateData(ctx context.Context, userCred
if err != nil {
return input, err
}
if !utils.IsInStringArray(input.ContactType, allContactType) {
switch {
case input.ContactType == api.WEBHOOK:
case utils.IsInStringArray(input.ContactType, ConfigManager.filterContactType(allContactType, "")):
//check uids, rids and contacts
if len(input.Receivers) == 0 && len(input.Contacts) == 0 {
return input, httperrors.NewMissingParameterError("receivers | contacts")
}
case utils.IsInStringArray(input.ContactType, ConfigManager.filterContactType(allContactType, api.CTYPE_ROBOT_ONLY)):
default:
return input, httperrors.NewInputParameterError("Unconfigured contact type %q", input.ContactType)
}
// check uids, rids and contacts
if len(input.Receivers) == 0 && len(input.Contacts) == 0 {
return input, httperrors.NewMissingParameterError("receivers | contacts")
if !utils.IsInStringArray(input.ContactType, allContactType) {
}
// check receivers
if len(input.Receivers) > 0 {
@@ -181,6 +187,9 @@ func (n *SNotification) ReceiverNotificationsNotOK() ([]SReceiverNotification, e
rnq := ReceiverNotificationManager.Query().Equals("notification_id", n.Id).NotEquals("status", api.RECEIVER_NOTIFICATION_OK)
rns := make([]SReceiverNotification, 0, 1)
err := db.FetchModelObjects(ReceiverNotificationManager, rnq, &rns)
if err == sql.ErrNoRows {
return []SReceiverNotification{}, nil
}
if err != nil {
return nil, err
}
+1 -5
View File
@@ -104,10 +104,6 @@ func (self *NotificationSendTask) OnInit(ctx context.Context, obj db.IStandalone
contactMap[contact] = &rns[i]
}
if len(contactMap) == 0 {
self.taskFailed(ctx, notification, strings.Join(failedRecord, "; "), true)
}
// set status before send
now := time.Now()
contacts := make([]string, 0, len(contactMap))
@@ -138,7 +134,7 @@ func (self *NotificationSendTask) OnInit(ctx context.Context, obj db.IStandalone
for _, rn := range contactMap {
rn.AfterSend(ctx, true, "")
}
if len(failedRecord) == len(contacts) {
if len(failedRecord) > 0 && len(failedRecord) == len(contacts) {
self.taskFailed(ctx, notification, strings.Join(failedRecord, "; "), true)
return
}