Add connection logging
diff --git a/rackspace/lb/v1/lbs/fixtures.go b/rackspace/lb/v1/lbs/fixtures.go
index 4c497e4..db6545a 100644
--- a/rackspace/lb/v1/lbs/fixtures.go
+++ b/rackspace/lb/v1/lbs/fixtures.go
@@ -378,3 +378,55 @@
`)
})
}
+
+func mockGetLoggingResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/connectionlogging", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "connectionLogging": {
+ "enabled": true
+ }
+}
+ `)
+ })
+}
+
+func mockEnableLoggingResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/connectionlogging", 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, `
+{
+ "connectionLogging":{
+ "enabled":true
+ }
+}
+ `)
+
+ w.WriteHeader(http.StatusOK)
+ })
+}
+
+func mockDisableLoggingResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/connectionlogging", 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, `
+{
+ "connectionLogging":{
+ "enabled":false
+ }
+}
+ `)
+
+ w.WriteHeader(http.StatusOK)
+ })
+}
diff --git a/rackspace/lb/v1/lbs/requests.go b/rackspace/lb/v1/lbs/requests.go
index a26e8ce..81431b0 100644
--- a/rackspace/lb/v1/lbs/requests.go
+++ b/rackspace/lb/v1/lbs/requests.go
@@ -3,6 +3,7 @@
import (
"errors"
+ "github.com/mitchellh/mapstructure"
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
@@ -393,3 +394,61 @@
return AlgorithmPage{pagination.SinglePageBase(r)}
})
}
+
+// IsLoggingEnabled returns true if the load balancer has connection logging
+// enabled and false if not.
+func IsLoggingEnabled(client *gophercloud.ServiceClient, id int) (bool, error) {
+ var body interface{}
+
+ _, err := perigee.Request("GET", loggingURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ Results: &body,
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return false, err
+ }
+
+ var resp struct {
+ CL struct {
+ Enabled bool `mapstructure:"enabled"`
+ } `mapstructure:"connectionLogging"`
+ }
+
+ err = mapstructure.Decode(body, &resp)
+ return resp.CL.Enabled, err
+}
+
+func toConnLoggingMap(state bool) map[string]map[string]bool {
+ return map[string]map[string]bool{
+ "connectionLogging": map[string]bool{"enabled": false},
+ }
+}
+
+// EnableLogging will enable connection logging for a specified load balancer.
+func EnableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
+ reqBody := toConnLoggingMap(true)
+ var res gophercloud.ErrResult
+
+ _, res.Err = perigee.Request("GET", loggingURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{200},
+ })
+
+ return res
+}
+
+// DisableLogging will disable connection logging for a specified load balancer.
+func DisableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
+ reqBody := toConnLoggingMap(false)
+ var res gophercloud.ErrResult
+
+ _, res.Err = perigee.Request("GET", loggingURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{200},
+ })
+
+ return res
+}
diff --git a/rackspace/lb/v1/lbs/requests_test.go b/rackspace/lb/v1/lbs/requests_test.go
index fd50883..35ccfd7 100644
--- a/rackspace/lb/v1/lbs/requests_test.go
+++ b/rackspace/lb/v1/lbs/requests_test.go
@@ -286,3 +286,14 @@
th.AssertNoErr(t, err)
th.AssertEquals(t, 1, count)
}
+
+func TestIsLoggingEnabled(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockGetLoggingResponse(t, id1)
+
+ res, err := IsLoggingEnabled(client.ServiceClient(), id1)
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, true, res)
+}
diff --git a/rackspace/lb/v1/lbs/urls.go b/rackspace/lb/v1/lbs/urls.go
index 7b435b3..5100e94 100644
--- a/rackspace/lb/v1/lbs/urls.go
+++ b/rackspace/lb/v1/lbs/urls.go
@@ -10,6 +10,7 @@
path = "loadbalancers"
protocolsPath = "protocols"
algorithmsPath = "algorithms"
+ logPath = "connectionlogging"
)
func resourceURL(c *gophercloud.ServiceClient, id int) string {
@@ -27,3 +28,7 @@
func algorithmsURL(c *gophercloud.ServiceClient) string {
return c.ServiceURL(path, algorithmsPath)
}
+
+func loggingURL(c *gophercloud.ServiceClient, id int) string {
+ return c.ServiceURL(path, strconv.Itoa(id), logPath)
+}