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