Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 1 | package monitors |
| 2 | |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 8 | |
Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 9 | // Type represents the type of Monitor. |
| 10 | type Type string |
| 11 | |
| 12 | // Useful constants. |
| 13 | const ( |
| 14 | CONNECT Type = "CONNECT" |
| 15 | HTTP Type = "HTTP" |
| 16 | HTTPS Type = "HTTPS" |
| 17 | ) |
| 18 | |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 19 | // Monitor represents a health monitor API resource. A monitor comes in three |
| 20 | // forms: CONNECT, HTTP or HTTPS. |
| 21 | // |
| 22 | // A CONNECT monitor establishes a basic connection to each node on its defined |
| 23 | // port to ensure that the service is listening properly. The connect monitor |
| 24 | // is the most basic type of health check and does no post-processing or |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 25 | // protocol-specific health checks. |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 26 | // |
| 27 | // HTTP and HTTPS health monitors are generally considered more intelligent and |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 28 | // powerful than CONNECT. It is capable of processing an HTTP or HTTPS response |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 29 | // to determine the condition of a node. It supports the same basic properties |
| 30 | // as CONNECT and includes additional attributes that are used to evaluate the |
| 31 | // HTTP response. |
| 32 | type Monitor struct { |
Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 33 | // Number of permissible monitor failures before removing a node from |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 34 | // rotation. |
Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 35 | AttemptLimit int `mapstructure:"attemptsBeforeDeactivation"` |
| 36 | |
| 37 | // The minimum number of seconds to wait before executing the health monitor. |
Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 38 | Delay int |
| 39 | |
| 40 | // Maximum number of seconds to wait for a connection to be established |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 41 | // before timing out. |
Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 42 | Timeout int |
| 43 | |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 44 | // Type of the health monitor. |
Jamie Hannaford | d44daa8 | 2014-11-04 12:30:01 +0100 | [diff] [blame] | 45 | Type Type |
| 46 | |
| 47 | // A regular expression that will be used to evaluate the contents of the |
| 48 | // body of the response. |
| 49 | BodyRegex string |
| 50 | |
| 51 | // The name of a host for which the health monitors will check. |
| 52 | HostHeader string |
| 53 | |
| 54 | // The HTTP path that will be used in the sample request. |
| 55 | Path string |
| 56 | |
| 57 | // A regular expression that will be used to evaluate the HTTP status code |
| 58 | // returned in the response. |
| 59 | StatusRegex string |
| 60 | } |
| 61 | |
Jamie Hannaford | 7afb7af | 2014-11-04 13:32:20 +0100 | [diff] [blame] | 62 | // UpdateResult represents the result of an Update operation. |
| 63 | type UpdateResult struct { |
| 64 | gophercloud.ErrResult |
| 65 | } |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 66 | |
| 67 | // GetResult represents the result of a Get operation. |
| 68 | type GetResult struct { |
| 69 | gophercloud.Result |
| 70 | } |
| 71 | |
Jamie Hannaford | d7301dd | 2014-11-04 14:05:39 +0100 | [diff] [blame] | 72 | // DeleteResult represents the result of an Delete operation. |
| 73 | type DeleteResult struct { |
| 74 | gophercloud.ErrResult |
| 75 | } |
| 76 | |
Jamie Hannaford | c9a4d89 | 2014-11-04 14:01:56 +0100 | [diff] [blame] | 77 | // Extract interprets any GetResult as a Monitor. |
| 78 | func (r GetResult) Extract() (*Monitor, error) { |
| 79 | if r.Err != nil { |
| 80 | return nil, r.Err |
| 81 | } |
| 82 | |
| 83 | var response struct { |
| 84 | M Monitor `mapstructure:"healthMonitor"` |
| 85 | } |
| 86 | |
| 87 | err := mapstructure.Decode(r.Body, &response) |
| 88 | |
| 89 | return &response.M, err |
| 90 | } |