blob: 0fa550e41ba79f066739ba98a755da01d5496d7f [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
Brad Ison20644be2016-03-28 19:13:05 -040037// GetResult temporarily contains the response from a Get call.
38type GetResult struct {
39 webhookResult
40}
41
Brad Isone6e0ec12016-03-27 21:26:46 -040042// ExtractWebhooks extracts a slice of Webhooks from a CreateResult.
43func (res CreateResult) ExtractWebhooks() ([]Webhook, error) {
44 if res.Err != nil {
45 return nil, res.Err
46 }
47
48 return commonExtractWebhooks(res.Body)
49}
50
Brad Ison366a7a02016-03-27 17:06:21 -040051// Webhook represents a webhook associted with a scaling policy.
52type Webhook struct {
53 // UUID for the webhook.
54 ID string `mapstructure:"id" json:"id"`
55
56 // Name of the webhook.
57 Name string `mapstructure:"name" json:"name"`
58
59 // Links associated with the webhook, including the capability URL.
60 Links []gophercloud.Link `mapstructure:"links" json:"links"`
61
62 // Metadata associated with the webhook.
63 Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
64}
65
66// WebhookPage is the page returned by a pager when traversing over a collection
67// of webhooks.
68type WebhookPage struct {
69 pagination.SinglePageBase
70}
71
72// IsEmpty returns true if a page contains no Webhook results.
73func (page WebhookPage) IsEmpty() (bool, error) {
74 hooks, err := ExtractWebhooks(page)
75
76 if err != nil {
77 return true, err
78 }
79
80 return len(hooks) == 0, nil
81}
82
83// ExtractWebhooks interprets the results of a single page from a List() call,
84// producing a slice of Webhooks.
85func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
Brad Isone6e0ec12016-03-27 21:26:46 -040086 return commonExtractWebhooks(page.(WebhookPage).Body)
87}
Brad Ison366a7a02016-03-27 17:06:21 -040088
Brad Isone6e0ec12016-03-27 21:26:46 -040089func commonExtractWebhooks(body interface{}) ([]Webhook, error) {
Brad Ison366a7a02016-03-27 17:06:21 -040090 var response struct {
91 Webhooks []Webhook `mapstructure:"webhooks"`
92 }
93
Brad Isone6e0ec12016-03-27 21:26:46 -040094 err := mapstructure.Decode(body, &response)
Brad Ison366a7a02016-03-27 17:06:21 -040095
96 if err != nil {
97 return nil, err
98 }
99
100 return response.Webhooks, err
101}