Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 1 | package monitors |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | var ( |
| 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 Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 13 | ) |
| 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. |
| 17 | type UpdateOptsBuilder interface { |
| 18 | ToMonitorUpdateMap() (map[string]interface{}, error) |
| 19 | } |
| 20 | |
| 21 | // UpdateConnectMonitorOpts represents the options needed to update a CONNECT |
| 22 | // monitor. |
| 23 | type 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 Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 37 | // ToMonitorUpdateMap produces a map for updating CONNECT monitors. |
| 38 | func (opts UpdateConnectMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) { |
| 39 | type m map[string]interface{} |
| 40 | |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 41 | if !gophercloud.IntWithinRange(opts.AttemptLimit, 1, 10) { |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 42 | return m{}, errAttemptLimit |
| 43 | } |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 44 | if !gophercloud.IntWithinRange(opts.Delay, 1, 3600) { |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 45 | return m{}, errDelay |
| 46 | } |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 47 | if !gophercloud.IntWithinRange(opts.Timeout, 1, 300) { |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 48 | 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. |
| 60 | type 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. |
| 92 | func (opts UpdateHTTPMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) { |
| 93 | type m map[string]interface{} |
| 94 | |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 95 | if !gophercloud.IntWithinRange(opts.AttemptLimit, 1, 10) { |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 96 | return m{}, errAttemptLimit |
| 97 | } |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 98 | if !gophercloud.IntWithinRange(opts.Delay, 1, 3600) { |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 99 | return m{}, errDelay |
| 100 | } |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 101 | if !gophercloud.IntWithinRange(opts.Timeout, 1, 300) { |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 102 | 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. |
| 135 | func 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 Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 144 | _, res.Err = c.Put(rootURL(c, id), reqBody, nil, nil) |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 145 | return res |
| 146 | } |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 147 | |
Jamie Hannaford | d7301dd | 2014-11-04 14:05:39 +0100 | [diff] [blame] | 148 | // Get is the operation responsible for showing details of a health monitor. |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 149 | func Get(c *gophercloud.ServiceClient, id int) GetResult { |
| 150 | var res GetResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 151 | _, res.Err = c.Get(rootURL(c, id), &res.Body, nil) |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 152 | return res |
| 153 | } |
Jamie Hannaford | d7301dd | 2014-11-04 14:05:39 +0100 | [diff] [blame] | 154 | |
| 155 | // Delete is the operation responsible for deleting a health monitor. |
| 156 | func Delete(c *gophercloud.ServiceClient, id int) DeleteResult { |
| 157 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 158 | _, res.Err = c.Delete(rootURL(c, id), nil) |
Jamie Hannaford | d7301dd | 2014-11-04 14:05:39 +0100 | [diff] [blame] | 159 | return res |
| 160 | } |