yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 incloudsphere
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type STask struct {
    28  	Id                string
    29  	Name              string
    30  	Detail            string
    31  	Error             string
    32  	State             string
    33  	CreatedResourceId string
    34  }
    35  
    36  func (self *SphereClient) waitTask(id string) (string, error) {
    37  	resId := ""
    38  	return resId, cloudprovider.Wait(time.Second*10, time.Minute*10, func() (bool, error) {
    39  		task := &STask{}
    40  		err := self.get("/tasks/"+id, nil, task)
    41  		if err != nil {
    42  			return false, errors.Wrapf(err, "get task %s", id)
    43  		}
    44  		switch task.State {
    45  		case "WAITING", "READY", "RUNNING":
    46  			log.Debugf("task %s with %s state: %s", task.Id, task.Detail, task.State)
    47  			return false, nil
    48  		case "FINISHED":
    49  			resId = task.CreatedResourceId
    50  			return true, nil
    51  		case "ERROR":
    52  			return false, fmt.Errorf("%s: %s", task.Detail, task.Error)
    53  		default:
    54  			log.Debugf("task %s with %s state: %s", task.Id, task.Detail, task.State)
    55  			return false, fmt.Errorf("%s: %s invalid state: %v", task.Detail, task.Error, task.State)
    56  		}
    57  	})
    58  }