Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 1 | package webhooks |
| 2 | |
| 3 | import ( |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 4 | "errors" |
| 5 | |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // List returns all webhooks for a scaling policy. |
| 11 | func 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 Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 20 | |
| 21 | // CreateOptsBuilder is the interface responsible for generating the JSON |
| 22 | // for a Create operation. |
| 23 | type 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. |
| 29 | type CreateOpts []CreateOpt |
| 30 | |
| 31 | // CreateOpt represents the options to create a webhook. |
| 32 | type 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. |
| 43 | func (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. |
| 67 | func 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 Ison | 20644be | 2016-03-28 19:13:05 -0400 | [diff] [blame^] | 87 | |
| 88 | // Get requests the details of a single webhook with the given ID. |
| 89 | func 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 | } |