dbinstance_create_task.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 dbinstance
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type DBInstanceCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DBInstanceCreateTask{})
  32. }
  33. func (self *DBInstanceCreateTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) {
  34. rds.SetStatus(ctx, self.UserCred, api.DBINSTANCE_CREATE_FAILED, err.Error())
  35. db.OpsLog.LogEvent(rds, db.ACT_CREATE, err, self.GetUserCred())
  36. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CREATE, err, self.UserCred, false)
  37. notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{
  38. Obj: rds,
  39. Action: notifyclient.ActionCreate,
  40. IsFail: true,
  41. })
  42. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  43. }
  44. func (self *DBInstanceCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  45. rds := obj.(*models.SDBInstance)
  46. self.CreateDBInstance(ctx, rds)
  47. }
  48. func (self *DBInstanceCreateTask) CreateDBInstance(ctx context.Context, rds *models.SDBInstance) {
  49. region, err := rds.GetRegion()
  50. if err != nil {
  51. self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegion"))
  52. return
  53. }
  54. self.SetStage("OnCreateDBInstanceComplete", nil)
  55. if len(rds.DBInstancebackupId) > 0 {
  56. err = region.GetDriver().RequestCreateDBInstanceFromBackup(ctx, self.UserCred, rds, self)
  57. } else {
  58. err = region.GetDriver().RequestCreateDBInstance(ctx, self.UserCred, rds, self)
  59. }
  60. if err != nil {
  61. self.taskFailed(ctx, rds, err)
  62. return
  63. }
  64. }
  65. func (self *DBInstanceCreateTask) OnCreateDBInstanceComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  66. rds := obj.(*models.SDBInstance)
  67. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CREATE, nil, self.UserCred, true)
  68. self.SetStage("OnSyncDBInstanceStatusComplete", nil)
  69. rds.StartDBInstanceSyncTask(ctx, self.UserCred, self.GetTaskId())
  70. }
  71. func (self *DBInstanceCreateTask) OnCreateDBInstanceCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  72. rds := obj.(*models.SDBInstance)
  73. self.taskFailed(ctx, rds, fmt.Errorf("%s", data.String()))
  74. }
  75. func (self *DBInstanceCreateTask) OnSyncDBInstanceStatusComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  76. rds := obj.(*models.SDBInstance)
  77. //notifyclient.NotifyWebhook(ctx, self.UserCred, rds, notifyclient.ActionCreate)
  78. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  79. Obj: rds,
  80. Action: notifyclient.ActionCreate,
  81. })
  82. self.SetStageComplete(ctx, nil)
  83. }
  84. func (self *DBInstanceCreateTask) OnSyncDBInstanceStatusCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  85. self.SetStageFailed(ctx, data)
  86. }