yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/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 proxmox 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "strings" 21 "time" 22 23 "yunion.io/x/log" 24 "yunion.io/x/pkg/errors" 25 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 ) 28 29 type STask struct { 30 User string `json:"user"` 31 Type string `json:"type"` 32 Saved string `json:"saved"` 33 Status string `json:"status"` 34 Starttime int `json:"starttime"` 35 Node string `json:"node"` 36 ID string `json:"id"` 37 Endtime int `json:"endtime"` 38 Upid string `json:"upid"` 39 } 40 41 func (c *SProxmoxClient) getTaskId(taskResponse map[string]interface{}) string { 42 if taskResponse["errors"] != nil { 43 errJSON, _ := json.MarshalIndent(taskResponse["errors"], "", " ") 44 return string(errJSON) 45 } 46 if taskResponse["data"] == nil { 47 return "" 48 } 49 50 taskUpid := taskResponse["data"].(string) 51 return taskUpid 52 } 53 54 func (self *SProxmoxClient) waitTask(id string) (string, error) { 55 resId := "" 56 return resId, cloudprovider.Wait(time.Second*10, time.Minute*10, func() (bool, error) { 57 tasks := []STask{} 58 err := self.get("/cluster/tasks", nil, &tasks) 59 if err != nil { 60 return false, errors.Wrapf(err, "get task %s", id) 61 } 62 63 for _, task := range tasks { 64 if task.Upid == id { 65 switch strings.ToLower(task.Status) { 66 case "running": 67 log.Debugf("task %s state: %s", task.Upid, task.Status) 68 return false, nil 69 case "ok": 70 log.Debugf("task %s state: %s", task.Upid, task.Status) 71 resId = task.Upid 72 return true, nil 73 case "": 74 log.Debugf("task %s not finish", task.Upid) 75 return false, nil 76 default: 77 log.Debugf("task %s state: %s", task.Upid, task.Status) 78 return false, fmt.Errorf("task %s state: %s", task.Upid, task.Status) 79 } 80 81 } 82 } 83 84 return false, fmt.Errorf("not find task %s ", id) 85 }) 86 }