Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 5 | "github.com/racker/perigee" |
Jon Perritt | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame] | 6 | storage "github.com/rackspace/gophercloud/openstack/storage/v1" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/openstack/utils" |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 8 | "net/http" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 11 | type ListResult *http.Response |
| 12 | type DownloadResult *http.Response |
| 13 | type GetResult *http.Response |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 14 | |
| 15 | // List is a function that retrieves all objects in a container. It also returns the details |
| 16 | // for the container. To extract only the object information or names, pass the ListResult |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 17 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 18 | func List(c *storage.Client, opts ListOpts) (ListResult, error) { |
| 19 | contentType := "" |
| 20 | |
| 21 | h, err := c.GetHeaders() |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | |
| 26 | query := utils.BuildQuery(opts.Params) |
| 27 | |
| 28 | if !opts.Full { |
| 29 | contentType = "text/plain" |
| 30 | } |
| 31 | |
| 32 | url := c.GetContainerURL(opts.Container) + query |
| 33 | resp, err := perigee.Request("GET", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 34 | MoreHeaders: h, |
| 35 | OkCodes: []int{200, 204}, |
| 36 | Accept: contentType, |
| 37 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 38 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Download is a function that retrieves the content and metadata for an object. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 42 | // To extract just the content, pass the DownloadResult response to the ExtractContent |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 43 | // function. |
| 44 | func Download(c *storage.Client, opts DownloadOpts) (DownloadResult, error) { |
| 45 | h, err := c.GetHeaders() |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | for k, v := range opts.Headers { |
| 51 | h[k] = v |
| 52 | } |
| 53 | |
| 54 | query := utils.BuildQuery(opts.Params) |
| 55 | |
| 56 | url := c.GetObjectURL(opts.Container, opts.Name) + query |
| 57 | resp, err := perigee.Request("GET", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 58 | MoreHeaders: h, |
| 59 | OkCodes: []int{200}, |
| 60 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 61 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Create is a function that creates a new object or replaces an existing object. |
| 65 | func Create(c *storage.Client, opts CreateOpts) error { |
| 66 | var reqBody []byte |
| 67 | |
| 68 | h, err := c.GetHeaders() |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | for k, v := range opts.Headers { |
| 74 | h[k] = v |
| 75 | } |
| 76 | |
| 77 | for k, v := range opts.Metadata { |
| 78 | h["X-Object-Meta-"+k] = v |
| 79 | } |
| 80 | |
| 81 | query := utils.BuildQuery(opts.Params) |
| 82 | |
| 83 | content := opts.Content |
| 84 | if content != nil { |
| 85 | reqBody = make([]byte, content.Len()) |
| 86 | _, err = content.Read(reqBody) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | url := c.GetObjectURL(opts.Container, opts.Name) + query |
| 93 | _, err = perigee.Request("PUT", url, perigee.Options{ |
| 94 | ReqBody: reqBody, |
| 95 | MoreHeaders: h, |
| 96 | OkCodes: []int{201}, |
| 97 | }) |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | // Copy is a function that copies one object to another. |
| 102 | func Copy(c *storage.Client, opts CopyOpts) error { |
| 103 | h, err := c.GetHeaders() |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | for k, v := range opts.Metadata { |
| 109 | h["X-Object-Meta-"+k] = v |
| 110 | } |
| 111 | |
| 112 | h["Destination"] = fmt.Sprintf("/%s/%s", opts.NewContainer, opts.NewName) |
| 113 | |
| 114 | url := c.GetObjectURL(opts.Container, opts.Name) |
| 115 | _, err = perigee.Request("COPY", url, perigee.Options{ |
| 116 | MoreHeaders: h, |
| 117 | OkCodes: []int{201}, |
| 118 | }) |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | // Delete is a function that deletes an object. |
| 123 | func Delete(c *storage.Client, opts DeleteOpts) error { |
| 124 | h, err := c.GetHeaders() |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | query := utils.BuildQuery(opts.Params) |
| 130 | |
| 131 | url := c.GetObjectURL(opts.Container, opts.Name) + query |
| 132 | _, err = perigee.Request("DELETE", url, perigee.Options{ |
| 133 | MoreHeaders: h, |
| 134 | OkCodes: []int{204}, |
| 135 | }) |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | // Get is a function that retrieves the metadata of an object. To extract just the custom |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 140 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 141 | func Get(c *storage.Client, opts GetOpts) (GetResult, error) { |
| 142 | h, err := c.GetHeaders() |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | |
| 147 | for k, v := range opts.Headers { |
| 148 | h[k] = v |
| 149 | } |
| 150 | |
| 151 | url := c.GetObjectURL(opts.Container, opts.Name) |
| 152 | resp, err := perigee.Request("HEAD", url, perigee.Options{ |
| 153 | MoreHeaders: h, |
| 154 | OkCodes: []int{204}, |
| 155 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame^] | 156 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Update is a function that creates, updates, or deletes an object's metadata. |
| 160 | func Update(c *storage.Client, opts UpdateOpts) error { |
| 161 | h, err := c.GetHeaders() |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | for k, v := range opts.Headers { |
| 167 | h[k] = v |
| 168 | } |
| 169 | |
| 170 | for k, v := range opts.Metadata { |
| 171 | h["X-Object-Meta-"+k] = v |
| 172 | } |
| 173 | |
| 174 | url := c.GetObjectURL(opts.Container, opts.Name) |
| 175 | _, err = perigee.Request("POST", url, perigee.Options{ |
| 176 | MoreHeaders: h, |
| 177 | OkCodes: []int{202, 204}, |
| 178 | }) |
| 179 | return err |
| 180 | } |