blob: d4ba27653ca11c06c5137dcd3417fbc23bc354a9 [file] [log] [blame]
Jamie Hannaford7afb7af2014-11-04 13:32:20 +01001package monitors
2
3import (
4 "errors"
5
Jamie Hannaford7afb7af2014-11-04 13:32:20 +01006 "github.com/rackspace/gophercloud"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +01007)
8
9var (
10 errAttemptLimit = errors.New("AttemptLimit field must be an int greater than 1 and less than 10")
11 errDelay = errors.New("Delay field must be an int greater than 1 and less than 10")
12 errTimeout = errors.New("Timeout field must be an int greater than 1 and less than 10")
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010013)
14
15// UpdateOptsBuilder is the interface options structs have to satisfy in order
16// to be used in the main Update operation in this package.
17type UpdateOptsBuilder interface {
18 ToMonitorUpdateMap() (map[string]interface{}, error)
19}
20
21// UpdateConnectMonitorOpts represents the options needed to update a CONNECT
22// monitor.
23type UpdateConnectMonitorOpts struct {
24 // Required - number of permissible monitor failures before removing a node
25 // from rotation. Must be a number between 1 and 10.
26 AttemptLimit int
27
28 // Required - the minimum number of seconds to wait before executing the
29 // health monitor. Must be a number between 1 and 3600.
30 Delay int
31
32 // Required - maximum number of seconds to wait for a connection to be
33 // established before timing out. Must be a number between 1 and 300.
34 Timeout int
35}
36
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010037// ToMonitorUpdateMap produces a map for updating CONNECT monitors.
38func (opts UpdateConnectMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
39 type m map[string]interface{}
40
Jamie Hannaford950561c2014-11-12 11:12:20 +010041 if !gophercloud.IntWithinRange(opts.AttemptLimit, 1, 10) {
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010042 return m{}, errAttemptLimit
43 }
Jamie Hannaford950561c2014-11-12 11:12:20 +010044 if !gophercloud.IntWithinRange(opts.Delay, 1, 3600) {
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010045 return m{}, errDelay
46 }
Jamie Hannaford950561c2014-11-12 11:12:20 +010047 if !gophercloud.IntWithinRange(opts.Timeout, 1, 300) {
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010048 return m{}, errTimeout
49 }
50
51 return m{"healthMonitor": m{
52 "attemptsBeforeDeactivation": opts.AttemptLimit,
53 "delay": opts.Delay,
54 "timeout": opts.Timeout,
55 "type": CONNECT,
56 }}, nil
57}
58
59// UpdateHTTPMonitorOpts represents the options needed to update a HTTP monitor.
60type UpdateHTTPMonitorOpts struct {
61 // Required - number of permissible monitor failures before removing a node
62 // from rotation. Must be a number between 1 and 10.
63 AttemptLimit int `mapstructure:"attemptsBeforeDeactivation"`
64
65 // Required - the minimum number of seconds to wait before executing the
66 // health monitor. Must be a number between 1 and 3600.
67 Delay int
68
69 // Required - maximum number of seconds to wait for a connection to be
70 // established before timing out. Must be a number between 1 and 300.
71 Timeout int
72
73 // Required - a regular expression that will be used to evaluate the contents
74 // of the body of the response.
75 BodyRegex string
76
77 // Required - the HTTP path that will be used in the sample request.
78 Path string
79
80 // Required - a regular expression that will be used to evaluate the HTTP
81 // status code returned in the response.
82 StatusRegex string
83
84 // Optional - the name of a host for which the health monitors will check.
85 HostHeader string
86
87 // Required - either HTTP or HTTPS
88 Type Type
89}
90
91// ToMonitorUpdateMap produces a map for updating HTTP(S) monitors.
92func (opts UpdateHTTPMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
93 type m map[string]interface{}
94
Jamie Hannaford950561c2014-11-12 11:12:20 +010095 if !gophercloud.IntWithinRange(opts.AttemptLimit, 1, 10) {
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010096 return m{}, errAttemptLimit
97 }
Jamie Hannaford950561c2014-11-12 11:12:20 +010098 if !gophercloud.IntWithinRange(opts.Delay, 1, 3600) {
Jamie Hannaford7afb7af2014-11-04 13:32:20 +010099 return m{}, errDelay
100 }
Jamie Hannaford950561c2014-11-12 11:12:20 +0100101 if !gophercloud.IntWithinRange(opts.Timeout, 1, 300) {
Jamie Hannaford7afb7af2014-11-04 13:32:20 +0100102 return m{}, errTimeout
103 }
104 if opts.Type != HTTP && opts.Type != HTTPS {
105 return m{}, errors.New("Type must either by HTTP or HTTPS")
106 }
107 if opts.BodyRegex == "" {
108 return m{}, errors.New("BodyRegex is a required field")
109 }
110 if opts.Path == "" {
111 return m{}, errors.New("Path is a required field")
112 }
113 if opts.StatusRegex == "" {
114 return m{}, errors.New("StatusRegex is a required field")
115 }
116
117 json := m{
118 "attemptsBeforeDeactivation": opts.AttemptLimit,
119 "delay": opts.Delay,
120 "timeout": opts.Timeout,
121 "type": opts.Type,
122 "bodyRegex": opts.BodyRegex,
123 "path": opts.Path,
124 "statusRegex": opts.StatusRegex,
125 }
126
127 if opts.HostHeader != "" {
128 json["hostHeader"] = opts.HostHeader
129 }
130
131 return m{"healthMonitor": json}, nil
132}
133
134// Update is the operation responsible for updating a health monitor.
135func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
136 var res UpdateResult
137
138 reqBody, err := opts.ToMonitorUpdateMap()
139 if err != nil {
140 res.Err = err
141 return res
142 }
143
Jamie Hannaford5497f942015-03-25 11:55:51 +0100144 _, res.Err = c.Put(rootURL(c, id), reqBody, nil, nil)
Jamie Hannaford7afb7af2014-11-04 13:32:20 +0100145 return res
146}
Jamie Hannafordc9a4d892014-11-04 14:01:56 +0100147
Jamie Hannafordd7301dd2014-11-04 14:05:39 +0100148// Get is the operation responsible for showing details of a health monitor.
Jamie Hannafordc9a4d892014-11-04 14:01:56 +0100149func Get(c *gophercloud.ServiceClient, id int) GetResult {
150 var res GetResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100151 _, res.Err = c.Get(rootURL(c, id), &res.Body, nil)
Jamie Hannafordc9a4d892014-11-04 14:01:56 +0100152 return res
153}
Jamie Hannafordd7301dd2014-11-04 14:05:39 +0100154
155// Delete is the operation responsible for deleting a health monitor.
156func Delete(c *gophercloud.ServiceClient, id int) DeleteResult {
157 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100158 _, res.Err = c.Delete(rootURL(c, id), nil)
Jamie Hannafordd7301dd2014-11-04 14:05:39 +0100159 return res
160}