Merge pull request #548 from bison/autoscale-webhooks

[RFR] Rackspace Auto Scale: webhooks
diff --git a/rackspace/autoscale/v1/webhooks/doc.go b/rackspace/autoscale/v1/webhooks/doc.go
new file mode 100644
index 0000000..0b043a9
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/doc.go
@@ -0,0 +1,11 @@
+/*
+Package webhooks provides information and interaction with the webhook API resource
+in the Rackspace Auto Scale service.
+
+Auto Scale uses webhooks to initiate scaling events.  Webhooks are associated
+with scaling policies and provide capability URLs which can be accessed
+anonymously to trigger execution of those policies.  The Auto Scale webhook
+architecture allows Auto Scale to be integrated with other systems, for example,
+monitoring systems.
+*/
+package webhooks
diff --git a/rackspace/autoscale/v1/webhooks/fixtures.go b/rackspace/autoscale/v1/webhooks/fixtures.go
new file mode 100644
index 0000000..daa4c6c
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/fixtures.go
@@ -0,0 +1,226 @@
+// +build fixtures
+
+package webhooks
+
+import (
+	"fmt"
+	"net/http"
+	"testing"
+
+	"github.com/rackspace/gophercloud"
+	th "github.com/rackspace/gophercloud/testhelper"
+	"github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// WebhookListBody contains the canned body of a webhooks.List response.
+const WebhookListBody = `
+{
+  "webhooks": [
+    {
+      "id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
+      "name": "first hook",
+      "links": [
+        {
+          "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/",
+          "rel": "self"
+        },
+        {
+          "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
+          "rel": "capability"
+        }
+      ],
+      "metadata": {}
+    },
+    {
+      "id": "76711c36-dfbe-4f5e-bea6-cded99690515",
+      "name": "second hook",
+      "links": [
+        {
+          "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/76711c36-dfbe-4f5e-bea6-cded99690515/",
+          "rel": "self"
+        },
+        {
+          "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
+          "rel": "capability"
+        }
+      ],
+      "metadata": {
+        "notes": "a note about this webhook"
+      }
+    }
+  ],
+  "webhooks_links": []
+}
+`
+
+// WebhookCreateBody contains the canned body of a webhooks.Create response.
+const WebhookCreateBody = WebhookListBody
+
+// WebhookCreateRequest contains the canned body of a webhooks.Create request.
+const WebhookCreateRequest = `
+[
+  {
+    "name": "first hook"
+  },
+  {
+    "name": "second hook",
+    "metadata": {
+      "notes": "a note about this webhook"
+    }
+  }
+]
+`
+
+// WebhookGetBody contains the canned body of a webhooks.Get response.
+const WebhookGetBody = `
+{
+  "webhook": {
+    "id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
+    "name": "first hook",
+    "links": [
+      {
+        "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/",
+        "rel": "self"
+      },
+      {
+        "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
+        "rel": "capability"
+      }
+    ],
+    "metadata": {}
+  }
+}
+`
+
+// WebhookUpdateRequest contains the canned body of a webhooks.Update request.
+const WebhookUpdateRequest = `
+{
+  "name": "updated hook",
+  "metadata": {
+    "new-key": "some data"
+  }
+}
+`
+
+var (
+	// FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
+	FirstWebhook = Webhook{
+		ID:   "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
+		Name: "first hook",
+		Links: []gophercloud.Link{
+			{
+				Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/",
+				Rel:  "self",
+			},
+			{
+				Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
+				Rel:  "capability",
+			},
+		},
+		Metadata: map[string]string{},
+	}
+
+	// SecondWebhook is a Webhook corresponding to the second result in WebhookListBody.
+	SecondWebhook = Webhook{
+		ID:   "76711c36-dfbe-4f5e-bea6-cded99690515",
+		Name: "second hook",
+		Links: []gophercloud.Link{
+			{
+				Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/76711c36-dfbe-4f5e-bea6-cded99690515/",
+				Rel:  "self",
+			},
+			{
+				Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
+				Rel:  "capability",
+			},
+		},
+		Metadata: map[string]string{
+			"notes": "a note about this webhook",
+		},
+	}
+)
+
+// HandleWebhookListSuccessfully sets up the test server to respond to a webhooks List request.
+func HandleWebhookListSuccessfully(t *testing.T) {
+	path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
+
+	th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+		th.TestMethod(t, r, "GET")
+		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+		w.Header().Add("Content-Type", "application/json")
+
+		fmt.Fprintf(w, WebhookListBody)
+	})
+}
+
+// HandleWebhookCreateSuccessfully sets up the test server to respond to a webhooks Create request.
+func HandleWebhookCreateSuccessfully(t *testing.T) {
+	path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
+
+	th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+		th.TestMethod(t, r, "POST")
+		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+		th.TestHeader(t, r, "Content-Type", "application/json")
+		th.TestHeader(t, r, "Accept", "application/json")
+
+		th.TestJSONRequest(t, r, WebhookCreateRequest)
+
+		w.Header().Add("Content-Type", "application/json")
+		w.WriteHeader(http.StatusCreated)
+
+		fmt.Fprintf(w, WebhookCreateBody)
+	})
+}
+
+// HandleWebhookGetSuccessfully sets up the test server to respond to a webhooks Get request.
+func HandleWebhookGetSuccessfully(t *testing.T) {
+	groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+	policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+	webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
+
+	path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
+
+	th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+		th.TestMethod(t, r, "GET")
+		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+		w.Header().Add("Content-Type", "application/json")
+
+		fmt.Fprintf(w, WebhookGetBody)
+	})
+}
+
+// HandleWebhookUpdateSuccessfully sets up the test server to respond to a webhooks Update request.
+func HandleWebhookUpdateSuccessfully(t *testing.T) {
+	groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+	policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+	webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
+
+	path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
+
+	th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+		th.TestMethod(t, r, "PUT")
+		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+		th.TestJSONRequest(t, r, WebhookUpdateRequest)
+
+		w.WriteHeader(http.StatusNoContent)
+	})
+}
+
+// HandleWebhookDeleteSuccessfully sets up the test server to respond to a webhooks Delete request.
+func HandleWebhookDeleteSuccessfully(t *testing.T) {
+	groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+	policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+	webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
+
+	path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
+
+	th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+		th.TestMethod(t, r, "DELETE")
+		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+		w.WriteHeader(http.StatusNoContent)
+	})
+}
diff --git a/rackspace/autoscale/v1/webhooks/requests.go b/rackspace/autoscale/v1/webhooks/requests.go
new file mode 100644
index 0000000..2a88959
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/requests.go
@@ -0,0 +1,164 @@
+package webhooks
+
+import (
+	"errors"
+
+	"github.com/rackspace/gophercloud"
+	"github.com/rackspace/gophercloud/pagination"
+)
+
+// Validation errors returned by create or update operations.
+var (
+	ErrNoName     = errors.New("Webhook name cannot by empty.")
+	ErrNoMetadata = errors.New("Webhook metadata cannot be nil.")
+)
+
+// List returns all webhooks for a scaling policy.
+func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
+	url := listURL(client, groupID, policyID)
+
+	createPageFn := func(r pagination.PageResult) pagination.Page {
+		return WebhookPage{pagination.SinglePageBase(r)}
+	}
+
+	return pagination.NewPager(client, url, createPageFn)
+}
+
+// CreateOptsBuilder is the interface responsible for generating the JSON
+// for a Create operation.
+type CreateOptsBuilder interface {
+	ToWebhookCreateMap() ([]map[string]interface{}, error)
+}
+
+// CreateOpts is a slice of CreateOpt structs, that allow the user to create
+// multiple webhooks in a single operation.
+type CreateOpts []CreateOpt
+
+// CreateOpt represents the options to create a webhook.
+type CreateOpt struct {
+	// Name [required] is a name for the webhook.
+	Name string
+
+	// Metadata [optional] is user-provided key-value metadata.
+	// Maximum length for keys and values is 256 characters.
+	Metadata map[string]string
+}
+
+// ToWebhookCreateMap converts a slice of CreateOpt structs into a map for use
+// in the request body of a Create operation.
+func (opts CreateOpts) ToWebhookCreateMap() ([]map[string]interface{}, error) {
+	var webhooks []map[string]interface{}
+
+	for _, o := range opts {
+		if o.Name == "" {
+			return nil, ErrNoName
+		}
+
+		hook := make(map[string]interface{})
+
+		hook["name"] = o.Name
+
+		if o.Metadata != nil {
+			hook["metadata"] = o.Metadata
+		}
+
+		webhooks = append(webhooks, hook)
+	}
+
+	return webhooks, nil
+}
+
+// Create requests a new webhook be created and associated with the given group
+// and scaling policy.
+func Create(client *gophercloud.ServiceClient, groupID, policyID string, opts CreateOptsBuilder) CreateResult {
+	var res CreateResult
+
+	reqBody, err := opts.ToWebhookCreateMap()
+
+	if err != nil {
+		res.Err = err
+		return res
+	}
+
+	_, res.Err = client.Post(createURL(client, groupID, policyID), reqBody, &res.Body, nil)
+
+	return res
+}
+
+// Get requests the details of a single webhook with the given ID.
+func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) GetResult {
+	var result GetResult
+
+	_, result.Err = client.Get(getURL(client, groupID, policyID, webhookID), &result.Body, nil)
+
+	return result
+}
+
+// UpdateOptsBuilder is the interface responsible for generating the map
+// structure for producing JSON for an Update operation.
+type UpdateOptsBuilder interface {
+	ToWebhookUpdateMap() (map[string]interface{}, error)
+}
+
+// UpdateOpts represents the options for updating an existing webhook.
+//
+// Update operations completely replace the configuration being updated. Empty
+// values in the update are accepted and overwrite previously specified
+// parameters.
+type UpdateOpts struct {
+	// Name of the webhook.
+	Name string `mapstructure:"name" json:"name"`
+
+	// Metadata associated with the webhook.
+	Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
+}
+
+// ToWebhookUpdateMap converts an UpdateOpts struct into a map for use as the
+// request body in an Update request.
+func (opts UpdateOpts) ToWebhookUpdateMap() (map[string]interface{}, error) {
+	if opts.Name == "" {
+		return nil, ErrNoName
+	}
+
+	if opts.Metadata == nil {
+		return nil, ErrNoMetadata
+	}
+
+	hook := make(map[string]interface{})
+
+	hook["name"] = opts.Name
+	hook["metadata"] = opts.Metadata
+
+	return hook, nil
+}
+
+// Update requests the configuration of the given webhook be updated.
+func Update(client *gophercloud.ServiceClient, groupID, policyID, webhookID string, opts UpdateOptsBuilder) UpdateResult {
+	var result UpdateResult
+
+	url := updateURL(client, groupID, policyID, webhookID)
+	reqBody, err := opts.ToWebhookUpdateMap()
+
+	if err != nil {
+		result.Err = err
+		return result
+	}
+
+	_, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{
+		OkCodes: []int{204},
+	})
+
+	return result
+}
+
+// Delete requests the given webhook be permanently deleted.
+func Delete(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) DeleteResult {
+	var result DeleteResult
+
+	url := deleteURL(client, groupID, policyID, webhookID)
+	_, result.Err = client.Delete(url, &gophercloud.RequestOpts{
+		OkCodes: []int{204},
+	})
+
+	return result
+}
diff --git a/rackspace/autoscale/v1/webhooks/requests_test.go b/rackspace/autoscale/v1/webhooks/requests_test.go
new file mode 100644
index 0000000..950c90d
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/requests_test.go
@@ -0,0 +1,132 @@
+package webhooks
+
+import (
+	"testing"
+
+	"github.com/rackspace/gophercloud/pagination"
+	th "github.com/rackspace/gophercloud/testhelper"
+	"github.com/rackspace/gophercloud/testhelper/client"
+)
+
+const (
+	groupID  = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+	policyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+	firstID  = "2bd1822c-58c5-49fd-8b3d-ed44781a58d1" // FirstWebhook
+	secondID = "76711c36-dfbe-4f5e-bea6-cded99690515" // SecondWebhook
+)
+
+func TestList(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleWebhookListSuccessfully(t)
+
+	pages := 0
+	pager := List(client.ServiceClient(), groupID, policyID)
+
+	err := pager.EachPage(func(page pagination.Page) (bool, error) {
+		pages++
+
+		webhooks, err := ExtractWebhooks(page)
+
+		if err != nil {
+			return false, err
+		}
+
+		if len(webhooks) != 2 {
+			t.Fatalf("Expected 2 policies, got %d", len(webhooks))
+		}
+
+		th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
+		th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
+
+		return true, nil
+	})
+
+	th.AssertNoErr(t, err)
+
+	if pages != 1 {
+		t.Errorf("Expected 1 page, saw %d", pages)
+	}
+}
+
+func TestCreate(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleWebhookCreateSuccessfully(t)
+
+	client := client.ServiceClient()
+	opts := CreateOpts{
+		{
+			Name: "first hook",
+		},
+		{
+			Name: "second hook",
+			Metadata: map[string]string{
+				"notes": "a note about this webhook",
+			},
+		},
+	}
+
+	webhooks, err := Create(client, groupID, policyID, opts).Extract()
+
+	th.AssertNoErr(t, err)
+	th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
+	th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
+}
+
+func TestGet(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleWebhookGetSuccessfully(t)
+
+	client := client.ServiceClient()
+
+	webhook, err := Get(client, groupID, policyID, firstID).Extract()
+
+	th.AssertNoErr(t, err)
+	th.CheckDeepEquals(t, FirstWebhook, *webhook)
+}
+
+func TestUpdate(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleWebhookUpdateSuccessfully(t)
+
+	client := client.ServiceClient()
+	opts := UpdateOpts{
+		Name: "updated hook",
+		Metadata: map[string]string{
+			"new-key": "some data",
+		},
+	}
+
+	err := Update(client, groupID, policyID, firstID, opts).ExtractErr()
+
+	th.AssertNoErr(t, err)
+}
+
+func TestUpdateNoMetadata(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleWebhookUpdateSuccessfully(t)
+
+	client := client.ServiceClient()
+	opts := UpdateOpts{
+		Name: "updated hook",
+	}
+
+	err := Update(client, groupID, policyID, firstID, opts).ExtractErr()
+
+	th.AssertEquals(t, ErrNoMetadata, err)
+}
+
+func TestDelete(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleWebhookDeleteSuccessfully(t)
+
+	client := client.ServiceClient()
+	err := Delete(client, groupID, policyID, firstID).ExtractErr()
+
+	th.AssertNoErr(t, err)
+}
diff --git a/rackspace/autoscale/v1/webhooks/results.go b/rackspace/autoscale/v1/webhooks/results.go
new file mode 100644
index 0000000..ac64d56
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/results.go
@@ -0,0 +1,110 @@
+package webhooks
+
+import (
+	"github.com/mitchellh/mapstructure"
+
+	"github.com/rackspace/gophercloud"
+	"github.com/rackspace/gophercloud/pagination"
+)
+
+type webhookResult struct {
+	gophercloud.Result
+}
+
+// Extract interprets any webhookResult as a Webhook, if possible.
+func (r webhookResult) Extract() (*Webhook, error) {
+	if r.Err != nil {
+		return nil, r.Err
+	}
+
+	var response struct {
+		Webhook Webhook `mapstructure:"webhook"`
+	}
+
+	err := mapstructure.Decode(r.Body, &response)
+
+	return &response.Webhook, err
+}
+
+// CreateResult represents the result of a create operation.
+type CreateResult struct {
+	webhookResult
+}
+
+// Extract extracts a slice of Webhooks from a CreateResult.  Multiple webhooks
+// can be created in a single operation, so the result of a create is always a
+// list of webhooks.
+func (res CreateResult) Extract() ([]Webhook, error) {
+	if res.Err != nil {
+		return nil, res.Err
+	}
+
+	return commonExtractWebhooks(res.Body)
+}
+
+// GetResult temporarily contains the response from a Get call.
+type GetResult struct {
+	webhookResult
+}
+
+// UpdateResult represents the result of an update operation.
+type UpdateResult struct {
+	gophercloud.ErrResult
+}
+
+// DeleteResult represents the result of a delete operation.
+type DeleteResult struct {
+	gophercloud.ErrResult
+}
+
+// Webhook represents a webhook associted with a scaling policy.
+type Webhook struct {
+	// UUID for the webhook.
+	ID string `mapstructure:"id" json:"id"`
+
+	// Name of the webhook.
+	Name string `mapstructure:"name" json:"name"`
+
+	// Links associated with the webhook, including the capability URL.
+	Links []gophercloud.Link `mapstructure:"links" json:"links"`
+
+	// Metadata associated with the webhook.
+	Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
+}
+
+// WebhookPage is the page returned by a pager when traversing over a collection
+// of webhooks.
+type WebhookPage struct {
+	pagination.SinglePageBase
+}
+
+// IsEmpty returns true if a page contains no Webhook results.
+func (page WebhookPage) IsEmpty() (bool, error) {
+	hooks, err := ExtractWebhooks(page)
+
+	if err != nil {
+		return true, err
+	}
+
+	return len(hooks) == 0, nil
+}
+
+// ExtractWebhooks interprets the results of a single page from a List() call,
+// producing a slice of Webhooks.
+func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
+	return commonExtractWebhooks(page.(WebhookPage).Body)
+}
+
+func commonExtractWebhooks(body interface{}) ([]Webhook, error) {
+	var response struct {
+		Webhooks []Webhook `mapstructure:"webhooks"`
+	}
+
+	err := mapstructure.Decode(body, &response)
+
+	if err != nil {
+		return nil, err
+	}
+
+	return response.Webhooks, err
+}
diff --git a/rackspace/autoscale/v1/webhooks/urls.go b/rackspace/autoscale/v1/webhooks/urls.go
new file mode 100644
index 0000000..f5432e1
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/urls.go
@@ -0,0 +1,23 @@
+package webhooks
+
+import "github.com/rackspace/gophercloud"
+
+func listURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
+	return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks")
+}
+
+func createURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
+	return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks")
+}
+
+func getURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
+	return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks", webhookID)
+}
+
+func updateURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
+	return getURL(c, groupID, policyID, webhookID)
+}
+
+func deleteURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
+	return getURL(c, groupID, policyID, webhookID)
+}
diff --git a/rackspace/autoscale/v1/webhooks/urls_test.go b/rackspace/autoscale/v1/webhooks/urls_test.go
new file mode 100644
index 0000000..44661e6
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/urls_test.go
@@ -0,0 +1,44 @@
+package webhooks
+
+import (
+	"testing"
+
+	"github.com/rackspace/gophercloud"
+	th "github.com/rackspace/gophercloud/testhelper"
+)
+
+const endpoint = "http://localhost:57909/"
+
+func endpointClient() *gophercloud.ServiceClient {
+	return &gophercloud.ServiceClient{Endpoint: endpoint}
+}
+
+func TestListURL(t *testing.T) {
+	actual := listURL(endpointClient(), "123", "456")
+	expected := endpoint + "groups/123/policies/456/webhooks"
+	th.CheckEquals(t, expected, actual)
+}
+
+func TestCreateURL(t *testing.T) {
+	actual := createURL(endpointClient(), "123", "456")
+	expected := endpoint + "groups/123/policies/456/webhooks"
+	th.CheckEquals(t, expected, actual)
+}
+
+func TestGetURL(t *testing.T) {
+	actual := getURL(endpointClient(), "123", "456", "789")
+	expected := endpoint + "groups/123/policies/456/webhooks/789"
+	th.CheckEquals(t, expected, actual)
+}
+
+func TestUpdateURL(t *testing.T) {
+	actual := updateURL(endpointClient(), "123", "456", "789")
+	expected := endpoint + "groups/123/policies/456/webhooks/789"
+	th.CheckEquals(t, expected, actual)
+}
+
+func TestDeleteURL(t *testing.T) {
+	actual := deleteURL(endpointClient(), "123", "456", "789")
+	expected := endpoint + "groups/123/policies/456/webhooks/789"
+	th.CheckEquals(t, expected, actual)
+}
diff --git a/rackspace/client.go b/rackspace/client.go
index a8f413e..09e9006 100644
--- a/rackspace/client.go
+++ b/rackspace/client.go
@@ -222,3 +222,13 @@
 	}
 	return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
 }
+
+// NewAutoScaleV1 creates a ServiceClient that may be used to access the v1 Auto Scale service.
+func NewAutoScaleV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
+	eo.ApplyDefaults("rax:autoscale")
+	url, err := client.EndpointLocator(eo)
+	if err != nil {
+		return nil, err
+	}
+	return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
+}