yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/task.go (about)

     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  
    15  package hcso
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/log"
    22  )
    23  
    24  func (self *SRegion) waitTaskStatus(serviceType string, taskId string, targetStatus string, interval time.Duration, timeout time.Duration) error {
    25  	start := time.Now()
    26  	for time.Now().Sub(start) < timeout {
    27  		status, err := self.GetTaskStatus(serviceType, taskId)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		if status == targetStatus {
    32  			break
    33  		} else if status == TASK_FAIL {
    34  			return fmt.Errorf("task %s failed", taskId)
    35  		} else {
    36  			time.Sleep(interval)
    37  		}
    38  	}
    39  	return nil
    40  }
    41  
    42  func (self *SRegion) GetTaskStatus(serviceType string, taskId string) (string, error) {
    43  	querys := map[string]string{"service_type": serviceType}
    44  	task, err := self.ecsClient.Jobs.Get(taskId, querys)
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  
    49  	status, err := task.GetString("status")
    50  	if status == TASK_FAIL {
    51  		log.Debugf("task %s failed: %s", taskId, task.String())
    52  	}
    53  
    54  	return status, err
    55  }
    56  
    57  // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0022225398.html
    58  // 数据结构  entities -> []job
    59  func (self *SRegion) GetAllSubTaskEntityIDs(serviceType string, taskId string, entityKeyName string) ([]string, error) {
    60  	err := self.waitTaskStatus(serviceType, taskId, TASK_SUCCESS, 10*time.Second, 600*time.Second)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	querys := map[string]string{"service_type": serviceType}
    66  	ret, err := self.ecsClient.Jobs.Get(taskId, querys)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	entities, err := ret.GetArray("entities", "sub_jobs")
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	ids := make([]string, 0)
    77  	for i := range entities {
    78  		entity := entities[i]
    79  		rid, err := entity.GetString("entities", entityKeyName)
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  
    84  		ids = append(ids, rid)
    85  	}
    86  
    87  	return ids, nil
    88  }
    89  
    90  // 数据结构  entities -> job
    91  func (self *SRegion) GetTaskEntityID(serviceType string, taskId string, entityKeyName string) (string, error) {
    92  	err := self.waitTaskStatus(serviceType, taskId, TASK_SUCCESS, 10*time.Second, 600*time.Second)
    93  	if err != nil {
    94  		return "", err
    95  	}
    96  
    97  	querys := map[string]string{"service_type": serviceType}
    98  	ret, err := self.ecsClient.Jobs.Get(taskId, querys)
    99  	if err != nil {
   100  		return "", err
   101  	}
   102  
   103  	return ret.GetString("entities", entityKeyName)
   104  }