blob: 38aa4a751e4156d514802161edd922db85f06956 [file] [log] [blame]
Brad Ison366a7a02016-03-27 17:06:21 -04001package webhooks
2
3import (
Brad Isone6e0ec12016-03-27 21:26:46 -04004 "errors"
5
Brad Ison366a7a02016-03-27 17:06:21 -04006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10// List returns all webhooks for a scaling policy.
11func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
12 url := listURL(client, groupID, policyID)
13
14 createPageFn := func(r pagination.PageResult) pagination.Page {
15 return WebhookPage{pagination.SinglePageBase(r)}
16 }
17
18 return pagination.NewPager(client, url, createPageFn)
19}
Brad Isone6e0ec12016-03-27 21:26:46 -040020
21// CreateOptsBuilder is the interface responsible for generating the JSON
22// for a Create operation.
23type CreateOptsBuilder interface {
24 ToWebhookCreateMap() ([]map[string]interface{}, error)
25}
26
27// CreateOpts is a slice of CreateOpt structs, that allow the user to create
28// multiple webhooks in a single operation.
29type CreateOpts []CreateOpt
30
31// CreateOpt represents the options to create a webhook.
32type CreateOpt struct {
33 // Name [required] is a name for the webhook.
34 Name string
35
36 // Metadata [optional] is user-provided key-value metadata.
37 // Maximum length for keys and values is 256 characters.
38 Metadata map[string]string
39}
40
41// ToWebhookCreateMap converts a slice of CreateOpt structs into a map for use
42// in the request body of a Create operation.
43func (opts CreateOpts) ToWebhookCreateMap() ([]map[string]interface{}, error) {
44 var webhooks []map[string]interface{}
45
46 for _, o := range opts {
47 if o.Name == "" {
48 return nil, errors.New("Cannot create a Webhook without a name.")
49 }
50
51 hook := make(map[string]interface{})
52
53 hook["name"] = o.Name
54
55 if o.Metadata != nil {
56 hook["metadata"] = o.Metadata
57 }
58
59 webhooks = append(webhooks, hook)
60 }
61
62 return webhooks, nil
63}
64
65// Create requests a new webhook be created and associated with the given group
66// and scaling policy.
67func Create(client *gophercloud.ServiceClient, groupID, policyID string, opts CreateOptsBuilder) CreateResult {
68 var res CreateResult
69
70 reqBody, err := opts.ToWebhookCreateMap()
71
72 if err != nil {
73 res.Err = err
74 return res
75 }
76
77 resp, err := client.Post(createURL(client, groupID, policyID), reqBody, &res.Body, nil)
78
79 if err != nil {
80 res.Err = err
81 return res
82 }
83
84 pr := pagination.PageResultFromParsed(resp, res.Body)
85 return CreateResult{pagination.SinglePageBase(pr)}
86}
Brad Ison20644be2016-03-28 19:13:05 -040087
88// Get requests the details of a single webhook with the given ID.
89func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) GetResult {
90 var result GetResult
91
92 _, result.Err = client.Get(getURL(client, groupID, policyID, webhookID), &result.Body, nil)
93
94 return result
95}