diff --git a/pkg/monitor/models/alert.go b/pkg/monitor/models/alert.go index cd6462b528..27f1e09f61 100644 --- a/pkg/monitor/models/alert.go +++ b/pkg/monitor/models/alert.go @@ -21,6 +21,7 @@ import ( "time" "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/rbacscope" "yunion.io/x/sqlchemy" @@ -36,7 +37,8 @@ import ( ) const ( - AlertMetadataTitle = "alert_title" + AlertMetadataTitle = "alert_title" + AlertMetadataChannel = "alert_channel" ) var ( @@ -179,6 +181,22 @@ func (alert *SAlert) GetTitle() string { return alert.GetMetadata(context.Background(), AlertMetadataTitle, nil) } +func (alert *SAlert) SetChannel(ctx context.Context, channel []string) error { + return alert.SetMetadata(ctx, AlertMetadataChannel, jsonutils.Marshal(channel), nil) +} + +func (alert *SAlert) GetChannel() []string { + channelJson := alert.GetMetadataJson(context.Background(), AlertMetadataChannel, nil) + if channelJson == nil { + return []string{} + } + channel := []string{} + if err := channelJson.Unmarshal(&channel); err != nil { + log.Warningf("get channel failed when unmarshal: %v", err) + } + return channel +} + func (alert *SAlert) ShouldUpdateState(newState monitor.AlertStateType) bool { return monitor.AlertStateType(alert.State) != newState } diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index 2bd9cad724..f0da6cd8db 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -379,6 +379,7 @@ func (alert *SCommonAlert) customizeCreateNotis(ctx context.Context, userCred mc return alert.createAlertNoti(ctx, userCred, input.Name, s, input.SilentPeriod, true) } + alert.SetChannel(ctx, input.Channel) for _, channel := range input.Channel { s := &monitor.NotificationSettingOneCloud{ Channel: channel, @@ -1096,6 +1097,7 @@ func (alert *SCommonAlert) PostUpdate( if err := alert.UpdateNotification(ctx, userCred, query, data); err != nil { log.Errorf("update notification error: %v", err) } + alert.SetChannel(ctx, updateInput.Channel) } if _, err := data.GetString("scope"); err == nil { _, err = alert.PerformSetScope(ctx, userCred, query, data)