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 | |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame^] | 14 | // CreateResult represents the result of a create operation. Multiple webhooks |
| 15 | // can be created in a single call, so the result should be treated as a typical |
| 16 | // pagination Page. ExtractWebhooks() can be used to extract a slice of |
| 17 | // Webhooks from a single page. |
| 18 | type CreateResult struct { |
| 19 | pagination.SinglePageBase |
| 20 | } |
| 21 | |
| 22 | // ExtractWebhooks extracts a slice of Webhooks from a CreateResult. |
| 23 | func (res CreateResult) ExtractWebhooks() ([]Webhook, error) { |
| 24 | if res.Err != nil { |
| 25 | return nil, res.Err |
| 26 | } |
| 27 | |
| 28 | return commonExtractWebhooks(res.Body) |
| 29 | } |
| 30 | |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 31 | // Webhook represents a webhook associted with a scaling policy. |
| 32 | type Webhook struct { |
| 33 | // UUID for the webhook. |
| 34 | ID string `mapstructure:"id" json:"id"` |
| 35 | |
| 36 | // Name of the webhook. |
| 37 | Name string `mapstructure:"name" json:"name"` |
| 38 | |
| 39 | // Links associated with the webhook, including the capability URL. |
| 40 | Links []gophercloud.Link `mapstructure:"links" json:"links"` |
| 41 | |
| 42 | // Metadata associated with the webhook. |
| 43 | Metadata map[string]string `mapstructure:"metadata" json:"metadata"` |
| 44 | } |
| 45 | |
| 46 | // WebhookPage is the page returned by a pager when traversing over a collection |
| 47 | // of webhooks. |
| 48 | type WebhookPage struct { |
| 49 | pagination.SinglePageBase |
| 50 | } |
| 51 | |
| 52 | // IsEmpty returns true if a page contains no Webhook results. |
| 53 | func (page WebhookPage) IsEmpty() (bool, error) { |
| 54 | hooks, err := ExtractWebhooks(page) |
| 55 | |
| 56 | if err != nil { |
| 57 | return true, err |
| 58 | } |
| 59 | |
| 60 | return len(hooks) == 0, nil |
| 61 | } |
| 62 | |
| 63 | // ExtractWebhooks interprets the results of a single page from a List() call, |
| 64 | // producing a slice of Webhooks. |
| 65 | func ExtractWebhooks(page pagination.Page) ([]Webhook, error) { |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame^] | 66 | return commonExtractWebhooks(page.(WebhookPage).Body) |
| 67 | } |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 68 | |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame^] | 69 | func commonExtractWebhooks(body interface{}) ([]Webhook, error) { |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 70 | var response struct { |
| 71 | Webhooks []Webhook `mapstructure:"webhooks"` |
| 72 | } |
| 73 | |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame^] | 74 | err := mapstructure.Decode(body, &response) |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 75 | |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | return response.Webhooks, err |
| 81 | } |