blob: 4cf3d2d50e7c14d29adbc92127f0f96f5b68113f [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
61 Headers map[string]string
62 Params map[string]string
63}
64
Jon Perrittbef727e2014-05-12 22:41:55 -050065// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
66// object's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050067type UpdateOpts struct {
68 Container string
69 Name string
70 Metadata map[string]string
71 Headers map[string]string
72}
73
Ash Wilsonca6f7562014-09-16 15:43:54 -040074// ExtractInfo is a function that takes a page of objects and returns their full information.
75func ExtractInfo(page pagination.Page) ([]Object, error) {
76 untyped := page.(ListResult).Body.([]interface{})
77 results := make([]Object, len(untyped))
78 for index, each := range untyped {
79 results[index] = Object(each.(map[string]interface{}))
Jon Perritteb575642014-04-24 15:16:31 -050080 }
Ash Wilsonca6f7562014-09-16 15:43:54 -040081 return results, nil
Jon Perritt816d2a02014-03-11 20:49:46 -050082}
83
Ash Wilsonca6f7562014-09-16 15:43:54 -040084// ExtractNames is a function that takes a page of objects and returns only their names.
85func ExtractNames(page pagination.Page) ([]string, error) {
86 casted := page.(ListResult)
87 ct := casted.Header.Get("Content-Type")
88
89 switch {
90 case strings.HasPrefix(ct, "application/json"):
91 parsed, err := ExtractInfo(page)
92 if err != nil {
93 return nil, err
94 }
95
96 names := make([]string, 0, len(parsed))
97 for _, object := range parsed {
98 names = append(names, object["name"].(string))
99 }
100 return names, nil
101 case strings.HasPrefix(ct, "text/plain"):
102 names := make([]string, 0, 50)
103
104 body := string(page.(ListResult).Body.([]uint8))
105 for _, name := range strings.Split(body, "\n") {
106 if len(name) > 0 {
107 names = append(names, name)
108 }
109 }
110
111 return names, nil
112 default:
113 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
Jon Perritteb575642014-04-24 15:16:31 -0500114 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500115}
116
Jon Perritteb575642014-04-24 15:16:31 -0500117// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -0500118// and returns the object's content.
Jon Perritteb575642014-04-24 15:16:31 -0500119func ExtractContent(dr DownloadResult) ([]byte, error) {
120 var body []byte
121 defer dr.Body.Close()
122 body, err := ioutil.ReadAll(dr.Body)
123 return body, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500124}
125
Jon Perritteb575642014-04-24 15:16:31 -0500126// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -0500127// and returns the custom metadata associated with the object.
Jon Perritteb575642014-04-24 15:16:31 -0500128func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -0500129 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -0500130 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -0500131 if strings.HasPrefix(k, "X-Object-Meta-") {
132 key := strings.TrimPrefix(k, "X-Object-Meta-")
133 metadata[key] = v[0]
134 }
135 }
136 return metadata
137}