| package webhooks |
| |
| import ( |
| "errors" |
| |
| "github.com/rackspace/gophercloud" |
| "github.com/rackspace/gophercloud/pagination" |
| ) |
| |
| // List returns all webhooks for a scaling policy. |
| func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager { |
| url := listURL(client, groupID, policyID) |
| |
| createPageFn := func(r pagination.PageResult) pagination.Page { |
| return WebhookPage{pagination.SinglePageBase(r)} |
| } |
| |
| return pagination.NewPager(client, url, createPageFn) |
| } |
| |
| // CreateOptsBuilder is the interface responsible for generating the JSON |
| // for a Create operation. |
| type CreateOptsBuilder interface { |
| ToWebhookCreateMap() ([]map[string]interface{}, error) |
| } |
| |
| // CreateOpts is a slice of CreateOpt structs, that allow the user to create |
| // multiple webhooks in a single operation. |
| type CreateOpts []CreateOpt |
| |
| // CreateOpt represents the options to create a webhook. |
| type CreateOpt struct { |
| // Name [required] is a name for the webhook. |
| Name string |
| |
| // Metadata [optional] is user-provided key-value metadata. |
| // Maximum length for keys and values is 256 characters. |
| Metadata map[string]string |
| } |
| |
| // ToWebhookCreateMap converts a slice of CreateOpt structs into a map for use |
| // in the request body of a Create operation. |
| func (opts CreateOpts) ToWebhookCreateMap() ([]map[string]interface{}, error) { |
| var webhooks []map[string]interface{} |
| |
| for _, o := range opts { |
| if o.Name == "" { |
| return nil, errors.New("Cannot create a Webhook without a name.") |
| } |
| |
| hook := make(map[string]interface{}) |
| |
| hook["name"] = o.Name |
| |
| if o.Metadata != nil { |
| hook["metadata"] = o.Metadata |
| } |
| |
| webhooks = append(webhooks, hook) |
| } |
| |
| return webhooks, nil |
| } |
| |
| // Create requests a new webhook be created and associated with the given group |
| // and scaling policy. |
| func Create(client *gophercloud.ServiceClient, groupID, policyID string, opts CreateOptsBuilder) CreateResult { |
| var res CreateResult |
| |
| reqBody, err := opts.ToWebhookCreateMap() |
| |
| if err != nil { |
| res.Err = err |
| return res |
| } |
| |
| resp, err := client.Post(createURL(client, groupID, policyID), reqBody, &res.Body, nil) |
| |
| if err != nil { |
| res.Err = err |
| return res |
| } |
| |
| pr := pagination.PageResultFromParsed(resp, res.Body) |
| return CreateResult{pagination.SinglePageBase(pr)} |
| } |
| |
| // Get requests the details of a single webhook with the given ID. |
| func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) GetResult { |
| var result GetResult |
| |
| _, result.Err = client.Get(getURL(client, groupID, policyID, webhookID), &result.Body, nil) |
| |
| return result |
| } |