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