blob: a15e20e35dbfc37b6828ef1b8db8b88016e26ee4 [file] [log] [blame]
Brad Ison366a7a02016-03-27 17:06:21 -04001package webhooks
2
3import (
4 "github.com/mitchellh/mapstructure"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10type webhookResult struct {
11 gophercloud.Result
12}
13
14// Webhook represents a webhook associted with a scaling policy.
15type Webhook struct {
16 // UUID for the webhook.
17 ID string `mapstructure:"id" json:"id"`
18
19 // Name of the webhook.
20 Name string `mapstructure:"name" json:"name"`
21
22 // Links associated with the webhook, including the capability URL.
23 Links []gophercloud.Link `mapstructure:"links" json:"links"`
24
25 // Metadata associated with the webhook.
26 Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
27}
28
29// WebhookPage is the page returned by a pager when traversing over a collection
30// of webhooks.
31type WebhookPage struct {
32 pagination.SinglePageBase
33}
34
35// IsEmpty returns true if a page contains no Webhook results.
36func (page WebhookPage) IsEmpty() (bool, error) {
37 hooks, err := ExtractWebhooks(page)
38
39 if err != nil {
40 return true, err
41 }
42
43 return len(hooks) == 0, nil
44}
45
46// ExtractWebhooks interprets the results of a single page from a List() call,
47// producing a slice of Webhooks.
48func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
49 casted := page.(WebhookPage).Body
50
51 var response struct {
52 Webhooks []Webhook `mapstructure:"webhooks"`
53 }
54
55 err := mapstructure.Decode(casted, &response)
56
57 if err != nil {
58 return nil, err
59 }
60
61 return response.Webhooks, err
62}