forked from IBM-Cloud/bluemix-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitors.go
117 lines (103 loc) · 3.3 KB
/
monitors.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
package cisv1
import (
"fmt"
"github.com/IBM-Cloud/bluemix-go/client"
)
type Monitor struct {
Id string `json:"id"`
Path string `json:"path"`
Description string `json:"description"`
ExpBody string `json:"expected_body"`
ExpCodes string `json:"expected_codes"`
// Headers omitted future enhancement
MonType string `json:"type"`
Method string `json:"method"`
Timeout int `json:"timeout"`
Retries int `json:"retries"`
Interval int `json:"interval"`
FollowRedirects bool `json:"follow_redirects"`
AllowInsecure bool `json:"allow_insecure"`
}
type MonitorResults struct {
MonitorList []Monitor `json:"result"`
ResultsInfo ResultsCount `json:"result_info"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
}
type MonitorResult struct {
Monitor Monitor `json:"result"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
Messages []string `json:"messages"`
}
type MonitorBody struct {
Description string `json:"description"`
ExpCodes string `json:"expected_codes"`
ExpBody string `json:"expected_body"`
Path string `json:"path"`
// Headers ommited TBC
MonType string `json:"type,omitempty"`
Method string `json:"method,omitempty"`
Timeout int `json:"timeout,omitempty"`
Retries int `json:"retries,omitempty"`
Interval int `json:"interval,omitempty"`
FollowRedirects bool `json:"follow_redirects,omitempty"`
AllowInsecure bool `json:"allow_insecure,omitempty"`
}
type MonitorDelete struct {
Result struct {
MonitorId string
} `json:"result"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
Messages []string `json:"messages"`
}
type Monitors interface {
ListMonitors(cisId string) ([]Monitor, error)
GetMonitor(cisId string, monitorId string) (*Monitor, error)
CreateMonitor(cisId string, monitorBody MonitorBody) (*Monitor, error)
DeleteMonitor(cisId string, monitorId string) error
}
type monitors struct {
client *client.Client
}
func newMonitorAPI(c *client.Client) Monitors {
return &monitors{
client: c,
}
}
func (r *monitors) ListMonitors(cisId string) ([]Monitor, error) {
monitorResults := MonitorResults{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/monitors/", cisId)
_, err := r.client.Get(rawURL, &monitorResults)
if err != nil {
return nil, err
}
return monitorResults.MonitorList, err
}
func (r *monitors) GetMonitor(cisId string, monitorId string) (*Monitor, error) {
monitorResult := MonitorResult{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/monitors/%s", cisId, monitorId)
_, err := r.client.Get(rawURL, &monitorResult, nil)
if err != nil {
return nil, err
}
return &monitorResult.Monitor, nil
}
func (r *monitors) DeleteMonitor(cisId string, monitorId string) error {
rawURL := fmt.Sprintf("/v1/%s/load_balancers/monitors/%s", cisId, monitorId)
_, err := r.client.Delete(rawURL)
if err != nil {
return err
}
return nil
}
func (r *monitors) CreateMonitor(cisId string, monitorBody MonitorBody) (*Monitor, error) {
monitorResult := MonitorResult{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/monitors/", cisId)
_, err := r.client.Post(rawURL, &monitorBody, &monitorResult)
if err != nil {
return nil, err
}
return &monitorResult.Monitor, nil
}