blob: 4e6f23a56e734ace6c44243a4516b045319965f4 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
4 "fmt"
Jon Perritt816d2a02014-03-11 20:49:46 -05005 "github.com/racker/perigee"
Jon Perrittb6b1d022014-04-14 21:50:45 -05006 storage "github.com/rackspace/gophercloud/openstack/storage/v1"
Jon Perritt816d2a02014-03-11 20:49:46 -05007 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perritteb575642014-04-24 15:16:31 -05008 "net/http"
Jon Perritt816d2a02014-03-11 20:49:46 -05009)
10
Jon Perrittbef727e2014-05-12 22:41:55 -050011// ListResult is a *http.Response that is returned from a call to the List function.
Jon Perritteb575642014-04-24 15:16:31 -050012type ListResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050013
14// DownloadResult is a *http.Response that is returned from a call to the Download function.
Jon Perritteb575642014-04-24 15:16:31 -050015type DownloadResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050016
17// GetResult is a *http.Response that is returned from a call to the Get function.
Jon Perritteb575642014-04-24 15:16:31 -050018type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050019
20// List is a function that retrieves all objects in a container. It also returns the details
21// for the container. To extract only the object information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050022// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritt816d2a02014-03-11 20:49:46 -050023func List(c *storage.Client, opts ListOpts) (ListResult, error) {
24 contentType := ""
25
26 h, err := c.GetHeaders()
27 if err != nil {
28 return nil, err
29 }
30
31 query := utils.BuildQuery(opts.Params)
32
33 if !opts.Full {
34 contentType = "text/plain"
35 }
36
37 url := c.GetContainerURL(opts.Container) + query
38 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050039 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050040 Accept: contentType,
41 })
Jon Perritteb575642014-04-24 15:16:31 -050042 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050043}
44
45// Download is a function that retrieves the content and metadata for an object.
Jon Perritteb575642014-04-24 15:16:31 -050046// To extract just the content, pass the DownloadResult response to the ExtractContent
Jon Perritt816d2a02014-03-11 20:49:46 -050047// function.
48func Download(c *storage.Client, opts DownloadOpts) (DownloadResult, error) {
49 h, err := c.GetHeaders()
50 if err != nil {
51 return nil, err
52 }
53
54 for k, v := range opts.Headers {
55 h[k] = v
56 }
57
58 query := utils.BuildQuery(opts.Params)
59
60 url := c.GetObjectURL(opts.Container, opts.Name) + query
61 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050062 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050063 })
Jon Perritteb575642014-04-24 15:16:31 -050064 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050065}
66
67// Create is a function that creates a new object or replaces an existing object.
68func Create(c *storage.Client, opts CreateOpts) error {
69 var reqBody []byte
70
71 h, err := c.GetHeaders()
72 if err != nil {
73 return err
74 }
75
76 for k, v := range opts.Headers {
77 h[k] = v
78 }
79
80 for k, v := range opts.Metadata {
81 h["X-Object-Meta-"+k] = v
82 }
83
84 query := utils.BuildQuery(opts.Params)
85
86 content := opts.Content
87 if content != nil {
Jon Perritt884e0312014-08-14 17:25:38 -050088 reqBody = make([]byte, 0)
Jon Perritt816d2a02014-03-11 20:49:46 -050089 _, err = content.Read(reqBody)
90 if err != nil {
91 return err
92 }
93 }
94
95 url := c.GetObjectURL(opts.Container, opts.Name) + query
96 _, err = perigee.Request("PUT", url, perigee.Options{
97 ReqBody: reqBody,
98 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050099 })
100 return err
101}
102
103// Copy is a function that copies one object to another.
104func Copy(c *storage.Client, opts CopyOpts) error {
105 h, err := c.GetHeaders()
106 if err != nil {
107 return err
108 }
109
110 for k, v := range opts.Metadata {
111 h["X-Object-Meta-"+k] = v
112 }
113
114 h["Destination"] = fmt.Sprintf("/%s/%s", opts.NewContainer, opts.NewName)
115
116 url := c.GetObjectURL(opts.Container, opts.Name)
117 _, err = perigee.Request("COPY", url, perigee.Options{
118 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500119 })
120 return err
121}
122
123// Delete is a function that deletes an object.
124func Delete(c *storage.Client, opts DeleteOpts) error {
125 h, err := c.GetHeaders()
126 if err != nil {
127 return err
128 }
129
130 query := utils.BuildQuery(opts.Params)
131
132 url := c.GetObjectURL(opts.Container, opts.Name) + query
133 _, err = perigee.Request("DELETE", url, perigee.Options{
134 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500135 })
136 return err
137}
138
139// Get is a function that retrieves the metadata of an object. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500140// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt816d2a02014-03-11 20:49:46 -0500141func 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,
Jon Perritt816d2a02014-03-11 20:49:46 -0500154 })
Jon Perritteb575642014-04-24 15:16:31 -0500155 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500156}
157
158// Update is a function that creates, updates, or deletes an object's metadata.
159func Update(c *storage.Client, opts UpdateOpts) error {
160 h, err := c.GetHeaders()
161 if err != nil {
162 return err
163 }
164
165 for k, v := range opts.Headers {
166 h[k] = v
167 }
168
169 for k, v := range opts.Metadata {
170 h["X-Object-Meta-"+k] = v
171 }
172
173 url := c.GetObjectURL(opts.Container, opts.Name)
174 _, err = perigee.Request("POST", url, perigee.Options{
175 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500176 })
177 return err
178}