Adding content caching and adding missing tests
diff --git a/rackspace/lb/v1/lbs/fixtures.go b/rackspace/lb/v1/lbs/fixtures.go
index 5f30ec5..2c26d2c 100644
--- a/rackspace/lb/v1/lbs/fixtures.go
+++ b/rackspace/lb/v1/lbs/fixtures.go
@@ -510,3 +510,55 @@
`)
})
}
+
+func mockGetCachingResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/contentcaching", 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, `
+{
+ "contentCaching": {
+ "enabled": true
+ }
+}
+ `)
+ })
+}
+
+func mockEnableCachingResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/contentcaching", 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, `
+{
+ "contentCaching":{
+ "enabled":true
+ }
+}
+ `)
+
+ w.WriteHeader(http.StatusOK)
+ })
+}
+
+func mockDisableCachingResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id)+"/contentcaching", 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, `
+{
+ "contentCaching":{
+ "enabled":false
+ }
+}
+ `)
+
+ w.WriteHeader(http.StatusOK)
+ })
+}
diff --git a/rackspace/lb/v1/lbs/requests.go b/rackspace/lb/v1/lbs/requests.go
index 81e7fdc..2d80062 100644
--- a/rackspace/lb/v1/lbs/requests.go
+++ b/rackspace/lb/v1/lbs/requests.go
@@ -421,7 +421,7 @@
func toConnLoggingMap(state bool) map[string]map[string]bool {
return map[string]map[string]bool{
- "connectionLogging": map[string]bool{"enabled": false},
+ "connectionLogging": map[string]bool{"enabled": state},
}
}
@@ -430,7 +430,7 @@
reqBody := toConnLoggingMap(true)
var res gophercloud.ErrResult
- _, res.Err = perigee.Request("GET", loggingURL(client, id), perigee.Options{
+ _, res.Err = perigee.Request("PUT", loggingURL(client, id), perigee.Options{
MoreHeaders: client.AuthenticatedHeaders(),
ReqBody: &reqBody,
OkCodes: []int{200},
@@ -444,7 +444,7 @@
reqBody := toConnLoggingMap(false)
var res gophercloud.ErrResult
- _, res.Err = perigee.Request("GET", loggingURL(client, id), perigee.Options{
+ _, res.Err = perigee.Request("PUT", loggingURL(client, id), perigee.Options{
MoreHeaders: client.AuthenticatedHeaders(),
ReqBody: &reqBody,
OkCodes: []int{200},
@@ -508,3 +508,57 @@
return res
}
+
+func IsContentCached(client *gophercloud.ServiceClient, id int) (bool, error) {
+ var body interface{}
+
+ _, err := perigee.Request("GET", cacheURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ Results: &body,
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return false, err
+ }
+
+ var resp struct {
+ CC struct {
+ Enabled bool `mapstructure:"enabled"`
+ } `mapstructure:"contentCaching"`
+ }
+
+ err = mapstructure.Decode(body, &resp)
+ return resp.CC.Enabled, err
+}
+
+func toCachingMap(state bool) map[string]map[string]bool {
+ return map[string]map[string]bool{
+ "contentCaching": map[string]bool{"enabled": state},
+ }
+}
+
+func EnableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
+ reqBody := toCachingMap(true)
+ var res gophercloud.ErrResult
+
+ _, res.Err = perigee.Request("PUT", cacheURL(client, id), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{200},
+ })
+
+ return res
+}
+
+func DisableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
+ reqBody := toCachingMap(false)
+ var res gophercloud.ErrResult
+
+ _, res.Err = perigee.Request("PUT", cacheURL(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 59b18e4..32af5c3 100644
--- a/rackspace/lb/v1/lbs/requests_test.go
+++ b/rackspace/lb/v1/lbs/requests_test.go
@@ -298,6 +298,26 @@
th.AssertEquals(t, true, res)
}
+func TestEnablingLogging(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockEnableLoggingResponse(t, id1)
+
+ err := EnableLogging(client.ServiceClient(), id1).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestDisablingLogging(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockDisableLoggingResponse(t, id1)
+
+ err := DisableLogging(client.ServiceClient(), id1).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
func TestGetErrorPage(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -362,3 +382,34 @@
}
th.AssertDeepEquals(t, expected, content)
}
+
+func TestIsCached(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockGetCachingResponse(t, id1)
+
+ res, err := IsContentCached(client.ServiceClient(), id1)
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, true, res)
+}
+
+func TestEnablingCaching(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockEnableCachingResponse(t, id1)
+
+ err := EnableCaching(client.ServiceClient(), id1).ExtractErr()
+ th.AssertNoErr(t, err)
+}
+
+func TestDisablingCaching(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockDisableCachingResponse(t, id1)
+
+ err := DisableCaching(client.ServiceClient(), id1).ExtractErr()
+ th.AssertNoErr(t, err)
+}
diff --git a/rackspace/lb/v1/lbs/urls.go b/rackspace/lb/v1/lbs/urls.go
index f647073..471a86b 100644
--- a/rackspace/lb/v1/lbs/urls.go
+++ b/rackspace/lb/v1/lbs/urls.go
@@ -13,6 +13,7 @@
logPath = "connectionlogging"
epPath = "errorpage"
stPath = "stats"
+ cachePath = "contentcaching"
)
func resourceURL(c *gophercloud.ServiceClient, id int) string {
@@ -42,3 +43,7 @@
func statsURL(c *gophercloud.ServiceClient, id int) string {
return c.ServiceURL(path, strconv.Itoa(id), stPath)
}
+
+func cacheURL(c *gophercloud.ServiceClient, id int) string {
+ return c.ServiceURL(path, strconv.Itoa(id), cachePath)
+}