cloud_region_sync_skus_task.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 cloudaccount
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/util/compare"
  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/mcclient"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type CloudRegionSyncSkusTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTaskAndWorker(CloudRegionSyncSkusTask{}, SkuSyncWorkerManager)
  31. }
  32. func (self *CloudRegionSyncSkusTask) taskFailed(ctx context.Context, region *models.SCloudregion, msg string) {
  33. db.OpsLog.LogEvent(region, db.ACT_SYNC_CLOUD_SKUS, msg, self.GetUserCred())
  34. logclient.AddActionLogWithStartable(self, region, logclient.ACT_CLOUD_SYNC, msg, self.UserCred, false)
  35. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  36. }
  37. func (self *CloudRegionSyncSkusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  38. region := obj.(*models.SCloudregion)
  39. res, _ := self.GetParams().GetString("resource")
  40. type SyncFunc func(ctx context.Context, userCred mcclient.TokenCredential, region *models.SCloudregion, xor bool) compare.SyncResult
  41. var syncFunc SyncFunc
  42. switch res {
  43. case models.ServerSkuManager.Keyword():
  44. syncFunc = models.SyncServerSkusByRegion
  45. case models.ElasticcacheSkuManager.Keyword():
  46. syncFunc = models.ElasticcacheSkuManager.SyncElasticcacheSkus
  47. case models.DBInstanceSkuManager.Keyword():
  48. syncFunc = models.DBInstanceSkuManager.SyncDBInstanceSkus
  49. case models.NatSkuManager.Keyword():
  50. result := region.SyncNatSkus(ctx, self.GetUserCred(), false)
  51. log.Infof("Sync %s %s skus for region %s result: %s", region.Provider, res, region.Name, result.Result())
  52. }
  53. if syncFunc != nil {
  54. result := syncFunc(ctx, self.GetUserCred(), region, false)
  55. log.Infof("Sync %s %s skus for region %s result: %s", region.Provider, res, region.Name, result.Result())
  56. if result.IsError() {
  57. self.taskFailed(ctx, region, result.Result())
  58. return
  59. }
  60. }
  61. self.SetStageComplete(ctx, nil)
  62. }