dbinstance_sync_secgroups_task.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type DBInstanceSyncSecgroupsTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(DBInstanceSyncSecgroupsTask{})
  30. }
  31. func (self *DBInstanceSyncSecgroupsTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) {
  32. rds.SetStatus(ctx, self.UserCred, api.DBINSTANCE_SYNC_SECGROUP_FAILED, err.Error())
  33. db.OpsLog.LogEvent(rds, db.ACT_SYNC_CONF, err, self.GetUserCred())
  34. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_SYNC_CONF, err, self.UserCred, false)
  35. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  36. }
  37. func (self *DBInstanceSyncSecgroupsTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  38. rds := obj.(*models.SDBInstance)
  39. driver, err := rds.GetRegionDriver()
  40. if err != nil {
  41. self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegionDriver"))
  42. return
  43. }
  44. self.SetStage("OnSyncSecurityGroupsComplete", nil)
  45. err = driver.RequestSyncRdsSecurityGroups(ctx, self.GetUserCred(), rds, self)
  46. if err != nil {
  47. self.taskFailed(ctx, rds, errors.Wrapf(err, "RequestSyncRdsSecurityGroups"))
  48. return
  49. }
  50. }
  51. func (self *DBInstanceSyncSecgroupsTask) OnSyncSecurityGroupsCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  52. self.SetStageFailed(ctx, data)
  53. }
  54. func (self *DBInstanceSyncSecgroupsTask) OnSyncSecurityGroupsComplete(ctx context.Context, rds *models.SDBInstance, data jsonutils.JSONObject) {
  55. self.SetStage("OnSyncComplete", nil)
  56. rds.StartDBInstanceSyncTask(ctx, self.GetUserCred(), self.GetTaskId())
  57. }
  58. func (self *DBInstanceSyncSecgroupsTask) OnSyncCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  59. self.SetStageFailed(ctx, data)
  60. }
  61. func (self *DBInstanceSyncSecgroupsTask) OnSyncComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  62. self.SetStageComplete(ctx, nil)
  63. }