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