cdn_domain_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 cdn
  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 CDNDomainRemoteUpdateTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(CDNDomainRemoteUpdateTask{})
  31. }
  32. func (self *CDNDomainRemoteUpdateTask) taskFail(ctx context.Context, cdn *models.SCDNDomain, err error) {
  33. cdn.SetStatus(ctx, self.UserCred, apis.STATUS_UPDATE_TAGS_FAILED, err.Error())
  34. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  35. }
  36. func (self *CDNDomainRemoteUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  37. cdn := obj.(*models.SCDNDomain)
  38. replaceTags := jsonutils.QueryBoolean(self.Params, "replace_tags", false)
  39. iCDNDomain, err := cdn.GetICloudCDNDomain(ctx)
  40. if err != nil {
  41. self.taskFail(ctx, cdn, errors.Wrapf(err, "GetICloudCDNDomain"))
  42. return
  43. }
  44. oldTags, err := iCDNDomain.GetTags()
  45. if err != nil {
  46. if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
  47. self.OnRemoteUpdateComplete(ctx, cdn, nil)
  48. return
  49. }
  50. self.taskFail(ctx, cdn, errors.Wrapf(err, "GetTags"))
  51. return
  52. }
  53. tags, err := cdn.GetAllUserMetadata()
  54. if err != nil {
  55. self.taskFail(ctx, cdn, errors.Wrapf(err, "GetAllUserMetadata"))
  56. return
  57. }
  58. tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
  59. err = cloudprovider.SetTags(ctx, iCDNDomain, cdn.ManagerId, tags, replaceTags)
  60. if err != nil {
  61. if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
  62. self.OnRemoteUpdateComplete(ctx, cdn, nil)
  63. return
  64. }
  65. logclient.AddActionLogWithStartable(self, cdn, logclient.ACT_UPDATE_TAGS, err, self.GetUserCred(), false)
  66. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  67. return
  68. }
  69. logclient.AddActionLogWithStartable(self, cdn, logclient.ACT_UPDATE_TAGS, tagsUpdateInfo, self.GetUserCred(), true)
  70. self.OnRemoteUpdateComplete(ctx, cdn, nil)
  71. }
  72. func (self *CDNDomainRemoteUpdateTask) OnRemoteUpdateComplete(ctx context.Context, cdn *models.SCDNDomain, data jsonutils.JSONObject) {
  73. self.SetStage("OnSyncStatusComplete", nil)
  74. models.StartResourceSyncStatusTask(ctx, self.UserCred, cdn, "CDNDomainSyncstatusTask", self.GetTaskId())
  75. }
  76. func (self *CDNDomainRemoteUpdateTask) OnSyncStatusComplete(ctx context.Context, cdn *models.SCDNDomain, data jsonutils.JSONObject) {
  77. self.SetStageComplete(ctx, nil)
  78. }
  79. func (self *CDNDomainRemoteUpdateTask) OnSyncStatusCompleteFailed(ctx context.Context, cdn *models.SCDNDomain, data jsonutils.JSONObject) {
  80. self.SetStageFailed(ctx, data)
  81. }