mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/yunionio/cloudpods.git
synced 2026-09-20 08:03:53 +08:00
* refactor(notify): ALL * feat(climc): Notify v2 * feat: support notify for codegen.py * delete deprecated code and Maintain SDK compatibility * Adjust the template for mail validation to support captcha * fix: The issue of notify refactoring * add copyright for notifyv2 cli * Adjust according to the advice of make check * Remove utils package in notify * fix import format * remove redundant Template module * fix validateCreate and validateUpdate of notifytemplate * fix update issues of receivers * fix: Add SetStageComplete in tasks
110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
api "yunion.io/x/onecloud/pkg/apis/notify"
|
|
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
)
|
|
|
|
var ReceiverNotificationManager *SReceiverNotificationManager
|
|
|
|
func init() {
|
|
db.InitManager(func() {
|
|
ReceiverNotificationManager = &SReceiverNotificationManager{
|
|
SJointResourceBaseManager: db.NewJointResourceBaseManager(
|
|
SReceiverNotification{},
|
|
"receivernotification_tbl",
|
|
"receivernotification",
|
|
"receivernotifications",
|
|
NotificationManager,
|
|
ReceiverManager,
|
|
),
|
|
}
|
|
ReceiverNotificationManager.SetVirtualObject(ReceiverNotificationManager)
|
|
})
|
|
}
|
|
|
|
type SReceiverNotificationManager struct {
|
|
db.SJointResourceBaseManager
|
|
}
|
|
|
|
// +onecloud:swagger-gen-ignore
|
|
type SReceiverNotification struct {
|
|
db.SJointResourceBase
|
|
|
|
ReceiverID string `width:"128" charset:"ascii" nullable:"false"`
|
|
NotificationID string `width:"128" charset:"ascii" nullable:"false"`
|
|
// ignore if ReceiverID is not empty
|
|
Contact string `width:"128" nullable:"false"`
|
|
SendAt time.Time `nullable:"false"`
|
|
SendBy string `width:"128" nullable:"false"`
|
|
Status string `width:"36" charset:"ascii"`
|
|
FailedReason string `width:"1024"`
|
|
}
|
|
|
|
func (rnm *SReceiverNotificationManager) Create(ctx context.Context, userCred mcclient.TokenCredential, receiverID, notificationID string) (*SReceiverNotification, error) {
|
|
rn := &SReceiverNotification{
|
|
ReceiverID: receiverID,
|
|
NotificationID: notificationID,
|
|
Status: api.RECEIVER_NOTIFICATION_RECEIVED,
|
|
SendBy: userCred.GetUserId(),
|
|
}
|
|
return rn, rnm.TableSpec().Insert(ctx, rn)
|
|
}
|
|
|
|
func (rnm *SReceiverNotificationManager) GetMasterFieldName() string {
|
|
return "notification_id"
|
|
}
|
|
|
|
func (rnm *SReceiverNotificationManager) GetSlaveFieldName() string {
|
|
return "receiver_id"
|
|
}
|
|
|
|
func (rnm *SReceiverNotificationManager) CreateWithoutReceiver(ctx context.Context, userCred mcclient.TokenCredential, contact, notificationID string) (*SReceiverNotification, error) {
|
|
rn := &SReceiverNotification{
|
|
NotificationID: notificationID,
|
|
Contact: contact,
|
|
Status: api.RECEIVER_NOTIFICATION_RECEIVED,
|
|
SendBy: userCred.GetUserId(),
|
|
}
|
|
return rn, rnm.TableSpec().Insert(ctx, rn)
|
|
}
|
|
|
|
func (rn *SReceiverNotification) Receiver() (*SReceiver, error) {
|
|
q := ReceiverManager.Query().Equals("id", rn.ReceiverID)
|
|
var receiver SReceiver
|
|
err := q.First(&receiver)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &receiver, nil
|
|
}
|
|
|
|
func (rn *SReceiverNotification) BeforeSend(ctx context.Context, sendTime time.Time) error {
|
|
if sendTime.IsZero() {
|
|
sendTime = time.Now()
|
|
}
|
|
_, err := db.Update(rn, func() error {
|
|
rn.SendAt = sendTime
|
|
rn.Status = api.RECEIVER_NOTIFICATION_SENT
|
|
return nil
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (rn *SReceiverNotification) AfterSend(ctx context.Context, success bool, reason string) error {
|
|
_, err := db.Update(rn, func() error {
|
|
if success {
|
|
rn.Status = api.RECEIVER_NOTIFICATION_OK
|
|
} else {
|
|
rn.Status = api.RECEIVER_NOTIFICATION_FAIL
|
|
rn.FailedReason = reason
|
|
}
|
|
return nil
|
|
})
|
|
return err
|
|
}
|