blob: bb9c4aca20dd19f8a43db8e3645f2786999dc1ef [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"
Jon Perritt816d2a02014-03-11 20:49:46 -050010)
11
Jon Perrittbef727e2014-05-12 22:41:55 -050012// ListResult is a *http.Response that is returned from a call to the List function.
Jon Perritteb575642014-04-24 15:16:31 -050013type ListResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050014
15// DownloadResult is a *http.Response that is returned from a call to the Download function.
Jon Perritteb575642014-04-24 15:16:31 -050016type DownloadResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050017
18// GetResult is a *http.Response that is returned from a call to the Get function.
Jon Perritteb575642014-04-24 15:16:31 -050019type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050020
21// List is a function that retrieves all objects in a container. It also returns the details
22// for the container. To extract only the object information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050023// response to the ExtractInfo or ExtractNames function, respectively.
Ash Wilson604320e2014-09-10 16:02:28 -040024func List(c *gophercloud.ServiceClient, opts ListOpts) (ListResult, error) {
Jon Perritt816d2a02014-03-11 20:49:46 -050025 contentType := ""
26
Ash Wilson604320e2014-09-10 16:02:28 -040027 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050028
29 query := utils.BuildQuery(opts.Params)
30
31 if !opts.Full {
32 contentType = "text/plain"
33 }
34
35 url := c.GetContainerURL(opts.Container) + query
36 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050037 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050038 Accept: contentType,
39 })
Jon Perritteb575642014-04-24 15:16:31 -050040 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050041}
42
43// Download is a function that retrieves the content and metadata for an object.
Jon Perritteb575642014-04-24 15:16:31 -050044// To extract just the content, pass the DownloadResult response to the ExtractContent
Jon Perritt816d2a02014-03-11 20:49:46 -050045// function.
Ash Wilson604320e2014-09-10 16:02:28 -040046func Download(c *gophercloud.ServiceClient, opts DownloadOpts) (DownloadResult, error) {
47 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050048
49 for k, v := range opts.Headers {
50 h[k] = v
51 }
52
53 query := utils.BuildQuery(opts.Params)
54
55 url := c.GetObjectURL(opts.Container, opts.Name) + query
56 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050057 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050058 })
Jon Perritteb575642014-04-24 15:16:31 -050059 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050060}
61
62// Create is a function that creates a new object or replaces an existing object.
Ash Wilson604320e2014-09-10 16:02:28 -040063func Create(c *gophercloud.ServiceClient, opts CreateOpts) error {
Jon Perritt816d2a02014-03-11 20:49:46 -050064 var reqBody []byte
65
Ash Wilson604320e2014-09-10 16:02:28 -040066 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050067
68 for k, v := range opts.Headers {
69 h[k] = v
70 }
71
72 for k, v := range opts.Metadata {
73 h["X-Object-Meta-"+k] = v
74 }
75
76 query := utils.BuildQuery(opts.Params)
77
78 content := opts.Content
79 if content != nil {
Jon Perritt884e0312014-08-14 17:25:38 -050080 reqBody = make([]byte, 0)
Ash Wilson604320e2014-09-10 16:02:28 -040081 _, err := content.Read(reqBody)
Jon Perritt816d2a02014-03-11 20:49:46 -050082 if err != nil {
83 return err
84 }
85 }
86
87 url := c.GetObjectURL(opts.Container, opts.Name) + query
88 _, err = perigee.Request("PUT", url, perigee.Options{
89 ReqBody: reqBody,
90 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050091 })
92 return err
93}
94
95// Copy is a function that copies one object to another.
Ash Wilson604320e2014-09-10 16:02:28 -040096func Copy(c *gophercloud.ServiceClient, opts CopyOpts) error {
97 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050098
99 for k, v := range opts.Metadata {
100 h["X-Object-Meta-"+k] = v
101 }
102
103 h["Destination"] = fmt.Sprintf("/%s/%s", opts.NewContainer, opts.NewName)
104
105 url := c.GetObjectURL(opts.Container, opts.Name)
106 _, err = perigee.Request("COPY", url, perigee.Options{
107 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500108 })
109 return err
110}
111
112// Delete is a function that deletes an object.
Ash Wilson604320e2014-09-10 16:02:28 -0400113func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
114 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500115
116 query := utils.BuildQuery(opts.Params)
117
118 url := c.GetObjectURL(opts.Container, opts.Name) + query
119 _, err = perigee.Request("DELETE", url, perigee.Options{
120 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500121 })
122 return err
123}
124
125// Get is a function that retrieves the metadata of an object. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500126// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400127func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
128 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500129
130 for k, v := range opts.Headers {
131 h[k] = v
132 }
133
Ash Wilson604320e2014-09-10 16:02:28 -0400134 url := getObjectURL(c, opts.Container, opts.Name)
Jon Perritt816d2a02014-03-11 20:49:46 -0500135 resp, err := perigee.Request("HEAD", url, perigee.Options{
136 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -0400137 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500138 })
Jon Perritteb575642014-04-24 15:16:31 -0500139 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500140}
141
142// Update is a function that creates, updates, or deletes an object's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -0400143func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
144 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500145
146 for k, v := range opts.Headers {
147 h[k] = v
148 }
149
150 for k, v := range opts.Metadata {
151 h["X-Object-Meta-"+k] = v
152 }
153
154 url := c.GetObjectURL(opts.Container, opts.Name)
155 _, err = perigee.Request("POST", url, perigee.Options{
156 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500157 })
158 return err
159}