-
Notifications
You must be signed in to change notification settings - Fork 1
/
elb_utils.go
60 lines (48 loc) · 1.78 KB
/
elb_utils.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
56
57
58
59
60
package flexibleengine
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/elbaas"
)
func waitForELBJobSuccess(networkingClient *golangsdk.ServiceClient, j *elbaas.Job, timeout time.Duration) (*elbaas.JobInfo, error) {
jobId := j.JobId
target := "SUCCESS"
pending := []string{"INIT", "RUNNING"}
log.Printf("[DEBUG] Waiting for elbaas job %s to become %s.", jobId, target)
ji, err := waitForELBResource(networkingClient, "job", j.JobId, target, pending, timeout, getELBJobInfo)
if err == nil {
return ji.(*elbaas.JobInfo), nil
}
return nil, err
}
func getELBJobInfo(networkingClient *golangsdk.ServiceClient, uri string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
info, err := elbaas.QueryJobInfo(networkingClient, uri).Extract()
if err != nil {
return nil, "", err
}
return info, info.Status, nil
}
}
type getELBResource func(networkingClient *golangsdk.ServiceClient, id string) resource.StateRefreshFunc
func waitForELBResource(networkingClient *golangsdk.ServiceClient, name string, id string, target string, pending []string, timeout time.Duration, f getELBResource) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Target: []string{target},
Pending: pending,
Refresh: f(networkingClient, id),
Timeout: timeout,
Delay: 5 * time.Second,
MinTimeout: 1 * time.Second,
}
o, err := stateConf.WaitForState()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return nil, fmt.Errorf("Error: elbaas %s %s not found: %s", name, id, err)
}
return nil, fmt.Errorf("Error waiting for elbaas %s %s to become %s: %s", name, id, target, err)
}
return o, nil
}