| 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 |  | 
| Brad Ison | d9ebfb9 | 2016-03-28 21:11:17 -0400 | [diff] [blame] | 10 | // ErrNoName represents a validation error in which a create or update operation | 
 | 11 | // has an empty name field. | 
 | 12 | var ErrNoName = errors.New("Webhook name cannot by empty.") | 
 | 13 |  | 
| Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 14 | // List returns all webhooks for a scaling policy. | 
 | 15 | func 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 Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 24 |  | 
 | 25 | // CreateOptsBuilder is the interface responsible for generating the JSON | 
 | 26 | // for a Create operation. | 
 | 27 | type 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. | 
 | 33 | type CreateOpts []CreateOpt | 
 | 34 |  | 
 | 35 | // CreateOpt represents the options to create a webhook. | 
 | 36 | type 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. | 
 | 47 | func (opts CreateOpts) ToWebhookCreateMap() ([]map[string]interface{}, error) { | 
 | 48 | 	var webhooks []map[string]interface{} | 
 | 49 |  | 
 | 50 | 	for _, o := range opts { | 
 | 51 | 		if o.Name == "" { | 
| Brad Ison | d9ebfb9 | 2016-03-28 21:11:17 -0400 | [diff] [blame] | 52 | 			return nil, ErrNoName | 
| Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 53 | 		} | 
 | 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. | 
 | 71 | func 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 Ison | 20644be | 2016-03-28 19:13:05 -0400 | [diff] [blame] | 91 |  | 
 | 92 | // Get requests the details of a single webhook with the given ID. | 
 | 93 | func 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 Ison | d9ebfb9 | 2016-03-28 21:11:17 -0400 | [diff] [blame] | 100 |  | 
 | 101 | // UpdateOptsBuilder is the interface responsible for generating the map | 
 | 102 | // structure for producing JSON for an Update operation. | 
 | 103 | type 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. | 
 | 112 | type 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. | 
 | 122 | func (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. | 
 | 139 | func 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 Ison | b5d5548 | 2016-03-29 13:19:14 -0400 | [diff] [blame^] | 156 |  | 
 | 157 | // Delete requests the given webhook be permanently deleted. | 
 | 158 | func 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 | } |