Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 4 | "fmt" |
Jon Perritt | 884e031 | 2014-08-14 17:25:38 -0500 | [diff] [blame] | 5 | "io" |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 6 | "io/ioutil" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 7 | "strings" |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 8 | |
| 9 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 12 | // Object is a structure that holds information related to a storage object. |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 13 | type Object map[string]interface{} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 14 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 15 | // ListOpts is a structure that holds parameters for listing objects. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 16 | type ListOpts struct { |
| 17 | Container string |
| 18 | Full bool |
| 19 | Params map[string]string |
| 20 | } |
| 21 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 22 | // DownloadOpts is a structure that holds parameters for downloading an object. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 23 | type DownloadOpts struct { |
| 24 | Container string |
| 25 | Name string |
| 26 | Headers map[string]string |
| 27 | Params map[string]string |
| 28 | } |
| 29 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 30 | // CreateOpts is a structure that holds parameters for creating an object. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 31 | type CreateOpts struct { |
| 32 | Container string |
| 33 | Name string |
Jon Perritt | 884e031 | 2014-08-14 17:25:38 -0500 | [diff] [blame] | 34 | Content io.Reader |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 35 | Metadata map[string]string |
| 36 | Headers map[string]string |
| 37 | Params map[string]string |
| 38 | } |
| 39 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 40 | // CopyOpts is a structure that holds parameters for copying one object to another. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 41 | type 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 Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 50 | // DeleteOpts is a structure that holds parameters for deleting an object. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 51 | type DeleteOpts struct { |
| 52 | Container string |
| 53 | Name string |
| 54 | Params map[string]string |
| 55 | } |
| 56 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 57 | // GetOpts is a structure that holds parameters for getting an object's metadata. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 58 | type GetOpts struct { |
| 59 | Container string |
| 60 | Name string |
| 61 | Headers map[string]string |
| 62 | Params map[string]string |
| 63 | } |
| 64 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 65 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting an |
| 66 | // object's metadata. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 67 | type UpdateOpts struct { |
| 68 | Container string |
| 69 | Name string |
| 70 | Metadata map[string]string |
| 71 | Headers map[string]string |
| 72 | } |
| 73 | |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 74 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
| 75 | func 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 Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 80 | } |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 81 | return results, nil |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Ash Wilson | ca6f756 | 2014-09-16 15:43:54 -0400 | [diff] [blame] | 84 | // ExtractNames is a function that takes a page of objects and returns only their names. |
| 85 | func 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 Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 114 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 117 | // ExtractContent is a function that takes a DownloadResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 118 | // and returns the object's content. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 119 | func 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 126 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 127 | // and returns the custom metadata associated with the object. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 128 | func ExtractMetadata(gr GetResult) map[string]string { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 129 | metadata := make(map[string]string) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 130 | for k, v := range gr.Header { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 131 | 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 | } |