Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame^] | 1 | package webhooks |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | type webhookResult struct { |
| 11 | gophercloud.Result |
| 12 | } |
| 13 | |
| 14 | // Webhook represents a webhook associted with a scaling policy. |
| 15 | type 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. |
| 31 | type WebhookPage struct { |
| 32 | pagination.SinglePageBase |
| 33 | } |
| 34 | |
| 35 | // IsEmpty returns true if a page contains no Webhook results. |
| 36 | func (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. |
| 48 | func 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 | } |