blob: ac64d566084a092a432d6e9b6c9e095ae01563cd [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 Isoncd10b152016-04-06 16:12:26 -040029// CreateResult represents the result of a create operation.
Brad Isone6e0ec12016-03-27 21:26:46 -040030type CreateResult struct {
Brad Isoncd10b152016-04-06 16:12:26 -040031 webhookResult
Brad Isone6e0ec12016-03-27 21:26:46 -040032}
33
Brad Isoncd10b152016-04-06 16:12:26 -040034// 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.
37func (res CreateResult) Extract() ([]Webhook, error) {
Brad Isone6e0ec12016-03-27 21:26:46 -040038 if res.Err != nil {
39 return nil, res.Err
40 }
41
42 return commonExtractWebhooks(res.Body)
43}
44
Brad Isond9ebfb92016-03-28 21:11:17 -040045// GetResult temporarily contains the response from a Get call.
46type GetResult struct {
47 webhookResult
48}
49
50// UpdateResult represents the result of an update operation.
51type UpdateResult struct {
52 gophercloud.ErrResult
53}
54
Brad Isonb5d55482016-03-29 13:19:14 -040055// DeleteResult represents the result of a delete operation.
56type DeleteResult struct {
57 gophercloud.ErrResult
58}
59
Brad Ison366a7a02016-03-27 17:06:21 -040060// Webhook represents a webhook associted with a scaling policy.
61type 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.
77type WebhookPage struct {
78 pagination.SinglePageBase
79}
80
81// IsEmpty returns true if a page contains no Webhook results.
82func (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.
94func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
Brad Isone6e0ec12016-03-27 21:26:46 -040095 return commonExtractWebhooks(page.(WebhookPage).Body)
96}
Brad Ison366a7a02016-03-27 17:06:21 -040097
Brad Isone6e0ec12016-03-27 21:26:46 -040098func commonExtractWebhooks(body interface{}) ([]Webhook, error) {
Brad Ison366a7a02016-03-27 17:06:21 -040099 var response struct {
100 Webhooks []Webhook `mapstructure:"webhooks"`
101 }
102
Brad Isone6e0ec12016-03-27 21:26:46 -0400103 err := mapstructure.Decode(body, &response)
Brad Ison366a7a02016-03-27 17:06:21 -0400104
105 if err != nil {
106 return nil, err
107 }
108
109 return response.Webhooks, err
110}