blob: 2774dd31ca151e5e0afc353c58616ea1e2c3ed8b [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 Isonb5d55482016-03-29 13:19:14 -040056// DeleteResult represents the result of a delete operation.
57type DeleteResult struct {
58 gophercloud.ErrResult
59}
60
Brad Ison366a7a02016-03-27 17:06:21 -040061// Webhook represents a webhook associted with a scaling policy.
62type Webhook struct {
63 // UUID for the webhook.
64 ID string `mapstructure:"id" json:"id"`
65
66 // Name of the webhook.
67 Name string `mapstructure:"name" json:"name"`
68
69 // Links associated with the webhook, including the capability URL.
70 Links []gophercloud.Link `mapstructure:"links" json:"links"`
71
72 // Metadata associated with the webhook.
73 Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
74}
75
76// WebhookPage is the page returned by a pager when traversing over a collection
77// of webhooks.
78type WebhookPage struct {
79 pagination.SinglePageBase
80}
81
82// IsEmpty returns true if a page contains no Webhook results.
83func (page WebhookPage) IsEmpty() (bool, error) {
84 hooks, err := ExtractWebhooks(page)
85
86 if err != nil {
87 return true, err
88 }
89
90 return len(hooks) == 0, nil
91}
92
93// ExtractWebhooks interprets the results of a single page from a List() call,
94// producing a slice of Webhooks.
95func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
Brad Isone6e0ec12016-03-27 21:26:46 -040096 return commonExtractWebhooks(page.(WebhookPage).Body)
97}
Brad Ison366a7a02016-03-27 17:06:21 -040098
Brad Isone6e0ec12016-03-27 21:26:46 -040099func commonExtractWebhooks(body interface{}) ([]Webhook, error) {
Brad Ison366a7a02016-03-27 17:06:21 -0400100 var response struct {
101 Webhooks []Webhook `mapstructure:"webhooks"`
102 }
103
Brad Isone6e0ec12016-03-27 21:26:46 -0400104 err := mapstructure.Decode(body, &response)
Brad Ison366a7a02016-03-27 17:06:21 -0400105
106 if err != nil {
107 return nil, err
108 }
109
110 return response.Webhooks, err
111}