image_sync_classmetadata_task.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 tasks
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/image"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/image/models"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type ImageSyncClassMetadataTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(ImageSyncClassMetadataTask{})
  33. }
  34. func (self *ImageSyncClassMetadataTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  35. img := obj.(*models.SImage)
  36. cm, err := img.GetAllClassMetadata()
  37. if err != nil {
  38. self.taskFailed(ctx, img, jsonutils.NewString(err.Error()))
  39. return
  40. }
  41. session := auth.GetAdminSession(ctx, "")
  42. _, err = compute.Cachedimages.PerformAction(session, img.Id, "set-class-metadata", jsonutils.Marshal(cm))
  43. if err == nil {
  44. self.taskSuccess(ctx, img)
  45. return
  46. }
  47. if errors.Cause(err) != httperrors.ErrResourceNotFound {
  48. self.taskFailed(ctx, img, jsonutils.NewString(err.Error()))
  49. return
  50. }
  51. params := jsonutils.NewDict()
  52. params.Set("image_id", jsonutils.NewString(img.Id))
  53. _, err = compute.Cachedimages.PerformClassAction(session, "cache-image", params)
  54. if err != nil {
  55. self.taskFailed(ctx, img, jsonutils.NewString(err.Error()))
  56. return
  57. }
  58. self.taskSuccess(ctx, img)
  59. }
  60. func (self *ImageSyncClassMetadataTask) taskFailed(ctx context.Context, image *models.SImage, reason jsonutils.JSONObject) {
  61. reasonStr, _ := reason.GetString()
  62. image.SetStatus(ctx, self.UserCred, api.IMAGE_STATUS_SYNC_CLASS_METADATA_FAILEd, reasonStr)
  63. logclient.AddActionLogWithStartable(self, image, logclient.ACT_SYNC_CLASS_METADATA, reason, self.UserCred, false)
  64. self.SetStageFailed(ctx, reason)
  65. }
  66. func (self *ImageSyncClassMetadataTask) taskSuccess(ctx context.Context, image *models.SImage) {
  67. logclient.AddActionLogWithStartable(self, image, logclient.ACT_SYNC_CLASS_METADATA, nil, self.UserCred, true)
  68. self.SetStageComplete(ctx, nil)
  69. }