blob: 410f5cb580c6c962a7629b270766adcff64d0681 [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 Isone6e0ec12016-03-27 21:26:46 -040014// CreateResult represents the result of a create operation. Multiple webhooks
15// can be created in a single call, so the result should be treated as a typical
16// pagination Page. ExtractWebhooks() can be used to extract a slice of
17// Webhooks from a single page.
18type CreateResult struct {
19 pagination.SinglePageBase
20}
21
22// ExtractWebhooks extracts a slice of Webhooks from a CreateResult.
23func (res CreateResult) ExtractWebhooks() ([]Webhook, error) {
24 if res.Err != nil {
25 return nil, res.Err
26 }
27
28 return commonExtractWebhooks(res.Body)
29}
30
Brad Ison366a7a02016-03-27 17:06:21 -040031// Webhook represents a webhook associted with a scaling policy.
32type Webhook struct {
33 // UUID for the webhook.
34 ID string `mapstructure:"id" json:"id"`
35
36 // Name of the webhook.
37 Name string `mapstructure:"name" json:"name"`
38
39 // Links associated with the webhook, including the capability URL.
40 Links []gophercloud.Link `mapstructure:"links" json:"links"`
41
42 // Metadata associated with the webhook.
43 Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
44}
45
46// WebhookPage is the page returned by a pager when traversing over a collection
47// of webhooks.
48type WebhookPage struct {
49 pagination.SinglePageBase
50}
51
52// IsEmpty returns true if a page contains no Webhook results.
53func (page WebhookPage) IsEmpty() (bool, error) {
54 hooks, err := ExtractWebhooks(page)
55
56 if err != nil {
57 return true, err
58 }
59
60 return len(hooks) == 0, nil
61}
62
63// ExtractWebhooks interprets the results of a single page from a List() call,
64// producing a slice of Webhooks.
65func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
Brad Isone6e0ec12016-03-27 21:26:46 -040066 return commonExtractWebhooks(page.(WebhookPage).Body)
67}
Brad Ison366a7a02016-03-27 17:06:21 -040068
Brad Isone6e0ec12016-03-27 21:26:46 -040069func commonExtractWebhooks(body interface{}) ([]Webhook, error) {
Brad Ison366a7a02016-03-27 17:06:21 -040070 var response struct {
71 Webhooks []Webhook `mapstructure:"webhooks"`
72 }
73
Brad Isone6e0ec12016-03-27 21:26:46 -040074 err := mapstructure.Decode(body, &response)
Brad Ison366a7a02016-03-27 17:06:21 -040075
76 if err != nil {
77 return nil, err
78 }
79
80 return response.Webhooks, err
81}