blob: 2a073575bac8861d1c33fb078a60e2cc95b16928 [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
Brad Isond9ebfb92016-03-28 21:11:17 -040010// ErrNoName represents a validation error in which a create or update operation
11// has an empty name field.
12var ErrNoName = errors.New("Webhook name cannot by empty.")
13
Brad Ison366a7a02016-03-27 17:06:21 -040014// List returns all webhooks for a scaling policy.
15func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
16 url := listURL(client, groupID, policyID)
17
18 createPageFn := func(r pagination.PageResult) pagination.Page {
19 return WebhookPage{pagination.SinglePageBase(r)}
20 }
21
22 return pagination.NewPager(client, url, createPageFn)
23}
Brad Isone6e0ec12016-03-27 21:26:46 -040024
25// CreateOptsBuilder is the interface responsible for generating the JSON
26// for a Create operation.
27type CreateOptsBuilder interface {
28 ToWebhookCreateMap() ([]map[string]interface{}, error)
29}
30
31// CreateOpts is a slice of CreateOpt structs, that allow the user to create
32// multiple webhooks in a single operation.
33type CreateOpts []CreateOpt
34
35// CreateOpt represents the options to create a webhook.
36type CreateOpt struct {
37 // Name [required] is a name for the webhook.
38 Name string
39
40 // Metadata [optional] is user-provided key-value metadata.
41 // Maximum length for keys and values is 256 characters.
42 Metadata map[string]string
43}
44
45// ToWebhookCreateMap converts a slice of CreateOpt structs into a map for use
46// in the request body of a Create operation.
47func (opts CreateOpts) ToWebhookCreateMap() ([]map[string]interface{}, error) {
48 var webhooks []map[string]interface{}
49
50 for _, o := range opts {
51 if o.Name == "" {
Brad Isond9ebfb92016-03-28 21:11:17 -040052 return nil, ErrNoName
Brad Isone6e0ec12016-03-27 21:26:46 -040053 }
54
55 hook := make(map[string]interface{})
56
57 hook["name"] = o.Name
58
59 if o.Metadata != nil {
60 hook["metadata"] = o.Metadata
61 }
62
63 webhooks = append(webhooks, hook)
64 }
65
66 return webhooks, nil
67}
68
69// Create requests a new webhook be created and associated with the given group
70// and scaling policy.
71func Create(client *gophercloud.ServiceClient, groupID, policyID string, opts CreateOptsBuilder) CreateResult {
72 var res CreateResult
73
74 reqBody, err := opts.ToWebhookCreateMap()
75
76 if err != nil {
77 res.Err = err
78 return res
79 }
80
81 resp, err := client.Post(createURL(client, groupID, policyID), reqBody, &res.Body, nil)
82
83 if err != nil {
84 res.Err = err
85 return res
86 }
87
88 pr := pagination.PageResultFromParsed(resp, res.Body)
89 return CreateResult{pagination.SinglePageBase(pr)}
90}
Brad Ison20644be2016-03-28 19:13:05 -040091
92// Get requests the details of a single webhook with the given ID.
93func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) GetResult {
94 var result GetResult
95
96 _, result.Err = client.Get(getURL(client, groupID, policyID, webhookID), &result.Body, nil)
97
98 return result
99}
Brad Isond9ebfb92016-03-28 21:11:17 -0400100
101// UpdateOptsBuilder is the interface responsible for generating the map
102// structure for producing JSON for an Update operation.
103type UpdateOptsBuilder interface {
104 ToWebhookUpdateMap() (map[string]interface{}, error)
105}
106
107// UpdateOpts represents the options for updating an existing webhook.
108//
109// Update operations completely replace the configuration being updated. Empty
110// values in the update are accepted and overwrite previously specified
111// parameters.
112type UpdateOpts struct {
113 // Name of the webhook.
114 Name string `mapstructure:"name" json:"name"`
115
116 // Metadata associated with the webhook.
117 Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
118}
119
120// ToWebhookUpdateMap converts an UpdateOpts struct into a map for use as the
121// request body in an Update request.
122func (opts UpdateOpts) ToWebhookUpdateMap() (map[string]interface{}, error) {
123 if opts.Name == "" {
124 return nil, ErrNoName
125 }
126
127 hook := make(map[string]interface{})
128
129 hook["name"] = opts.Name
130
131 if opts.Metadata != nil {
132 hook["metadata"] = opts.Metadata
133 }
134
135 return hook, nil
136}
137
138// Update requests the configuration of the given webhook be updated.
139func Update(client *gophercloud.ServiceClient, groupID, policyID, webhookID string, opts UpdateOptsBuilder) UpdateResult {
140 var result UpdateResult
141
142 url := updateURL(client, groupID, policyID, webhookID)
143 reqBody, err := opts.ToWebhookUpdateMap()
144
145 if err != nil {
146 result.Err = err
147 return result
148 }
149
150 _, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{
151 OkCodes: []int{204},
152 })
153
154 return result
155}
Brad Isonb5d55482016-03-29 13:19:14 -0400156
157// Delete requests the given webhook be permanently deleted.
158func Delete(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) DeleteResult {
159 var result DeleteResult
160
161 url := deleteURL(client, groupID, policyID, webhookID)
162 _, result.Err = client.Delete(url, &gophercloud.RequestOpts{
163 OkCodes: []int{204},
164 })
165
166 return result
167}