-
Notifications
You must be signed in to change notification settings - Fork 132
/
vrack_task.go
55 lines (45 loc) · 1.32 KB
/
vrack_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package ovh
import (
"fmt"
"log"
"net/url"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/ovh/go-ovh/ovh"
)
func waitForVrackTask(task *VrackTask, c *ovh.Client) error {
vrackId := task.ServiceName
taskId := task.Id
refreshFunc := func() (interface{}, string, error) {
task := &VrackTask{}
endpoint := fmt.Sprintf(
"/vrack/%s/task/%d",
url.PathEscape(vrackId),
taskId,
)
err := c.Get(endpoint, task)
if err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
log.Printf("[DEBUG] Task id %d on Vrack %s completed", taskId, vrackId)
return taskId, "completed", nil
} else {
return taskId, "", err
}
}
log.Printf("[DEBUG] Pending Task id %d on Vrack %s status: %s", task.Id, vrackId, task.Status)
return taskId, task.Status, nil
}
log.Printf("[DEBUG] Waiting for Vrack Task id %d: Vrack %s ", taskId, vrackId)
stateConf := &resource.StateChangeConf{
Pending: []string{"init", "todo", "doing"},
Target: []string{"completed"},
Refresh: refreshFunc,
Timeout: 60 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for vrack task %s/%d to complete: %s", vrackId, taskId, err)
}
return nil
}