mongodb_remote_update_task.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 mongodb
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type MongoDBRemoteUpdateTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(MongoDBRemoteUpdateTask{})
  31. }
  32. func (self *MongoDBRemoteUpdateTask) taskFail(ctx context.Context, mongodb *models.SMongoDB, err error) {
  33. mongodb.SetStatus(ctx, self.UserCred, apis.STATUS_UPDATE_TAGS_FAILED, err.Error())
  34. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  35. }
  36. func (self *MongoDBRemoteUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  37. mongodb := obj.(*models.SMongoDB)
  38. replaceTags := jsonutils.QueryBoolean(self.Params, "replace_tags", false)
  39. iMongoDB, err := mongodb.GetIMongoDB(ctx)
  40. if err != nil {
  41. self.taskFail(ctx, mongodb, errors.Wrapf(err, "GetIMongoDB"))
  42. return
  43. }
  44. oldTags, err := iMongoDB.GetTags()
  45. if err != nil {
  46. if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
  47. self.OnRemoteUpdateComplete(ctx, mongodb, nil)
  48. return
  49. }
  50. self.taskFail(ctx, mongodb, errors.Wrapf(err, "GetTags"))
  51. return
  52. }
  53. tags, err := mongodb.GetAllUserMetadata()
  54. if err != nil {
  55. self.taskFail(ctx, mongodb, errors.Wrapf(err, "GetAllUserMetadata"))
  56. return
  57. }
  58. tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
  59. err = cloudprovider.SetTags(ctx, iMongoDB, mongodb.ManagerId, tags, replaceTags)
  60. if err != nil {
  61. if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
  62. self.OnRemoteUpdateComplete(ctx, mongodb, nil)
  63. return
  64. }
  65. logclient.AddActionLogWithStartable(self, mongodb, logclient.ACT_UPDATE_TAGS, err, self.GetUserCred(), false)
  66. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  67. return
  68. }
  69. logclient.AddActionLogWithStartable(self, mongodb, logclient.ACT_UPDATE_TAGS, tagsUpdateInfo, self.GetUserCred(), true)
  70. self.OnRemoteUpdateComplete(ctx, mongodb, nil)
  71. }
  72. func (self *MongoDBRemoteUpdateTask) OnRemoteUpdateComplete(ctx context.Context, mongodb *models.SMongoDB, data jsonutils.JSONObject) {
  73. self.SetStage("OnSyncStatusComplete", nil)
  74. models.StartResourceSyncStatusTask(ctx, self.UserCred, mongodb, "MongoDBSyncstatusTask", self.GetTaskId())
  75. }
  76. func (self *MongoDBRemoteUpdateTask) OnSyncStatusComplete(ctx context.Context, mongodb *models.SMongoDB, data jsonutils.JSONObject) {
  77. self.SetStageComplete(ctx, nil)
  78. }
  79. func (self *MongoDBRemoteUpdateTask) OnSyncStatusCompleteFailed(ctx context.Context, mongodb *models.SMongoDB, data jsonutils.JSONObject) {
  80. self.SetStageFailed(ctx, data)
  81. }