From fc0f05286f88223e296ef9ec25fdb17a34bdf137 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Tue, 1 Dec 2020 21:34:21 +0800 Subject: [PATCH] feat(cloudid): notify clouduser created --- .../cloudid/content@cn/CLOUD_USER_CREATED | 6 +++++ .../cloudid/content@en/CLOUD_USER_CREATED | 6 +++++ .../cloudid/title@cn/CLOUD_USER_CREATED | 1 + .../cloudid/title@en/CLOUD_USER_CREATED | 1 + pkg/apis/cloudid/clouduser.go | 4 +++ pkg/cloudcommon/notifyclient/notify.go | 3 ++- pkg/cloudid/models/cloudaccount.go | 25 +++++++++++++++++++ pkg/cloudid/models/clouduser.go | 22 ++++++++++++++++ pkg/mcclient/options/cloudid/clouduser.go | 1 + 9 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@cn/CLOUD_USER_CREATED create mode 100644 build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@en/CLOUD_USER_CREATED create mode 100644 build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@cn/CLOUD_USER_CREATED create mode 100644 build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@en/CLOUD_USER_CREATED diff --git a/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@cn/CLOUD_USER_CREATED b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@cn/CLOUD_USER_CREATED new file mode 100644 index 0000000000..918347e431 --- /dev/null +++ b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@cn/CLOUD_USER_CREATED @@ -0,0 +1,6 @@ +公有云子账号创建成功 +主账号:{{ .account }} +名称:{{ .name }} +密码:{{ .password }} +登录地址: {{ .iam_login_url }} +资源ID: {{ .id }} diff --git a/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@en/CLOUD_USER_CREATED b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@en/CLOUD_USER_CREATED new file mode 100644 index 0000000000..aa29083458 --- /dev/null +++ b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/content@en/CLOUD_USER_CREATED @@ -0,0 +1,6 @@ +Clouduser has been created successfully +Root Account: {{ .account }} +Name:{{ .name }} +Password:{{ .password }} +IAM Login URL: {{ .iam_login_url }} +Resource ID: {{ .id }} diff --git a/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@cn/CLOUD_USER_CREATED b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@cn/CLOUD_USER_CREATED new file mode 100644 index 0000000000..dc71f2dd9e --- /dev/null +++ b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@cn/CLOUD_USER_CREATED @@ -0,0 +1 @@ +公有云子账号 {{ .name }} 创建成功 diff --git a/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@en/CLOUD_USER_CREATED b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@en/CLOUD_USER_CREATED new file mode 100644 index 0000000000..4e8e0d259e --- /dev/null +++ b/build/cloudid/root/opt/yunion/share/notify_templates/cloudid/title@en/CLOUD_USER_CREATED @@ -0,0 +1 @@ +The clouduser {{ .name }} created successfully diff --git a/pkg/apis/cloudid/clouduser.go b/pkg/apis/cloudid/clouduser.go index 1c8ed32fae..34360ac78a 100644 --- a/pkg/apis/cloudid/clouduser.go +++ b/pkg/apis/cloudid/clouduser.go @@ -82,6 +82,10 @@ type ClouduserCreateInput struct { // swagger:ignore ExternalId string `json:"external_id"` + + // 是否发送邮件通知(仅设置email不为空生效) + // default: false + Notify bool `json:"notify"` } type ClouduserListInput struct { diff --git a/pkg/cloudcommon/notifyclient/notify.go b/pkg/cloudcommon/notifyclient/notify.go index d2cefde2ed..a87beb7d72 100644 --- a/pkg/cloudcommon/notifyclient/notify.go +++ b/pkg/cloudcommon/notifyclient/notify.go @@ -149,10 +149,11 @@ func Notify(recipientId []string, isGroup bool, priority npk.TNotifyPriority, ev notify(context.Background(), recipientId, isGroup, priority, event, data) } -func NotifyWithContact(ctx context.Context, contacts []string, priority npk.TNotifyPriority, event string, data jsonutils.JSONObject) { +func NotifyWithContact(ctx context.Context, contacts []string, channel npk.TNotifyChannel, priority npk.TNotifyPriority, event string, data jsonutils.JSONObject) { p := sNotifyParams{ contacts: contacts, priority: priority, + channel: channel, event: event, data: data, } diff --git a/pkg/cloudid/models/cloudaccount.go b/pkg/cloudid/models/cloudaccount.go index 6c564fe80f..9afbcd1c18 100644 --- a/pkg/cloudid/models/cloudaccount.go +++ b/pkg/cloudid/models/cloudaccount.go @@ -34,6 +34,7 @@ import ( proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy" api "yunion.io/x/onecloud/pkg/apis/cloudid" + computeapi "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" @@ -110,6 +111,30 @@ func (manager *SCloudaccountManager) GetICloudaccounts() ([]SCloudaccount, error return accounts, nil } +func (self *SCloudaccount) GetClouduserAccountName(name string) (string, string) { + account := "" + switch self.Provider { + case computeapi.CLOUD_PROVIDER_ALIYUN: + suffix := strings.TrimPrefix(self.IamLoginUrl, "https://signin.aliyun.com/") + suffix = strings.TrimSuffix(suffix, "/login.htm") + if len(suffix) > 0 { + name = fmt.Sprintf("%s@%s", name, suffix) + account = suffix + } + case computeapi.CLOUD_PROVIDER_QCLOUD, computeapi.CLOUD_PROVIDER_HUAWEI: + u, _ := url.Parse(self.IamLoginUrl) + if u != nil { + account = u.Query().Get("account") + } + case computeapi.CLOUD_PROVIDER_AWS: + account := strings.TrimPrefix(self.IamLoginUrl, "https://") + if info := strings.Split(account, "."); len(info) > 0 { + account = info[0] + } + } + return account, name +} + func (manager *SCloudaccountManager) GetCloudaccounts() ([]SCloudaccount, error) { accounts := []SCloudaccount{} q := manager.Query() diff --git a/pkg/cloudid/models/clouduser.go b/pkg/cloudid/models/clouduser.go index d744f2bb1f..49f38b07b7 100644 --- a/pkg/cloudid/models/clouduser.go +++ b/pkg/cloudid/models/clouduser.go @@ -26,6 +26,7 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/pkg/tristate" "yunion.io/x/pkg/util/compare" + "yunion.io/x/pkg/util/regutils" "yunion.io/x/pkg/utils" "yunion.io/x/sqlchemy" @@ -33,9 +34,11 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + npk "yunion.io/x/onecloud/pkg/mcclient/modules/notify" "yunion.io/x/onecloud/pkg/util/logclient" "yunion.io/x/onecloud/pkg/util/seclib2" "yunion.io/x/onecloud/pkg/util/stringutils2" @@ -363,6 +366,10 @@ func (manager *SClouduserManager) ValidateCreateData(ctx context.Context, userCr return input, err } + if len(input.Email) > 0 && !regutils.MatchEmail(input.Email) { + return input, httperrors.NewInputParameterError("invalid email address") + } + if len(input.OwnerId) > 0 { user, err := db.UserCacheManager.FetchUserById(ctx, input.OwnerId) if err != nil { @@ -523,6 +530,21 @@ func (self *SClouduser) PostCreate(ctx context.Context, userCred mcclient.TokenC for _, groupId := range input.CloudgroupIds { self.joinGroup(groupId) } + if len(self.Email) > 0 && input.Notify { + msg := struct { + Account string + Name string + Password string + IamLoginUrl string + Id string + }{ + Id: self.Id, + Password: input.Password, + IamLoginUrl: account.IamLoginUrl, + } + msg.Account, msg.Name = account.GetClouduserAccountName(self.Name) + notifyclient.NotifyWithContact(ctx, []string{self.Email}, npk.NotifyByEmail, npk.NotifyPriorityNormal, "CLOUD_USER_CREATED", jsonutils.Marshal(msg)) + } self.StartClouduserSyncTask(ctx, userCred, "") } diff --git a/pkg/mcclient/options/cloudid/clouduser.go b/pkg/mcclient/options/cloudid/clouduser.go index b9bd2da7ec..ed47d3d3a9 100644 --- a/pkg/mcclient/options/cloudid/clouduser.go +++ b/pkg/mcclient/options/cloudid/clouduser.go @@ -43,6 +43,7 @@ type ClouduserCreateOptions struct { MobilePhone string `help:"phone number"` IsConsoleLogin *bool `help:"is console login"` Password string `help:"clouduser password"` + Notify *bool `help:"Notify user which set email when clouduser created"` } func (opts *ClouduserCreateOptions) Params() (jsonutils.JSONObject, error) {