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 | |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
| 9 | |
| 10 | // DeleteOptsBuilder allows extensions to add additional parameters to the |
| 11 | // Delete request. |
| 12 | type DeleteOptsBuilder interface { |
| 13 | ToBulkDeleteBody() (string, error) |
| 14 | } |
| 15 | |
| 16 | // DeleteOpts is a structure that holds parameters for deleting an object. |
| 17 | type DeleteOpts []string |
| 18 | |
| 19 | // ToBulkDeleteBody formats a DeleteOpts into a request body. |
| 20 | func (opts DeleteOpts) ToBulkDeleteBody() (string, error) { |
| 21 | return url.QueryEscape(strings.Join(opts, "\n")), nil |
| 22 | } |
| 23 | |
| 24 | // Delete will delete objects or containers in bulk. |
| 25 | func Delete(c *gophercloud.ServiceClient, opts DeleteOptsBuilder) DeleteResult { |
| 26 | var res DeleteResult |
| 27 | |
| 28 | if opts == nil { |
| 29 | return res |
| 30 | } |
| 31 | |
| 32 | reqString, err := opts.ToBulkDeleteBody() |
| 33 | if err != nil { |
| 34 | res.Err = err |
| 35 | return res |
| 36 | } |
| 37 | |
Jon Perritt | 260e088 | 2014-10-20 23:31:23 -0500 | [diff] [blame] | 38 | reqBody := strings.NewReader(reqString) |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 39 | |
Ash Wilson | 2199f10 | 2015-02-12 16:16:09 -0500 | [diff] [blame^] | 40 | resp, err := c.Request("DELETE", deleteURL(c), gophercloud.RequestOpts{ |
| 41 | MoreHeaders: map[string]string{"Content-Type": "text/plain"}, |
| 42 | OkCodes: []int{200}, |
| 43 | JSONBody: reqBody, |
| 44 | JSONResponse: &res.Body, |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 45 | }) |
Ash Wilson | 2199f10 | 2015-02-12 16:16:09 -0500 | [diff] [blame^] | 46 | res.Header = resp.Header |
Jon Perritt | 0ce24ad | 2014-10-20 21:59:45 -0500 | [diff] [blame] | 47 | res.Err = err |
| 48 | return res |
| 49 | } |