blob: cd248c873f612e254c10992c8bcba5d2843a7382 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
Ash Wilsonca6f7562014-09-16 15:43:54 -04004 "fmt"
Jon Perritt884e0312014-08-14 17:25:38 -05005 "io"
Jon Perritteb575642014-04-24 15:16:31 -05006 "io/ioutil"
Jon Perritt816d2a02014-03-11 20:49:46 -05007 "strings"
Ash Wilsonca6f7562014-09-16 15:43:54 -04008
9 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050010)
11
Jon Perrittbef727e2014-05-12 22:41:55 -050012// Object is a structure that holds information related to a storage object.
Jon Perrittc19adea2014-04-15 16:56:01 -050013type Object map[string]interface{}
Jon Perritt816d2a02014-03-11 20:49:46 -050014
Jon Perrittbef727e2014-05-12 22:41:55 -050015// ListOpts is a structure that holds parameters for listing objects.
Jon Perritt816d2a02014-03-11 20:49:46 -050016type ListOpts struct {
17 Container string
18 Full bool
19 Params map[string]string
20}
21
Jon Perrittbef727e2014-05-12 22:41:55 -050022// DownloadOpts is a structure that holds parameters for downloading an object.
Jon Perritt816d2a02014-03-11 20:49:46 -050023type DownloadOpts struct {
24 Container string
25 Name string
26 Headers map[string]string
27 Params map[string]string
28}
29
Jon Perrittbef727e2014-05-12 22:41:55 -050030// CreateOpts is a structure that holds parameters for creating an object.
Jon Perritt816d2a02014-03-11 20:49:46 -050031type CreateOpts struct {
32 Container string
33 Name string
Jon Perritt884e0312014-08-14 17:25:38 -050034 Content io.Reader
Jon Perritt816d2a02014-03-11 20:49:46 -050035 Metadata map[string]string
36 Headers map[string]string
37 Params map[string]string
38}
39
Jon Perrittbef727e2014-05-12 22:41:55 -050040// CopyOpts is a structure that holds parameters for copying one object to another.
Jon Perritt816d2a02014-03-11 20:49:46 -050041type CopyOpts struct {
42 Container string
43 Name string
44 NewContainer string
45 NewName string
46 Metadata map[string]string
47 Headers map[string]string
48}
49
Jon Perrittbef727e2014-05-12 22:41:55 -050050// DeleteOpts is a structure that holds parameters for deleting an object.
Jon Perritt816d2a02014-03-11 20:49:46 -050051type DeleteOpts struct {
52 Container string
53 Name string
54 Params map[string]string
55}
56
Jon Perrittbef727e2014-05-12 22:41:55 -050057// GetOpts is a structure that holds parameters for getting an object's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050058type GetOpts struct {
59 Container string
60 Name string
Jon Perritt816d2a02014-03-11 20:49:46 -050061 Params map[string]string
62}
63
Jon Perrittbef727e2014-05-12 22:41:55 -050064// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
65// object's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050066type UpdateOpts struct {
67 Container string
68 Name string
69 Metadata map[string]string
70 Headers map[string]string
71}
72
Ash Wilsonca6f7562014-09-16 15:43:54 -040073// ExtractInfo is a function that takes a page of objects and returns their full information.
74func ExtractInfo(page pagination.Page) ([]Object, error) {
75 untyped := page.(ListResult).Body.([]interface{})
76 results := make([]Object, len(untyped))
77 for index, each := range untyped {
78 results[index] = Object(each.(map[string]interface{}))
Jon Perritteb575642014-04-24 15:16:31 -050079 }
Ash Wilsonca6f7562014-09-16 15:43:54 -040080 return results, nil
Jon Perritt816d2a02014-03-11 20:49:46 -050081}
82
Ash Wilsonca6f7562014-09-16 15:43:54 -040083// ExtractNames is a function that takes a page of objects and returns only their names.
84func ExtractNames(page pagination.Page) ([]string, error) {
85 casted := page.(ListResult)
86 ct := casted.Header.Get("Content-Type")
87
88 switch {
89 case strings.HasPrefix(ct, "application/json"):
90 parsed, err := ExtractInfo(page)
91 if err != nil {
92 return nil, err
93 }
94
95 names := make([]string, 0, len(parsed))
96 for _, object := range parsed {
97 names = append(names, object["name"].(string))
98 }
99 return names, nil
100 case strings.HasPrefix(ct, "text/plain"):
101 names := make([]string, 0, 50)
102
103 body := string(page.(ListResult).Body.([]uint8))
104 for _, name := range strings.Split(body, "\n") {
105 if len(name) > 0 {
106 names = append(names, name)
107 }
108 }
109
110 return names, nil
111 default:
112 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
Jon Perritteb575642014-04-24 15:16:31 -0500113 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500114}
115
Jon Perritteb575642014-04-24 15:16:31 -0500116// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -0500117// and returns the object's content.
Jon Perritteb575642014-04-24 15:16:31 -0500118func ExtractContent(dr DownloadResult) ([]byte, error) {
119 var body []byte
120 defer dr.Body.Close()
121 body, err := ioutil.ReadAll(dr.Body)
Jon Perrittcc10e312014-09-16 00:06:31 -0500122 if err != nil {
123 return body, fmt.Errorf("Error trying to read DownloadResult body: %v", err)
124 }
125 return body, nil
Jon Perritt816d2a02014-03-11 20:49:46 -0500126}
127
Jon Perritteb575642014-04-24 15:16:31 -0500128// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -0500129// and returns the custom metadata associated with the object.
Jon Perritteb575642014-04-24 15:16:31 -0500130func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -0500131 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -0500132 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -0500133 if strings.HasPrefix(k, "X-Object-Meta-") {
134 key := strings.TrimPrefix(k, "X-Object-Meta-")
135 metadata[key] = v[0]
136 }
137 }
138 return metadata
139}