Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 1 | package bulk |
| 2 | |
| 3 | import ( |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 4 | "net/url" |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 5 | "strings" |
| 6 | |
| 7 | "github.com/racker/perigee" |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | ) |
| 10 | |
| 11 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 12 | // Delete request. |
| 13 | type DeleteOptsBuilder interface { |
| 14 | ToBulkDeleteBody() (string, error) |
| 15 | } |
| 16 | |
| 17 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 18 | type DeleteOpts []string |
| 19 | |
| 20 | // ToBulkDeleteBody formats a DeleteOpts into a request body. |
| 21 | func (opts DeleteOpts) ToBulkDeleteBody() (string, error) { |
| 22 | return url.QueryEscape(strings.Join(opts, "\n")), nil |
| 23 | } |
| 24 | |
| 25 | // Delete will delete objects or containers in bulk. |
| 26 | func Delete(c *gophercloud.ServiceClient, opts DeleteOptsBuilder) DeleteResult { |
| 27 | var res DeleteResult |
| 28 | |
| 29 | if opts == nil { |
| 30 | return res |
| 31 | } |
| 32 | |
| 33 | reqString, err := opts.ToBulkDeleteBody() |
| 34 | if err != nil { |
| 35 | res.Err = err |
| 36 | return res |
| 37 | } |
| 38 | |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 39 | reqBody := strings.NewReader(reqString) |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 40 | |
| 41 | resp, err := perigee.Request("DELETE", deleteURL(c), perigee.Options{ |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 42 | ContentType: "text/plain", |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 43 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 44 | OkCodes: []int{200}, |
| 45 | ReqBody: reqBody, |
| 46 | Results: &res.Body, |
| 47 | }) |
| 48 | res.Header = resp.HttpResponse.Header |
| 49 | res.Err = err |
| 50 | return res |
| 51 | } |