guest_base_task.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 guest
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. )
  22. type SGuestBaseTask struct {
  23. taskman.STask
  24. }
  25. func (self *SGuestBaseTask) getGuest() *models.SGuest {
  26. obj := self.GetObject()
  27. return obj.(*models.SGuest)
  28. }
  29. func (self *SGuestBaseTask) SetStageFailed(ctx context.Context, reason jsonutils.JSONObject) {
  30. self.finalReleasePendingUsage(ctx)
  31. self.STask.SetStageFailed(ctx, reason)
  32. }
  33. func (self *SGuestBaseTask) finalReleasePendingUsage(ctx context.Context) {
  34. pendingUsage := models.SQuota{}
  35. err := self.GetPendingUsage(&pendingUsage, 0)
  36. if err == nil && !pendingUsage.IsEmpty() {
  37. quotas.CancelPendingUsage(ctx, self.UserCred, &pendingUsage, &pendingUsage, false) // failure
  38. }
  39. pendingRegionUsage := models.SRegionQuota{}
  40. err = self.GetPendingUsage(&pendingRegionUsage, 1)
  41. if err == nil && !pendingRegionUsage.IsEmpty() {
  42. quotas.CancelPendingUsage(ctx, self.UserCred, &pendingRegionUsage, &pendingRegionUsage, false) // failure
  43. }
  44. }