mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix: specify notify user&groups domain as prefix
This commit is contained in:
@@ -48,7 +48,7 @@ func InitAuth(options *CommonOptions, authComplete auth.AuthCompletedCallback) {
|
||||
if len(users) == 0 && len(groups) == 0 {
|
||||
users = []string{"sysadmin"}
|
||||
}
|
||||
notifyclient.FetchNotifyAdminRecipients(context.Background(), options.Region, options.NotifyAdminDomain, users, groups)
|
||||
notifyclient.FetchNotifyAdminRecipients(context.Background(), options.Region, users, groups)
|
||||
|
||||
authComplete()
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appsrv"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/notify"
|
||||
@@ -196,22 +197,45 @@ func NotifySystemWarning(idstr string, name string, event string, reason string)
|
||||
SystemNotify(notify.NotifyPriorityImportant, SYSTEM_WARNING, jsonutils.Marshal(msg))
|
||||
}
|
||||
|
||||
func FetchNotifyAdminRecipients(ctx context.Context, region string, domain string, users []string, groups []string) {
|
||||
s := auth.GetAdminSession(ctx, region, "v1")
|
||||
func parseIdName(idName string) (string, string) {
|
||||
pos := strings.Index(idName, "\\")
|
||||
if pos > 0 {
|
||||
return idName[:pos], idName[pos+1:]
|
||||
} else {
|
||||
return "", idName
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
domainCache = make(map[string]string)
|
||||
)
|
||||
|
||||
func getIdentityId(s *mcclient.ClientSession, idName string, manager modules.Manager) (string, error) {
|
||||
domain, idName := parseIdName(idName)
|
||||
|
||||
query := jsonutils.NewDict()
|
||||
if len(domain) > 0 {
|
||||
domainId, err := modules.Domains.GetId(s, domain, nil)
|
||||
if err != nil {
|
||||
log.Errorf("fail to find domainId for domain %s", domain)
|
||||
return
|
||||
domainId, ok := domainCache[domain]
|
||||
if !ok {
|
||||
var err error
|
||||
domainId, err = modules.Domains.GetId(s, domain, nil)
|
||||
if err != nil {
|
||||
log.Errorf("fail to find domainId for domain %s: %s", domain, err)
|
||||
return "", err
|
||||
}
|
||||
domainCache[domain] = domainId
|
||||
}
|
||||
query.Add(jsonutils.NewString(domainId), "domain_id")
|
||||
}
|
||||
return manager.GetId(s, idName, query)
|
||||
}
|
||||
|
||||
func FetchNotifyAdminRecipients(ctx context.Context, region string, users []string, groups []string) {
|
||||
s := auth.GetAdminSession(ctx, region, "v1")
|
||||
|
||||
notifyAdminUsers = make([]string, 0)
|
||||
for _, u := range users {
|
||||
uId, err := modules.UsersV3.GetId(s, u, query)
|
||||
uId, err := getIdentityId(s, u, &modules.UsersV3)
|
||||
if err != nil {
|
||||
log.Warningf("fetch user %s fail: %s", u, err)
|
||||
} else {
|
||||
@@ -220,9 +244,9 @@ func FetchNotifyAdminRecipients(ctx context.Context, region string, domain strin
|
||||
}
|
||||
notifyAdminGroups = make([]string, 0)
|
||||
for _, g := range groups {
|
||||
gId, err := modules.Groups.GetId(s, g, query)
|
||||
gId, err := getIdentityId(s, g, &modules.Groups)
|
||||
if err != nil {
|
||||
log.Errorf("fetch group %s fail: %s", g, err)
|
||||
log.Warningf("fetch group %s fail: %s", g, err)
|
||||
} else {
|
||||
notifyAdminGroups = append(notifyAdminGroups, gId)
|
||||
}
|
||||
|
||||
@@ -36,9 +36,8 @@ type CommonOptions struct {
|
||||
ApplicationID string `help:"Application ID"`
|
||||
RequestWorkerCount int `default:"4" help:"Request worker thread count, default is 4"`
|
||||
|
||||
NotifyAdminDomain string `help:"domain of notify admin users or groups"`
|
||||
NotifyAdminUsers []string `default:"sysadmin" help:"System administrator user ID or name to notify system events"`
|
||||
NotifyAdminGroups []string `help:"System administrator group ID or name to notify system events"`
|
||||
NotifyAdminUsers []string `default:"sysadmin" help:"System administrator user ID or name to notify system events, if domain is not default, specify domain as prefix ending with double backslash, e.g. domain\\\\user"`
|
||||
NotifyAdminGroups []string `help:"System administrator group ID or name to notify system events, if domain is not default, specify domain as prefix ending with double backslash, e.g. domain\\\\group"`
|
||||
|
||||
EnableSsl bool `help:"Enable https"`
|
||||
SslCaCerts string `help:"ssl certificate ca root file, separating ca and cert file is not encouraged" alias:"ca-file"`
|
||||
|
||||
Reference in New Issue
Block a user