cloudprovider_sync_resources_task.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/appsrv"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudid/models"
  24. )
  25. type CloudproviderSyncResourcesTask struct {
  26. taskman.STask
  27. }
  28. var (
  29. CloudproviderSyncWorkerManager *appsrv.SWorkerManager
  30. )
  31. func init() {
  32. CloudproviderSyncWorkerManager = appsrv.NewWorkerManager("CloudproviderSyncWorkerManager", 30, 1024, false)
  33. taskman.RegisterTaskAndWorker(CloudproviderSyncResourcesTask{}, CloudproviderSyncWorkerManager)
  34. }
  35. func (self *CloudproviderSyncResourcesTask) taskFailed(ctx context.Context, cp *models.SCloudprovider, err error) {
  36. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  37. }
  38. func (self *CloudproviderSyncResourcesTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  39. cp := obj.(*models.SCloudprovider)
  40. provider, err := cp.GetProvider()
  41. if err != nil {
  42. self.taskFailed(ctx, cp, errors.Wrap(err, "GetProvider"))
  43. return
  44. }
  45. driver, err := cp.GetDriver()
  46. if err != nil {
  47. self.taskFailed(ctx, cp, errors.Wrap(err, "GetDriver"))
  48. return
  49. }
  50. err = driver.RequestSyncCloudproviderResources(ctx, self.GetUserCred(), cp, provider)
  51. if err != nil {
  52. if errors.Cause(err) != cloudprovider.ErrNotImplemented {
  53. self.taskFailed(ctx, cp, errors.Wrap(err, "RequestSyncCloudproviderResources"))
  54. return
  55. }
  56. }
  57. self.SetStageComplete(ctx, nil)
  58. }