diff --git a/pkg/notify/dispatcher.go b/pkg/notify/dispatcher.go index 6db04ba1cf..8283edd7af 100644 --- a/pkg/notify/dispatcher.go +++ b/pkg/notify/dispatcher.go @@ -118,9 +118,10 @@ func (self *NotifyModelDispatcher) UpdateConfig(ctx context.Context, body jsonut createData.Add(jsonutils.NewString(key), "key_text") createData.Add(jsonutils.NewString(contactType), "type") _, err := self.Create(ctx, jsonutils.JSONNull, createData, nil) - config[key] = tmp.String() + value, _ := tmp.GetString() + config[key] = value if err != nil { - return errors.Wrapf(err, "Create config (%s, %s, %s) failed", contactType, key, tmp) + return errors.Wrapf(err, "Create config (%s, %s, %s) failed", contactType, key, value) } } // update config @@ -250,7 +251,10 @@ func (self *NotifyModelDispatcher) VerifyTrigger(ctx context.Context, params map return nil, httperrors.NewGeneralError(err) } processID := verification.ID - models.SendVerifyMessage(userCred, verification, uid, contactType, contact) + err := models.SendVerifyMessage(ctx, userCred, verification, &scontact) + if err != nil { + return nil, err + } ret := map[string]map[string]string{ "contact": { "process_id": processID, diff --git a/pkg/notify/handlers.go b/pkg/notify/handlers.go index 087e5017af..4e2bf9f946 100644 --- a/pkg/notify/handlers.go +++ b/pkg/notify/handlers.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "net/http" - "net/url" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -182,38 +181,6 @@ func AddNotifyDispatcher(prefix string, app *appsrv.Application) { app.AddHandler2("POST", fmt.Sprintf("%s/%s/delete-template", prefix, modelDispatcher.KeywordPlural()), middleware(deleteTemplateHandler), metadata, "delete", tags) - - app.AddHandler2("POST", - fmt.Sprintf("%s/%s/email-url", prefix, modelDispatcher.KeywordPlural()), - middleware(updateEmailUrlHandler), metadata, "update_email_url", tags) - - app.AddHandler2("GET", - fmt.Sprintf("%s/%s/email-url", prefix, modelDispatcher.KeywordPlural()), - middleware(getEmailUrlHandler), metadata, "get_email_url", tags) -} - -func updateEmailUrlHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - userCred := policy.FetchUserCredential(ctx) - if !userCred.HasSystemAdminPrivilege() { - httperrors.ForbiddenError(w, "only system admin can update email url") - return - } - _, _, _, body := fetchEnv(ctx, w, r) - if !body.Contains("email_url") { - httperrors.InputParameterError(w, "miss email_url") - } - emailUrl, _ := body.GetString("email_url") - eUrl, err := url.Parse(emailUrl) - if err != nil { - httperrors.InputParameterError(w, "invalid url") - } - models.TemplateManager.SetEmailUrl(eUrl.String()) -} - -func getEmailUrlHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - ret := jsonutils.NewDict() - ret.Add(jsonutils.NewString(models.TemplateManager.GetEmailUrl()), "email_url") - appsrv.Send(w, ret.PrettyString()) } func templateUpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { diff --git a/pkg/notify/models/mod_config.go b/pkg/notify/models/mod_config.go index 55903240f4..d5a39d1755 100644 --- a/pkg/notify/models/mod_config.go +++ b/pkg/notify/models/mod_config.go @@ -25,6 +25,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/mcclient" _interface "yunion.io/x/onecloud/pkg/notify/interface" + "yunion.io/x/onecloud/pkg/notify/options" ) type SConfigManager struct { @@ -89,6 +90,47 @@ func (self *SConfigManager) InitializeData() error { sql = fmt.Sprintf("update %s set type='mobile' where type='sms_aliyun'", self.TableSpec().Name()) q = sqlchemy.NewRawQuery(sql, "") q.Row() + + // init webconsole's config + q = self.Query().Equals("type", "webconsole") + n, err := q.CountWithError() + if err != nil { + return err + } + if n >= 4 { + return nil + } + sql = fmt.Sprintf("update %s set deleted='1' where type='webconsole'", self.TableSpec().Name()) + q = sqlchemy.NewRawQuery(sql) + q.Row() + configs := []SConfig{ + { + Type: "webconsole", + KeyText: "auth_uri", + ValueText: options.Options.AuthURL, + }, + { + Type: "webconsole", + KeyText: "admin_user", + ValueText: options.Options.AdminUser, + }, + { + Type: "webconsole", + KeyText: "admin_password", + ValueText: options.Options.AdminPassword, + }, + { + Type: "webconsole", + KeyText: "admin_tenant_name", + ValueText: options.Options.AdminProject, + }, + } + for _, config := range configs { + err := self.TableSpec().Insert(&config) + if err != nil { + return err + } + } return nil } diff --git a/pkg/notify/models/mod_template.go b/pkg/notify/models/mod_template.go index 0905da87db..503ff43690 100644 --- a/pkg/notify/models/mod_template.go +++ b/pkg/notify/models/mod_template.go @@ -22,7 +22,6 @@ import ( "io/ioutil" "os" "strings" - "sync" ptem "text/template" "yunion.io/x/jsonutils" @@ -31,6 +30,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/notify/options" "yunion.io/x/onecloud/pkg/notify/rpc/apis" "yunion.io/x/onecloud/pkg/notify/template" ) @@ -58,12 +58,6 @@ const ( TEMPLATE_TYPE_REMOTE = "remote" ) -var ( - DefaultEmailUrl = "" - EmailUrl = "" - EmailUrlLock sync.RWMutex -) - type STemplate struct { SStandaloneResourceBase @@ -76,18 +70,7 @@ type STemplate struct { } func (tm *STemplateManager) GetEmailUrl() string { - EmailUrlLock.RLock() - defer EmailUrlLock.RUnlock() - if len(EmailUrl) == 0 { - return DefaultEmailUrl - } - return EmailUrl -} - -func (tm *STemplateManager) SetEmailUrl(url string) { - EmailUrlLock.Lock() - defer EmailUrlLock.Unlock() - EmailUrl = url + return options.Options.VerifyEmailUrl } func (tm *STemplateManager) InitializeData() error { diff --git a/pkg/notify/models/worker.go b/pkg/notify/models/worker.go index 42403cd1fb..5c981f3391 100644 --- a/pkg/notify/models/worker.go +++ b/pkg/notify/models/worker.go @@ -22,12 +22,11 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/cloudcommon/db" - "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/mcclient" - "yunion.io/x/onecloud/pkg/mcclient/modules/notify" "yunion.io/x/onecloud/pkg/notify/utils" ) @@ -69,25 +68,19 @@ func RestartService(config map[string]string, serviceName string) { }, nil, nil) } -func SendVerifyMessage(userCred mcclient.TokenCredential, verify *SVerify, uid, contactType, contact string) { - workMan.Run(func() { - sendVerifyMessage(context.Background(), userCred, verify, uid, contactType, contact) - }, nil, nil) -} - -func sendVerifyMessage(ctx context.Context, userCred mcclient.TokenCredential, verify *SVerify, uid, contactType, - contact string) { +func SendVerifyMessage(ctx context.Context, userCred mcclient.TokenCredential, verify *SVerify, + contact *SContact) error { var ( err error msg string ) processId, token := verify.ID, verify.Token - if contactType == "email" { + if contact.ContactType == "email" { emailUrl := strings.Replace(TemplateManager.GetEmailUrl(), "{0}", processId, 1) emailUrl = strings.Replace(emailUrl, "{1}", token, 1) // get uName - uName, err := utils.GetUsernameByID(ctx, uid) + uName, err := utils.GetUsernameByID(ctx, contact.UID) if err != nil || len(uName) == 0 { uName = "用户" } @@ -96,23 +89,23 @@ func sendVerifyMessage(ctx context.Context, userCred mcclient.TokenCredential, v Link string }{uName, emailUrl} msg = jsonutils.Marshal(data).String() - } else if contactType == "mobile" { + } else if contact.ContactType == "mobile" { msg = fmt.Sprintf(`{"code": "%s"}`, token) } else { // todo - return + return nil } - err = NotifyService.Send(ctx, contactType, contact, "verify", msg, "") + err = NotifyService.Send(ctx, contact.ContactType, contact.Contact, "verify", msg, "") if err != nil { verify.SetStatus(userCred, VERIFICATION_SENT_FAIL, "") - // notify the uid through the webconsole - notifyclient.RawNotify([]string{uid}, false, notify.NotifyByWebConsole, notify.NotifyPriorityCritical, - "Send Verify Message Failed", jsonutils.NewString(err.Error())) + // set contact's status as "init" + contact.SetStatus(userCred, CONTACT_INIT, "send verify message failed") log.Errorf("Send verify message failed: %s.", err.Error()) - return + return errors.Wrap(err, "Send Verify Message Failed") } verify.SetStatus(userCred, VERIFICATION_SENT, "") + return nil } func UpdateDingtalk(uid string) { diff --git a/pkg/notify/options/options.go b/pkg/notify/options/options.go index 500e645121..76cc57f8d6 100644 --- a/pkg/notify/options/options.go +++ b/pkg/notify/options/options.go @@ -27,7 +27,7 @@ type NotifyOption struct { UpdateInterval int `help:"Update send services interval(unit:s)" default:"30"` VerifyEmailUrl string `help:"url of verify email"` ReSendScope int `help:"Resend all messages that have not been sent successfully within ReSendScope -seconds"` +seconds" default:"30"` InitNotificationScope int `help:"initialize data of notification with in InitNotificationScope hours" default:"100"` } diff --git a/pkg/notify/rpc/send.go b/pkg/notify/rpc/send.go index 739e2baf1e..17a42d892d 100644 --- a/pkg/notify/rpc/send.go +++ b/pkg/notify/rpc/send.go @@ -124,7 +124,7 @@ func (self *SRpcService) Send(ctx context.Context, contactType, contact, topic, _, err = self.execute(ctx, f, contactType) if err != nil { - return errors.Wrapf(err, "contactType: %s", contactType) + return errors.Wrapf(err, "contactType '%s'", contactType) } return nil } diff --git a/pkg/notify/service.go b/pkg/notify/service.go index 7b40a57e01..870b4b7601 100644 --- a/pkg/notify/service.go +++ b/pkg/notify/service.go @@ -43,9 +43,6 @@ func StartService() { baseOpts := &options.Options.BaseOptions common_options.ParseOptions(opts, os.Args, "notify.conf", "notify") - // init email url - models.TemplateManager.SetEmailUrl(options.Options.VerifyEmailUrl) - // init auth app.InitAuth(commonOpts, func() { log.Infof("Auth complete!")