blob: 9628724af7593dc2275b8b36d493b486ad3970a1 [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
Brad Ison20644be2016-03-28 19:13:05 -040014// Extract interprets any webhookResult as a Webhook, if possible.
15func (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 Isone6e0ec12016-03-27 21:26:46 -040029// 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.
33type CreateResult struct {
34 pagination.SinglePageBase
35}
36
37// ExtractWebhooks extracts a slice of Webhooks from a CreateResult.
38func (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 Isond9ebfb92016-03-28 21:11:17 -040046// GetResult temporarily contains the response from a Get call.
47type GetResult struct {
48 webhookResult
49}
50
51// UpdateResult represents the result of an update operation.
52type UpdateResult struct {
53 gophercloud.ErrResult
54}
55
Brad Ison366a7a02016-03-27 17:06:21 -040056// Webhook represents a webhook associted with a scaling policy.
57type 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.
73type WebhookPage struct {
74 pagination.SinglePageBase
75}
76
77// IsEmpty returns true if a page contains no Webhook results.
78func (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.
90func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
Brad Isone6e0ec12016-03-27 21:26:46 -040091 return commonExtractWebhooks(page.(WebhookPage).Body)
92}
Brad Ison366a7a02016-03-27 17:06:21 -040093
Brad Isone6e0ec12016-03-27 21:26:46 -040094func commonExtractWebhooks(body interface{}) ([]Webhook, error) {
Brad Ison366a7a02016-03-27 17:06:21 -040095 var response struct {
96 Webhooks []Webhook `mapstructure:"webhooks"`
97 }
98
Brad Isone6e0ec12016-03-27 21:26:46 -040099 err := mapstructure.Decode(body, &response)
Brad Ison366a7a02016-03-27 17:06:21 -0400100
101 if err != nil {
102 return nil, err
103 }
104
105 return response.Webhooks, err
106}