blob: 931653e4183adc8e374483efb81e1b5baa212800 [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
Ash Wilsone47ea9e2014-09-10 16:03:44 -040035 url := getContainerURL(c, opts.Container) + query
Jon Perritt816d2a02014-03-11 20:49:46 -050036 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,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040039 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050040 })
Jon Perritteb575642014-04-24 15:16:31 -050041 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050042}
43
44// Download is a function that retrieves the content and metadata for an object.
Jon Perritteb575642014-04-24 15:16:31 -050045// To extract just the content, pass the DownloadResult response to the ExtractContent
Jon Perritt816d2a02014-03-11 20:49:46 -050046// function.
Ash Wilson604320e2014-09-10 16:02:28 -040047func Download(c *gophercloud.ServiceClient, opts DownloadOpts) (DownloadResult, error) {
48 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050049
50 for k, v := range opts.Headers {
51 h[k] = v
52 }
53
54 query := utils.BuildQuery(opts.Params)
55
Ash Wilsone47ea9e2014-09-10 16:03:44 -040056 url := getObjectURL(c, opts.Container, opts.Name) + query
Jon Perritt816d2a02014-03-11 20:49:46 -050057 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050058 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040059 OkCodes: []int{200},
Jon Perritt816d2a02014-03-11 20:49:46 -050060 })
Jon Perritteb575642014-04-24 15:16:31 -050061 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050062}
63
64// Create is a function that creates a new object or replaces an existing object.
Ash Wilson604320e2014-09-10 16:02:28 -040065func Create(c *gophercloud.ServiceClient, opts CreateOpts) error {
Jon Perritt816d2a02014-03-11 20:49:46 -050066 var reqBody []byte
67
Ash Wilson604320e2014-09-10 16:02:28 -040068 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050069
70 for k, v := range opts.Headers {
71 h[k] = v
72 }
73
74 for k, v := range opts.Metadata {
75 h["X-Object-Meta-"+k] = v
76 }
77
78 query := utils.BuildQuery(opts.Params)
79
80 content := opts.Content
81 if content != nil {
Jon Perritt884e0312014-08-14 17:25:38 -050082 reqBody = make([]byte, 0)
Ash Wilson604320e2014-09-10 16:02:28 -040083 _, err := content.Read(reqBody)
Jon Perritt816d2a02014-03-11 20:49:46 -050084 if err != nil {
85 return err
86 }
87 }
88
Ash Wilsone47ea9e2014-09-10 16:03:44 -040089 url := getObjectURL(c, opts.Container, opts.Name) + query
90 _, err := perigee.Request("PUT", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050091 ReqBody: reqBody,
92 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040093 OkCodes: []int{201},
Jon Perritt816d2a02014-03-11 20:49:46 -050094 })
95 return err
96}
97
98// Copy is a function that copies one object to another.
Ash Wilson604320e2014-09-10 16:02:28 -040099func Copy(c *gophercloud.ServiceClient, opts CopyOpts) error {
100 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500101
102 for k, v := range opts.Metadata {
103 h["X-Object-Meta-"+k] = v
104 }
105
106 h["Destination"] = fmt.Sprintf("/%s/%s", opts.NewContainer, opts.NewName)
107
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400108 url := getObjectURL(c, opts.Container, opts.Name)
109 _, err := perigee.Request("COPY", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500110 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400111 OkCodes: []int{201},
Jon Perritt816d2a02014-03-11 20:49:46 -0500112 })
113 return err
114}
115
116// Delete is a function that deletes an object.
Ash Wilson604320e2014-09-10 16:02:28 -0400117func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
118 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500119
120 query := utils.BuildQuery(opts.Params)
121
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400122 url := getObjectURL(c, opts.Container, opts.Name) + query
123 _, err := perigee.Request("DELETE", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500124 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400125 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500126 })
127 return err
128}
129
130// Get is a function that retrieves the metadata of an object. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500131// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400132func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
133 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500134
135 for k, v := range opts.Headers {
136 h[k] = v
137 }
138
Ash Wilson604320e2014-09-10 16:02:28 -0400139 url := getObjectURL(c, opts.Container, opts.Name)
Jon Perritt816d2a02014-03-11 20:49:46 -0500140 resp, err := perigee.Request("HEAD", url, perigee.Options{
141 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -0400142 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500143 })
Jon Perritteb575642014-04-24 15:16:31 -0500144 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500145}
146
147// Update is a function that creates, updates, or deletes an object's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -0400148func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
149 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500150
151 for k, v := range opts.Headers {
152 h[k] = v
153 }
154
155 for k, v := range opts.Metadata {
156 h["X-Object-Meta-"+k] = v
157 }
158
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400159 url := getObjectURL(c, opts.Container, opts.Name)
160 _, err := perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500161 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400162 OkCodes: []int{202},
Jon Perritt816d2a02014-03-11 20:49:46 -0500163 })
164 return err
165}