mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
feat(notify): add more config for webhook
This commit is contained in:
@@ -149,6 +149,9 @@ type SendParams struct {
|
||||
Event string
|
||||
Receivers SNotifyReceiver
|
||||
EmailMsg *SEmailMessage
|
||||
Header jsonutils.JSONObject
|
||||
Body jsonutils.JSONObject
|
||||
MsgKey string
|
||||
DomainId string
|
||||
RemoteTemplateParam SRemoteTemplateParam
|
||||
}
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
|
||||
package notify
|
||||
|
||||
import "yunion.io/x/onecloud/pkg/apis"
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
)
|
||||
|
||||
type RobotCreateInput struct {
|
||||
apis.SharableVirtualResourceCreateInput
|
||||
@@ -28,7 +32,10 @@ type RobotCreateInput struct {
|
||||
Address string `json:"address"`
|
||||
// description: Language preference
|
||||
// example: zh_CN
|
||||
Lang string `json:"lang"`
|
||||
Lang string `json:"lang"`
|
||||
Header jsonutils.JSONObject `json:"header"`
|
||||
Body jsonutils.JSONObject `json:"body"`
|
||||
MsgKey string `json:"msg_key"`
|
||||
}
|
||||
|
||||
type RobotDetails struct {
|
||||
@@ -54,5 +61,8 @@ type RobotUpdateInput struct {
|
||||
Address string `json:"address"`
|
||||
// description: Language preference
|
||||
// example: en
|
||||
Lang string `json:"lang"`
|
||||
Lang string `json:"lang"`
|
||||
Header jsonutils.JSONObject `json:"header"`
|
||||
Body jsonutils.JSONObject `json:"body"`
|
||||
MsgKey string `json:"msg_key"`
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package notify
|
||||
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options"
|
||||
)
|
||||
@@ -36,10 +37,25 @@ type RobotCreateOptions struct {
|
||||
Type string `choices:"feishu|dingtalk|workwx|webhook"`
|
||||
Address string
|
||||
Lang string
|
||||
Header string
|
||||
Body string
|
||||
MsgKey string
|
||||
}
|
||||
|
||||
func (rc *RobotCreateOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return jsonutils.Marshal(rc), nil
|
||||
dict := jsonutils.NewDict()
|
||||
jsonutils.Update(&dict, rc)
|
||||
header, err := jsonutils.Parse([]byte(rc.Header))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse header")
|
||||
}
|
||||
dict.Set("header", header)
|
||||
body, err := jsonutils.Parse([]byte(rc.Body))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse body")
|
||||
}
|
||||
dict.Set("body", body)
|
||||
return dict, nil
|
||||
}
|
||||
|
||||
type RobotOptions struct {
|
||||
@@ -62,8 +78,23 @@ type RobotUpdateOptions struct {
|
||||
type SrobotUpdateOptions struct {
|
||||
Address string
|
||||
Lang string
|
||||
Header string
|
||||
Body string
|
||||
MsgKey string
|
||||
}
|
||||
|
||||
func (ru *RobotUpdateOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return jsonutils.Marshal(ru.SrobotUpdateOptions), nil
|
||||
dict := jsonutils.NewDict()
|
||||
jsonutils.Update(&dict, ru)
|
||||
header, err := jsonutils.Parse([]byte(ru.Header))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse header")
|
||||
}
|
||||
dict.Set("header", header)
|
||||
body, err := jsonutils.Parse([]byte(ru.Body))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse body")
|
||||
}
|
||||
dict.Set("body", body)
|
||||
return dict, nil
|
||||
}
|
||||
|
||||
@@ -59,9 +59,12 @@ type SRobot struct {
|
||||
db.SSharableVirtualResourceBase
|
||||
db.SEnabledResourceBase
|
||||
|
||||
Type string `width:"16" nullable:"false" create:"required" get:"user" list:"user" index:"true"`
|
||||
Address string `nullable:"false" create:"required" update:"user" get:"user" list:"user"`
|
||||
Lang string `width:"16" nullable:"false" create:"required" update:"user" get:"user" list:"user"`
|
||||
Type string `width:"16" nullable:"false" create:"required" get:"user" list:"user" index:"true"`
|
||||
Address string `nullable:"false" create:"required" update:"user" get:"user" list:"user"`
|
||||
Lang string `width:"16" nullable:"false" create:"required" update:"user" get:"user" list:"user"`
|
||||
Header jsonutils.JSONObject `length:"long" charset:"utf8" nullable:"true" list:"user" create:"optional" update:"user"`
|
||||
Body jsonutils.JSONObject `length:"long" charset:"utf8" nullable:"true" list:"user" create:"optional" update:"user"`
|
||||
MsgKey string `width:"16" nullable:"true" update:"user" get:"user" list:"user"`
|
||||
}
|
||||
|
||||
var RobotList = []string{api.FEISHU_ROBOT, api.DINGTALK_ROBOT, api.WORKWX_ROBOT, api.WEBHOOK, api.WEBHOOK_ROBOT}
|
||||
@@ -93,6 +96,9 @@ func (rm *SRobotManager) ValidateCreateData(ctx context.Context, userCred mcclie
|
||||
Contact: input.Address,
|
||||
DomainId: input.ProjectDomainId,
|
||||
},
|
||||
Header: input.Header,
|
||||
Body: input.Body,
|
||||
MsgKey: input.MsgKey,
|
||||
Title: "Validate",
|
||||
Message: "This is a verification message, please ignore.",
|
||||
})
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/httputils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/notify"
|
||||
@@ -35,20 +35,37 @@ func (self *SWebhookSender) GetSenderType() string {
|
||||
}
|
||||
|
||||
func (self *SWebhookSender) Send(args api.SendParams) error {
|
||||
log.Infoln("this is in webhookSend ")
|
||||
body, err := jsonutils.ParseString(args.Message)
|
||||
if err != nil {
|
||||
log.Errorf("unable to parse %q: %v", args.Message, err)
|
||||
return errors.Wrapf(err, "unable to parse %q", args.Message)
|
||||
}
|
||||
if _, ok := body.(*jsonutils.JSONString); err != nil || ok {
|
||||
dict := jsonutils.NewDict()
|
||||
dict.Set("Msg", jsonutils.NewString(args.Message))
|
||||
if len(args.MsgKey) == 0 {
|
||||
dict.Set("Msg", jsonutils.NewString(args.Message))
|
||||
} else {
|
||||
dict.Set(args.MsgKey, jsonutils.NewString(args.Message))
|
||||
}
|
||||
if args.Body != nil {
|
||||
jsonutils.Update(dict, args.Body)
|
||||
}
|
||||
body = dict
|
||||
}
|
||||
event := strings.ToUpper(args.Event)
|
||||
header := http.Header{}
|
||||
if args.Header != nil {
|
||||
resmap, _ := args.Header.GetMap()
|
||||
for k, v := range resmap {
|
||||
vStr, err := v.GetString()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
header.Set(k, vStr)
|
||||
}
|
||||
}
|
||||
|
||||
header.Set(EVENT_HEADER, event)
|
||||
_, _, err = httputils.JSONRequest(cli, ctx, httputils.POST, args.Receivers.Contact, header, body, true)
|
||||
_, _, err = httputils.JSONRequest(cli, ctx, httputils.POST, args.Receivers.Contact, header, body, false)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -271,6 +271,9 @@ func (notificationSendTask *NotificationSendTask) batchSend(ctx context.Context,
|
||||
robot := receivers[i].receiver.(*models.SRobot)
|
||||
driver := models.GetDriver(fmt.Sprintf("%s-robot", robot.Type))
|
||||
params.Receivers.Contact = robot.Address
|
||||
params.Header = robot.Header
|
||||
params.Body = robot.Body
|
||||
params.MsgKey = robot.MsgKey
|
||||
err = driver.Send(params)
|
||||
if err != nil {
|
||||
fails = append(fails, FailedReceiverSpec{ReceiverSpec: receivers[i], Reason: err.Error()})
|
||||
|
||||
Reference in New Issue
Block a user