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