task.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 huawei
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. func (self *SRegion) waitTaskStatus(serviceType string, taskId string, targetStatus string, interval time.Duration, timeout time.Duration) error {
  21. start := time.Now()
  22. for time.Now().Sub(start) < timeout {
  23. status, err := self.GetTaskStatus(serviceType, taskId)
  24. if err != nil {
  25. return err
  26. }
  27. if status == targetStatus {
  28. break
  29. } else if status == TASK_FAIL {
  30. return fmt.Errorf("task %s failed", taskId)
  31. } else {
  32. time.Sleep(interval)
  33. }
  34. }
  35. return nil
  36. }
  37. type SJob struct {
  38. Status string
  39. Entities struct {
  40. SubJobs []struct {
  41. VolumeId string
  42. ServerId string
  43. }
  44. VolumeId string
  45. ImageId string
  46. }
  47. }
  48. func (self *SRegion) GetJob(serviceType string, jobId string) (*SJob, error) {
  49. resp, err := self.list(serviceType, "jobs/"+jobId, nil)
  50. if err != nil {
  51. return nil, err
  52. }
  53. ret := &SJob{}
  54. err = resp.Unmarshal(ret)
  55. if err != nil {
  56. return nil, errors.Wrapf(err, "Unmarshal")
  57. }
  58. return ret, nil
  59. }
  60. func (self *SRegion) GetTaskStatus(serviceType string, taskId string) (string, error) {
  61. job, err := self.GetJob(serviceType, taskId)
  62. if err != nil {
  63. return "", err
  64. }
  65. return job.Status, nil
  66. }
  67. // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0022225398.html
  68. // 数据结构 entities -> []job
  69. func (self *SRegion) GetAllSubTaskEntityIDs(serviceType string, taskId string) ([]string, error) {
  70. err := self.waitTaskStatus(serviceType, taskId, TASK_SUCCESS, 10*time.Second, 600*time.Second)
  71. if err != nil {
  72. return nil, err
  73. }
  74. job, err := self.GetJob(serviceType, taskId)
  75. if err != nil {
  76. return nil, err
  77. }
  78. ret := []string{}
  79. for _, entity := range job.Entities.SubJobs {
  80. if len(entity.VolumeId) > 0 {
  81. ret = append(ret, entity.VolumeId)
  82. }
  83. if len(entity.ServerId) > 0 {
  84. ret = append(ret, entity.ServerId)
  85. }
  86. }
  87. return ret, nil
  88. }
  89. // 数据结构 entities -> job
  90. func (self *SRegion) GetTaskEntityID(serviceType string, taskId string, key string) (string, error) {
  91. err := self.waitTaskStatus(serviceType, taskId, TASK_SUCCESS, 10*time.Second, 600*time.Second)
  92. if err != nil {
  93. return "", err
  94. }
  95. job, err := self.GetJob(serviceType, taskId)
  96. if err != nil {
  97. return "", err
  98. }
  99. switch key {
  100. case "volume_id":
  101. return job.Entities.VolumeId, nil
  102. case "image_id":
  103. return job.Entities.ImageId, nil
  104. default:
  105. return "", fmt.Errorf("unknown %s", key)
  106. }
  107. }