-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_iploadbalancing_refresh.go
126 lines (105 loc) · 3.27 KB
/
resource_iploadbalancing_refresh.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ovh
import (
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceIPLoadbalancingRefresh() *schema.Resource {
return &schema.Resource{
Create: resourceIPLoadbalancingRefreshCreate,
Read: resourceIPLoadbalancingRefreshRead,
Delete: resourceIPLoadbalancingRefreshDelete,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"keepers": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceIPLoadbalancingRefreshCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
service := d.Get("service_name").(string)
// verify if there are no active tasks for the loadbalancer
// at the moment and wait till finished if there are any
stateConf := &resource.StateChangeConf{
Target: []string{"empty"},
Refresh: func() (interface{}, string, error) {
for _, state := range []string{"todo", "doing"} {
taskResp := &[]int{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/task?action=refreshIplb&status=%s", service, state)
err := config.OVHClient.Get(endpoint, taskResp)
if err != nil {
return d, "error", fmt.Errorf("calling GET %s :\n\t %s", endpoint, err.Error())
}
if len(*taskResp) > 0 {
return d, "exists", nil
}
}
return d, "empty", nil
},
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for IPLoadbalancer tasks to finish: %s", err)
}
// verify if there are any outstanding changes to refresh
checkResp := &IPLoadbalancingRefreshPendings{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/pendingChanges", service)
err = config.OVHClient.Get(endpoint, checkResp)
if err != nil {
return fmt.Errorf("calling GET %s :\n\t %s", endpoint, err.Error())
}
// no changes detected, return successfull creation/refresh
if len(*checkResp) == 0 {
d.SetId(service)
return nil
}
// proceed with refresh
resp := &IPLoadbalancingRefreshTask{}
endpoint = fmt.Sprintf("/ipLoadbalancing/%s/refresh", service)
err = config.OVHClient.Post(endpoint, nil, resp)
if err != nil {
return fmt.Errorf("calling POST %s :\n\t %s", endpoint, err.Error())
}
stateConf = &resource.StateChangeConf{
Target: []string{"done"},
Refresh: func() (interface{}, string, error) {
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/task/%d", service, resp.ID)
stateResp := &IPLoadbalancingRefreshTask{}
err := config.OVHClient.Get(endpoint, stateResp)
if err != nil {
return nil, "", err
}
return d, stateResp.Status, nil
},
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for IPLoadbalancer refresh: %s", err)
}
d.SetId(service)
return nil
}
func resourceIPLoadbalancingRefreshRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceIPLoadbalancingRefreshDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}