clouduser_create_task.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tasks
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  23. "yunion.io/x/onecloud/pkg/cloudid/models"
  24. npk "yunion.io/x/onecloud/pkg/mcclient/modules/notify"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type ClouduserCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(ClouduserCreateTask{})
  32. }
  33. func (self *ClouduserCreateTask) taskFailed(ctx context.Context, clouduser *models.SClouduser, err error) {
  34. clouduser.SetStatus(ctx, self.GetUserCred(), apis.STATUS_CREATE_FAILED, err.Error())
  35. logclient.AddActionLogWithStartable(self, clouduser, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  36. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  37. }
  38. func (self *ClouduserCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  39. user := obj.(*models.SClouduser)
  40. provider, err := user.GetCloudprovider()
  41. if err != nil {
  42. self.taskFailed(ctx, user, errors.Wrapf(err, "GetCloudprovider"))
  43. return
  44. }
  45. account, err := provider.GetCloudaccount()
  46. if err != nil {
  47. self.taskFailed(ctx, user, errors.Wrapf(err, "GetCloudaccount"))
  48. return
  49. }
  50. driver, err := provider.GetDriver()
  51. if err != nil {
  52. self.taskFailed(ctx, user, errors.Wrapf(err, "GetDriver"))
  53. return
  54. }
  55. err = driver.RequestCreateClouduser(ctx, self.GetUserCred(), provider, user)
  56. if err != nil {
  57. self.taskFailed(ctx, user, errors.Wrapf(err, "RequestCreateClouduser"))
  58. return
  59. }
  60. user.SetStatus(ctx, self.GetUserCred(), apis.STATUS_AVAILABLE, "")
  61. if jsonutils.QueryBoolean(self.GetParams(), "notify", false) && len(user.Email) > 0 {
  62. msg := struct {
  63. Account string
  64. Name string
  65. Password string
  66. IamLoginUrl string
  67. Id string
  68. }{
  69. Id: self.Id,
  70. IamLoginUrl: account.IamLoginUrl,
  71. Account: account.AccountId,
  72. }
  73. msg.Password, _ = user.GetPassword()
  74. notifyclient.NotifyWithContact(ctx, []string{user.Email}, npk.NotifyByEmail, npk.NotifyPriorityNormal, "CLOUD_USER_CREATED", jsonutils.Marshal(msg))
  75. }
  76. self.SetStageComplete(ctx, nil)
  77. }