blob: 67aba34cb535ba4f4f9ecdcaab90e80043b36383 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
4 "fmt"
Jon Perritteb575642014-04-24 15:16:31 -05005 "net/http"
Ash Wilson604320e2014-09-10 16:02:28 -04006
7 "github.com/racker/perigee"
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilsonca6f7562014-09-16 15:43:54 -040010 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050011)
12
Ash Wilsonca6f7562014-09-16 15:43:54 -040013// ListResult is a single page of objects that is returned from a call to the List function.
14type ListResult struct {
15 pagination.MarkerPageBase
16}
17
18// IsEmpty returns true if a ListResult contains no object names.
19func (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.
28func (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 Perrittbef727e2014-05-12 22:41:55 -050038
39// DownloadResult is a *http.Response that is returned from a call to the Download function.
Jon Perritteb575642014-04-24 15:16:31 -050040type DownloadResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050041
42// GetResult is a *http.Response that is returned from a call to the Get function.
Jon Perritteb575642014-04-24 15:16:31 -050043type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050044
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 Perritteb575642014-04-24 15:16:31 -050047// response to the ExtractInfo or ExtractNames function, respectively.
Ash Wilsonca6f7562014-09-16 15:43:54 -040048func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
49 var headers map[string]string
Jon Perritt816d2a02014-03-11 20:49:46 -050050
51 query := utils.BuildQuery(opts.Params)
52
53 if !opts.Full {
Ash Wilsonca6f7562014-09-16 15:43:54 -040054 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 Perritt816d2a02014-03-11 20:49:46 -050061 }
62
Ash Wilsone47ea9e2014-09-10 16:03:44 -040063 url := getContainerURL(c, opts.Container) + query
Ash Wilsonca6f7562014-09-16 15:43:54 -040064 pager := pagination.NewPager(c, url, createPage)
65 pager.Headers = headers
66 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050067}
68
69// Download is a function that retrieves the content and metadata for an object.
Jon Perritteb575642014-04-24 15:16:31 -050070// To extract just the content, pass the DownloadResult response to the ExtractContent
Jon Perritt816d2a02014-03-11 20:49:46 -050071// function.
Ash Wilson604320e2014-09-10 16:02:28 -040072func Download(c *gophercloud.ServiceClient, opts DownloadOpts) (DownloadResult, error) {
73 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050074
75 for k, v := range opts.Headers {
76 h[k] = v
77 }
78
79 query := utils.BuildQuery(opts.Params)
80
Ash Wilsone47ea9e2014-09-10 16:03:44 -040081 url := getObjectURL(c, opts.Container, opts.Name) + query
Jon Perritt816d2a02014-03-11 20:49:46 -050082 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050083 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040084 OkCodes: []int{200},
Jon Perritt816d2a02014-03-11 20:49:46 -050085 })
Jon Perritteb575642014-04-24 15:16:31 -050086 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050087}
88
89// Create is a function that creates a new object or replaces an existing object.
Ash Wilson604320e2014-09-10 16:02:28 -040090func Create(c *gophercloud.ServiceClient, opts CreateOpts) error {
Jon Perritt816d2a02014-03-11 20:49:46 -050091 var reqBody []byte
92
Ash Wilson604320e2014-09-10 16:02:28 -040093 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050094
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 Perritt884e0312014-08-14 17:25:38 -0500107 reqBody = make([]byte, 0)
Ash Wilson604320e2014-09-10 16:02:28 -0400108 _, err := content.Read(reqBody)
Jon Perritt816d2a02014-03-11 20:49:46 -0500109 if err != nil {
110 return err
111 }
112 }
113
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400114 url := getObjectURL(c, opts.Container, opts.Name) + query
115 _, err := perigee.Request("PUT", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500116 ReqBody: reqBody,
117 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400118 OkCodes: []int{201},
Jon Perritt816d2a02014-03-11 20:49:46 -0500119 })
120 return err
121}
122
123// Copy is a function that copies one object to another.
Ash Wilson604320e2014-09-10 16:02:28 -0400124func Copy(c *gophercloud.ServiceClient, opts CopyOpts) error {
125 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500126
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 Wilsone47ea9e2014-09-10 16:03:44 -0400133 url := getObjectURL(c, opts.Container, opts.Name)
134 _, err := perigee.Request("COPY", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500135 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400136 OkCodes: []int{201},
Jon Perritt816d2a02014-03-11 20:49:46 -0500137 })
138 return err
139}
140
141// Delete is a function that deletes an object.
Ash Wilson604320e2014-09-10 16:02:28 -0400142func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
143 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500144
145 query := utils.BuildQuery(opts.Params)
146
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400147 url := getObjectURL(c, opts.Container, opts.Name) + query
148 _, err := perigee.Request("DELETE", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500149 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400150 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500151 })
152 return err
153}
154
155// Get is a function that retrieves the metadata of an object. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500156// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400157func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
158 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500159
160 for k, v := range opts.Headers {
161 h[k] = v
162 }
163
Ash Wilson604320e2014-09-10 16:02:28 -0400164 url := getObjectURL(c, opts.Container, opts.Name)
Jon Perritt816d2a02014-03-11 20:49:46 -0500165 resp, err := perigee.Request("HEAD", url, perigee.Options{
166 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -0400167 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500168 })
Jon Perritteb575642014-04-24 15:16:31 -0500169 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500170}
171
172// Update is a function that creates, updates, or deletes an object's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -0400173func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
174 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500175
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 Wilsone47ea9e2014-09-10 16:03:44 -0400184 url := getObjectURL(c, opts.Container, opts.Name)
185 _, err := perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500186 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400187 OkCodes: []int{202},
Jon Perritt816d2a02014-03-11 20:49:46 -0500188 })
189 return err
190}