forked from bizflycloud/gobizfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadbalancer_healthmonitor.go
165 lines (149 loc) · 5.54 KB
/
loadbalancer_healthmonitor.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gobizfly
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
)
// HealthMonitor represent the load balancer health monitor information
type HealthMonitor struct {
Name string `json:"name"`
Type string `json:"type"`
Delay int `json:"delay"`
MaxRetries int `json:"max_retries"`
MaxRetriesDown int `json:"max_retries_down"`
TimeOut int `json:"timeout"`
HTTPMethod string `json:"http_method"`
UrlPath string `json:"url_path"`
ExpectedCodes string `json:"expected_codes"`
HTTPVersion float32 `json:"http_version"`
OperatingStatus string `json:"operating_status"`
ProvisioningStatus string `json:"provisioning_status"`
DomainName string `json:"domain_name"`
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
TenantID string `json:"tenant_id"`
Pool []resourceID `json:"pool"`
}
// HealthMonitorCreateRequest represent the request bodfor creating a health monitor
type HealthMonitorCreateRequest struct {
Name string `json:"name"`
Type string `json:"type"`
TimeOut int `json:"timeout,omitempty"`
PoolID string `json:"pool_id"`
Delay int `json:"delay,omitempty"`
MaxRetries int `json:"max_retries,omitempty"`
MaxRetriesDown int `json:"max_retries_down,omitempty"`
HTTPMethod string `json:"http_method,omitempty"`
HTTPVersion float32 `json:"http_version,omitempty"`
URLPath string `json:"url_path,omitempty"`
ExpectedCodes string `json:"expected_codes,omitempty"`
DomainName string `json:"domain_name,omitempty"`
}
// HealthMonitorUpdateRequest represent the request bodfor updating a health monitor
type HealthMonitorUpdateRequest struct {
Name string `json:"name"`
TimeOut *int `json:"timeout,omitempty"`
Delay *int `json:"delay,omitempty"`
MaxRetries *int `json:"max_retries,omitempty"`
MaxRetriesDown *int `json:"max_retries_down,omitempty"`
HTTPMethod *string `json:"http_method,omitempty"`
HTTPVersion *float32 `json:"http_version,omitempty"`
URLPath *string `json:"url_path,omitempty"`
ExpectedCodes *string `json:"expected_codes,omitempty"`
DomainName *string `json:"domain_name,omitempty"`
}
type healthmonitor struct {
client *Client
}
var _ HealthMonitorService = (*healthmonitor)(nil)
// HealthMonitorService is an interface to interact with Bizfly API Health Monitor endpoint.
type HealthMonitorService interface {
Get(ctx context.Context, healthMonitorID string) (*HealthMonitor, error)
Delete(ctx context.Context, healthMonitorID string) error
Create(ctx context.Context, poolID string, hmcr *HealthMonitorCreateRequest) (*HealthMonitor, error)
Update(Ctx context.Context, healthMonitorID string, hmur *HealthMonitorUpdateRequest) (*HealthMonitor, error)
}
func (h *healthmonitor) itemPath(hmID string) string {
return strings.Join([]string{healthMonitorPath, hmID}, "/")
}
// Get gets detail a health monitor
func (h *healthmonitor) Get(ctx context.Context, hmID string) (*HealthMonitor, error) {
req, err := h.client.NewRequest(ctx, http.MethodGet, loadBalancerServiceName, h.itemPath(hmID), nil)
if err != nil {
return nil, err
}
resp, err := h.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
hm := &HealthMonitor{}
if err := json.NewDecoder(resp.Body).Decode(hm); err != nil {
return nil, err
}
return hm, nil
}
// Create creates a health monitor for a pool
func (h *healthmonitor) Create(ctx context.Context, poolID string, hmcr *HealthMonitorCreateRequest) (*HealthMonitor, error) {
var data struct {
HealthMonitor *HealthMonitorCreateRequest `json:"healthmonitor"`
}
hmcr.PoolID = poolID
data.HealthMonitor = hmcr
req, err := h.client.NewRequest(ctx, http.MethodPost, loadBalancerServiceName, "/"+strings.Join([]string{"pool", poolID, "healthmonitor"}, "/"), &data)
if err != nil {
return nil, err
}
resp, err := h.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respData struct {
HealthMonitor *HealthMonitor `json:"healthmonitor"`
}
if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
return nil, err
}
return respData.HealthMonitor, nil
}
// Delete deletes a health monitor
func (h *healthmonitor) Delete(ctx context.Context, hmID string) error {
req, err := h.client.NewRequest(ctx, http.MethodDelete, loadBalancerServiceName, h.itemPath(hmID), nil)
if err != nil {
return err
}
resp, err := h.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}
// Update - updates a health monitor
func (h *healthmonitor) Update(ctx context.Context, hmID string, hmur *HealthMonitorUpdateRequest) (*HealthMonitor, error) {
var data struct {
HealthMonitor *HealthMonitorUpdateRequest `json:"healthmonitor"`
}
data.HealthMonitor = hmur
req, err := h.client.NewRequest(ctx, http.MethodPut, loadBalancerServiceName, h.itemPath(hmID), data)
if err != nil {
return nil, err
}
resp, err := h.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respData struct {
HealthMonitor *HealthMonitor `json:"healthmonitor"`
}
if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
return nil, err
}
return respData.HealthMonitor, nil
}