Update monitor :tada:
diff --git a/rackspace/lb/v1/monitors/fixtures.go b/rackspace/lb/v1/monitors/fixtures.go
index 71f3812..023a379 100644
--- a/rackspace/lb/v1/monitors/fixtures.go
+++ b/rackspace/lb/v1/monitors/fixtures.go
@@ -1,4 +1,4 @@
-package nodes
+package monitors
import (
"fmt"
@@ -14,8 +14,8 @@
return "/loadbalancers/" + strconv.Itoa(lbID) + "/healthmonitor"
}
-func mockGetResponse(t *testing.T, lbID, nodeID int) {
- th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
+func mockGetResponse(t *testing.T, lbID int) {
+ th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
@@ -35,8 +35,8 @@
})
}
-func mockUpdateResponse(t *testing.T, lbID, nodeID int) {
- th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
+func mockUpdateConnectResponse(t *testing.T, lbID int) {
+ th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
@@ -55,8 +55,31 @@
})
}
-func mockDeleteResponse(t *testing.T, lbID, nodeID int) {
- th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
+func mockUpdateHTTPResponse(t *testing.T, lbID int) {
+ th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ th.TestJSONRequest(t, r, `
+{
+ "healthMonitor": {
+ "attemptsBeforeDeactivation": 3,
+ "bodyRegex": "{regex}",
+ "delay": 10,
+ "path": "/foo",
+ "statusRegex": "200",
+ "timeout": 10,
+ "type": "HTTPS"
+ }
+}
+ `)
+
+ w.WriteHeader(http.StatusOK)
+ })
+}
+
+func mockDeleteResponse(t *testing.T, lbID int) {
+ th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusOK)
diff --git a/rackspace/lb/v1/monitors/requests.go b/rackspace/lb/v1/monitors/requests.go
new file mode 100644
index 0000000..dac0cab
--- /dev/null
+++ b/rackspace/lb/v1/monitors/requests.go
@@ -0,0 +1,157 @@
+package monitors
+
+import (
+ "errors"
+
+ "github.com/racker/perigee"
+
+ "github.com/rackspace/gophercloud"
+)
+
+// UpdateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Update operation in this package.
+type UpdateOptsBuilder interface {
+ ToMonitorUpdateMap() (map[string]interface{}, error)
+}
+
+// UpdateConnectMonitorOpts represents the options needed to update a CONNECT
+// monitor.
+type UpdateConnectMonitorOpts struct {
+ // Required - number of permissible monitor failures before removing a node
+ // from rotation. Must be a number between 1 and 10.
+ AttemptLimit int
+
+ // Required - the minimum number of seconds to wait before executing the
+ // health monitor. Must be a number between 1 and 3600.
+ Delay int
+
+ // Required - maximum number of seconds to wait for a connection to be
+ // established before timing out. Must be a number between 1 and 300.
+ Timeout int
+}
+
+var (
+ errAttemptLimit = errors.New("AttemptLimit field must be an int greater than 1 and less than 10")
+ errDelay = errors.New("Delay field must be an int greater than 1 and less than 10")
+ errTimeout = errors.New("Timeout field must be an int greater than 1 and less than 10")
+)
+
+func withinRange(val, min, max int) bool {
+ return val > min && val < max
+}
+
+// ToMonitorUpdateMap produces a map for updating CONNECT monitors.
+func (opts UpdateConnectMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
+ type m map[string]interface{}
+
+ if !withinRange(opts.AttemptLimit, 1, 10) {
+ return m{}, errAttemptLimit
+ }
+ if !withinRange(opts.Delay, 1, 3600) {
+ return m{}, errDelay
+ }
+ if !withinRange(opts.Timeout, 1, 300) {
+ return m{}, errTimeout
+ }
+
+ return m{"healthMonitor": m{
+ "attemptsBeforeDeactivation": opts.AttemptLimit,
+ "delay": opts.Delay,
+ "timeout": opts.Timeout,
+ "type": CONNECT,
+ }}, nil
+}
+
+// UpdateHTTPMonitorOpts represents the options needed to update a HTTP monitor.
+type UpdateHTTPMonitorOpts struct {
+ // Required - number of permissible monitor failures before removing a node
+ // from rotation. Must be a number between 1 and 10.
+ AttemptLimit int `mapstructure:"attemptsBeforeDeactivation"`
+
+ // Required - the minimum number of seconds to wait before executing the
+ // health monitor. Must be a number between 1 and 3600.
+ Delay int
+
+ // Required - maximum number of seconds to wait for a connection to be
+ // established before timing out. Must be a number between 1 and 300.
+ Timeout int
+
+ // Required - a regular expression that will be used to evaluate the contents
+ // of the body of the response.
+ BodyRegex string
+
+ // Required - the HTTP path that will be used in the sample request.
+ Path string
+
+ // Required - a regular expression that will be used to evaluate the HTTP
+ // status code returned in the response.
+ StatusRegex string
+
+ // Optional - the name of a host for which the health monitors will check.
+ HostHeader string
+
+ // Required - either HTTP or HTTPS
+ Type Type
+}
+
+// ToMonitorUpdateMap produces a map for updating HTTP(S) monitors.
+func (opts UpdateHTTPMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
+ type m map[string]interface{}
+
+ if !withinRange(opts.AttemptLimit, 1, 10) {
+ return m{}, errAttemptLimit
+ }
+ if !withinRange(opts.Delay, 1, 3600) {
+ return m{}, errDelay
+ }
+ if !withinRange(opts.Timeout, 1, 300) {
+ return m{}, errTimeout
+ }
+ if opts.Type != HTTP && opts.Type != HTTPS {
+ return m{}, errors.New("Type must either by HTTP or HTTPS")
+ }
+ if opts.BodyRegex == "" {
+ return m{}, errors.New("BodyRegex is a required field")
+ }
+ if opts.Path == "" {
+ return m{}, errors.New("Path is a required field")
+ }
+ if opts.StatusRegex == "" {
+ return m{}, errors.New("StatusRegex is a required field")
+ }
+
+ json := m{
+ "attemptsBeforeDeactivation": opts.AttemptLimit,
+ "delay": opts.Delay,
+ "timeout": opts.Timeout,
+ "type": opts.Type,
+ "bodyRegex": opts.BodyRegex,
+ "path": opts.Path,
+ "statusRegex": opts.StatusRegex,
+ }
+
+ if opts.HostHeader != "" {
+ json["hostHeader"] = opts.HostHeader
+ }
+
+ return m{"healthMonitor": json}, nil
+}
+
+// Update is the operation responsible for updating a health monitor.
+func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
+ var res UpdateResult
+
+ reqBody, err := opts.ToMonitorUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("PUT", rootURL(c, id), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{200},
+ })
+
+ return res
+}
diff --git a/rackspace/lb/v1/monitors/requests_test.go b/rackspace/lb/v1/monitors/requests_test.go
new file mode 100644
index 0000000..8c2756f
--- /dev/null
+++ b/rackspace/lb/v1/monitors/requests_test.go
@@ -0,0 +1,46 @@
+package monitors
+
+import (
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+const lbID = 12345
+
+func TestUpdateCONNECT(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockUpdateConnectResponse(t, lbID)
+
+ opts := UpdateConnectMonitorOpts{
+ AttemptLimit: 3,
+ Delay: 10,
+ Timeout: 10,
+ }
+
+ err := Update(client.ServiceClient(), lbID, opts).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestUpdateHTTP(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockUpdateHTTPResponse(t, lbID)
+
+ opts := UpdateHTTPMonitorOpts{
+ AttemptLimit: 3,
+ Delay: 10,
+ Timeout: 10,
+ BodyRegex: "{regex}",
+ Path: "/foo",
+ StatusRegex: "200",
+ Type: HTTPS,
+ }
+
+ err := Update(client.ServiceClient(), lbID, opts).ExtractErr()
+ th.AssertNoErr(t, err)
+}
diff --git a/rackspace/lb/v1/monitors/results.go b/rackspace/lb/v1/monitors/results.go
index 88b76bd..666f504 100644
--- a/rackspace/lb/v1/monitors/results.go
+++ b/rackspace/lb/v1/monitors/results.go
@@ -1,5 +1,7 @@
package monitors
+import "github.com/rackspace/gophercloud"
+
// Type represents the type of Monitor.
type Type string
@@ -16,19 +18,17 @@
// does no post-processing or protocol specific health checks.
type ConnectMonitor struct {
// Number of permissible monitor failures before removing a node from
- // rotation. Must be a number between 1 and 10.
+ // rotation.
AttemptLimit int `mapstructure:"attemptsBeforeDeactivation"`
// The minimum number of seconds to wait before executing the health monitor.
- // Must be a number between 1 and 3600.
Delay int
// Maximum number of seconds to wait for a connection to be established
- // before timing out. Must be a number between 1 and 300.
+ // before timing out.
Timeout int
- // Type of the health monitor. Must be specified as "CONNECT" to monitor
- // connections.
+ // Type of the health monitor.
Type Type
}
@@ -39,19 +39,17 @@
// are used to evaluate the HTTP response.
type HTTPMonitor struct {
// Number of permissible monitor failures before removing a node from
- // rotation. Must be a number between 1 and 10.
+ // rotation.
AttemptLimit int `mapstructure:"attemptsBeforeDeactivation"`
// The minimum number of seconds to wait before executing the health monitor.
- // Must be a number between 1 and 3600.
Delay int
// Maximum number of seconds to wait for a connection to be established
- // before timing out. Must be a number between 1 and 300.
+ // before timing out.
Timeout int
- // Type of the health monitor. Must be specified as "CONNECT" to monitor
- // connections.
+ // Type of the health monitor.
Type Type
// A regular expression that will be used to evaluate the contents of the
@@ -71,3 +69,8 @@
// HTTPSMonitor the HTTPS equivalent of HTTPMonitor
type HTTPSMonitor HTTPMonitor
+
+// UpdateResult represents the result of an Update operation.
+type UpdateResult struct {
+ gophercloud.ErrResult
+}
diff --git a/rackspace/lb/v1/monitors/urls.go b/rackspace/lb/v1/monitors/urls.go
new file mode 100644
index 0000000..0a1e6df
--- /dev/null
+++ b/rackspace/lb/v1/monitors/urls.go
@@ -0,0 +1,16 @@
+package monitors
+
+import (
+ "strconv"
+
+ "github.com/rackspace/gophercloud"
+)
+
+const (
+ path = "loadbalancers"
+ monitorPath = "healthmonitor"
+)
+
+func rootURL(c *gophercloud.ServiceClient, lbID int) string {
+ return c.ServiceURL(path, strconv.Itoa(lbID), monitorPath)
+}