dbinstance_change_config.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 DBInstanceChangeConfigTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DBInstanceChangeConfigTask{})
  32. }
  33. func (self *DBInstanceChangeConfigTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) {
  34. rds.SetStatus(ctx, self.UserCred, api.DBINSTANCE_CHANGE_CONFIG_FAILED, err.Error())
  35. db.OpsLog.LogEvent(rds, db.ACT_CHANGE_CONFIG, err, self.GetUserCred())
  36. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CHANGE_CONFIG, err, self.UserCred, false)
  37. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  38. Obj: rds,
  39. Action: notifyclient.ActionChangeConfig,
  40. IsFail: true,
  41. })
  42. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  43. }
  44. func (self *DBInstanceChangeConfigTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  45. rds := obj.(*models.SDBInstance)
  46. self.SetStage("OnDBInstanceChangeConfigComplete", nil)
  47. input := &api.SDBInstanceChangeConfigInput{}
  48. err := self.GetParams().Unmarshal(input)
  49. if err != nil {
  50. self.taskFailed(ctx, rds, errors.Wrapf(err, "GetParams().Unmarshal"))
  51. return
  52. }
  53. region, err := rds.GetRegion()
  54. if err != nil {
  55. self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegion"))
  56. return
  57. }
  58. err = region.GetDriver().RequestChangeDBInstanceConfig(ctx, self.UserCred, rds, input, self)
  59. if err != nil {
  60. self.taskFailed(ctx, rds, err)
  61. return
  62. }
  63. }
  64. func (self *DBInstanceChangeConfigTask) OnDBInstanceChangeConfigComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  65. rds := obj.(*models.SDBInstance)
  66. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CHANGE_CONFIG, nil, self.UserCred, true)
  67. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  68. Obj: rds,
  69. Action: notifyclient.ActionChangeConfig,
  70. })
  71. self.SetStage("OnSyncDBInstanceStatusComplete", nil)
  72. rds.StartDBInstanceSyncTask(ctx, self.UserCred, self.GetTaskId())
  73. }
  74. func (self *DBInstanceChangeConfigTask) OnDBInstanceChangeConfigCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  75. instance := obj.(*models.SDBInstance)
  76. self.taskFailed(ctx, instance, fmt.Errorf("%s", data.String()))
  77. }
  78. func (self *DBInstanceChangeConfigTask) OnSyncDBInstanceStatusComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  79. self.SetStageComplete(ctx, nil)
  80. }
  81. func (self *DBInstanceChangeConfigTask) OnSyncDBInstanceStatusCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  82. self.SetStageFailed(ctx, data)
  83. }