operation.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 google
  15. import (
  16. "time"
  17. "yunion.io/x/log"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. )
  20. const (
  21. OPERATION_STATUS_RUNNING = "RUNNING"
  22. OPERATION_STATUS_DONE = "DONE"
  23. )
  24. type SOperation struct {
  25. Id string
  26. Name string
  27. OperationType string
  28. TargetLink string
  29. TargetId string
  30. Status string
  31. User string
  32. Progress int
  33. InsertTime time.Time
  34. StartTime time.Time
  35. EndTime time.Time
  36. SelfLink string
  37. Region string
  38. Kind string
  39. }
  40. func (self *SGoogleClient) GetOperation(id string) (*SOperation, error) {
  41. operation := &SOperation{}
  42. err := self.GetBySelfId(id, &operation)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return operation, nil
  47. }
  48. func (self *SGoogleClient) WaitOperation(id string, resource, action string) (string, error) {
  49. targetLink := ""
  50. err := cloudprovider.Wait(time.Second*5, time.Minute*5, func() (bool, error) {
  51. operation, err := self.GetOperation(id)
  52. if err != nil {
  53. return false, err
  54. }
  55. log.Debugf("%s %s operation status: %s expect %s", action, resource, operation.Status, OPERATION_STATUS_DONE)
  56. if operation.Status == OPERATION_STATUS_DONE {
  57. targetLink = operation.TargetLink
  58. return true, nil
  59. }
  60. return false, nil
  61. })
  62. return targetLink, err
  63. }
  64. func (region *SRegion) GetRdsOperation(id string) (*SOperation, error) {
  65. operation := &SOperation{}
  66. err := region.rdsGet(id, &operation)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return operation, nil
  71. }
  72. func (region *SRegion) WaitRdsOperation(id string, resource, action string) (string, error) {
  73. targetLink := ""
  74. err := cloudprovider.Wait(time.Second*5, time.Minute*20, func() (bool, error) {
  75. operation, err := region.GetRdsOperation(id)
  76. if err != nil {
  77. return false, err
  78. }
  79. log.Debugf("%s %s operation status: %s expect %s", action, resource, operation.Status, OPERATION_STATUS_DONE)
  80. if operation.Status == OPERATION_STATUS_DONE {
  81. targetLink = operation.TargetLink
  82. return true, nil
  83. }
  84. return false, nil
  85. })
  86. return targetLink, err
  87. }