blob: bfdb01ac50840aed4c2fbec1a3d3525c38b8582b [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
Brad Isoncd10b152016-04-06 16:12:26 -040081 _, res.Err = client.Post(createURL(client, groupID, policyID), reqBody, &res.Body, nil)
Brad Isone6e0ec12016-03-27 21:26:46 -040082
Brad Isoncd10b152016-04-06 16:12:26 -040083 return res
Brad Isone6e0ec12016-03-27 21:26:46 -040084}
Brad Ison20644be2016-03-28 19:13:05 -040085
86// Get requests the details of a single webhook with the given ID.
87func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) GetResult {
88 var result GetResult
89
90 _, result.Err = client.Get(getURL(client, groupID, policyID, webhookID), &result.Body, nil)
91
92 return result
93}
Brad Isond9ebfb92016-03-28 21:11:17 -040094
95// UpdateOptsBuilder is the interface responsible for generating the map
96// structure for producing JSON for an Update operation.
97type UpdateOptsBuilder interface {
98 ToWebhookUpdateMap() (map[string]interface{}, error)
99}
100
101// UpdateOpts represents the options for updating an existing webhook.
102//
103// Update operations completely replace the configuration being updated. Empty
104// values in the update are accepted and overwrite previously specified
105// parameters.
106type UpdateOpts struct {
107 // Name of the webhook.
108 Name string `mapstructure:"name" json:"name"`
109
110 // Metadata associated with the webhook.
111 Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
112}
113
114// ToWebhookUpdateMap converts an UpdateOpts struct into a map for use as the
115// request body in an Update request.
116func (opts UpdateOpts) ToWebhookUpdateMap() (map[string]interface{}, error) {
117 if opts.Name == "" {
118 return nil, ErrNoName
119 }
120
121 hook := make(map[string]interface{})
122
123 hook["name"] = opts.Name
124
125 if opts.Metadata != nil {
126 hook["metadata"] = opts.Metadata
127 }
128
129 return hook, nil
130}
131
132// Update requests the configuration of the given webhook be updated.
133func Update(client *gophercloud.ServiceClient, groupID, policyID, webhookID string, opts UpdateOptsBuilder) UpdateResult {
134 var result UpdateResult
135
136 url := updateURL(client, groupID, policyID, webhookID)
137 reqBody, err := opts.ToWebhookUpdateMap()
138
139 if err != nil {
140 result.Err = err
141 return result
142 }
143
144 _, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{
145 OkCodes: []int{204},
146 })
147
148 return result
149}
Brad Isonb5d55482016-03-29 13:19:14 -0400150
151// Delete requests the given webhook be permanently deleted.
152func Delete(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) DeleteResult {
153 var result DeleteResult
154
155 url := deleteURL(client, groupID, policyID, webhookID)
156 _, result.Err = client.Delete(url, &gophercloud.RequestOpts{
157 OkCodes: []int{204},
158 })
159
160 return result
161}