Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 5 | "net/http" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 6 | "strings" |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 7 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | b346140 | 2014-10-09 21:36:17 -0500 | [diff] [blame] | 10 | |
| 11 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 14 | // Container represents a container resource. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 15 | type Container struct { |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 16 | // The total number of bytes stored in the container. |
| 17 | Bytes int `json:"bytes" mapstructure:"bytes"` |
| 18 | |
| 19 | // The total number of objects stored in the container. |
| 20 | Count int `json:"count" mapstructure:"count"` |
| 21 | |
| 22 | // The name of the container. |
| 23 | Name string `json:"name" mapstructure:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 24 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 25 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 26 | // ContainerPage is the page returned by a pager when traversing over a |
| 27 | // collection of containers. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 28 | type ContainerPage struct { |
| 29 | pagination.MarkerPageBase |
| 30 | } |
| 31 | |
| 32 | // IsEmpty returns true if a ListResult contains no container names. |
| 33 | func (r ContainerPage) IsEmpty() (bool, error) { |
| 34 | names, err := ExtractNames(r) |
| 35 | if err != nil { |
| 36 | return true, err |
| 37 | } |
| 38 | return len(names) == 0, nil |
| 39 | } |
| 40 | |
| 41 | // LastMarker returns the last container name in a ListResult. |
| 42 | func (r ContainerPage) LastMarker() (string, error) { |
| 43 | names, err := ExtractNames(r) |
| 44 | if err != nil { |
| 45 | return "", err |
| 46 | } |
| 47 | if len(names) == 0 { |
| 48 | return "", nil |
| 49 | } |
| 50 | return names[len(names)-1], nil |
| 51 | } |
| 52 | |
| 53 | // ExtractInfo is a function that takes a ListResult and returns the containers' information. |
| 54 | func ExtractInfo(page pagination.Page) ([]Container, error) { |
| 55 | untyped := page.(ContainerPage).Body.([]interface{}) |
| 56 | results := make([]Container, len(untyped)) |
| 57 | for index, each := range untyped { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 58 | container := each.(map[string]interface{}) |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 59 | err := mapstructure.Decode(container, &results[index]) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 60 | if err != nil { |
| 61 | return results, err |
| 62 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 63 | } |
| 64 | return results, nil |
| 65 | } |
| 66 | |
| 67 | // ExtractNames is a function that takes a ListResult and returns the containers' names. |
| 68 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 69 | casted := page.(ContainerPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 70 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 71 | |
| 72 | switch { |
| 73 | case strings.HasPrefix(ct, "application/json"): |
| 74 | parsed, err := ExtractInfo(page) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | |
| 79 | names := make([]string, 0, len(parsed)) |
| 80 | for _, container := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 81 | names = append(names, container.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 82 | } |
| 83 | return names, nil |
| 84 | case strings.HasPrefix(ct, "text/plain"): |
| 85 | names := make([]string, 0, 50) |
| 86 | |
| 87 | body := string(page.(ContainerPage).Body.([]uint8)) |
| 88 | for _, name := range strings.Split(body, "\n") { |
| 89 | if len(name) > 0 { |
| 90 | names = append(names, name) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return names, nil |
| 95 | default: |
| 96 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 97 | } |
| 98 | } |
| 99 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 100 | // GetResult represents the result of a get operation. |
| 101 | type GetResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 102 | gophercloud.Result |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 105 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 106 | // and returns the custom metadata associated with the container. |
| 107 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 108 | if gr.Err != nil { |
| 109 | return nil, gr.Err |
| 110 | } |
| 111 | metadata := make(map[string]string) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 112 | for k, v := range gr.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 113 | if strings.HasPrefix(k, "X-Container-Meta-") { |
| 114 | key := strings.TrimPrefix(k, "X-Container-Meta-") |
| 115 | metadata[key] = v[0] |
| 116 | } |
| 117 | } |
| 118 | return metadata, nil |
| 119 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 120 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 121 | type headerResult struct { |
| 122 | gophercloud.Result |
| 123 | } |
| 124 | |
| 125 | // Extract pulls the unmodified headers from a Create, Update, or Delete result. |
| 126 | func (result headerResult) Extract() (http.Header, error) { |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 127 | return result.Header, result.Err |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 130 | // CreateResult represents the result of a create operation. To extract the |
| 131 | // the headers from the HTTP response, you can invoke the 'ExtractHeaders' |
| 132 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 133 | type CreateResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 134 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 137 | // UpdateResult represents the result of an update operation. To extract the |
| 138 | // the headers from the HTTP response, you can invoke the 'ExtractHeaders' |
| 139 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 140 | type UpdateResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 141 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 144 | // DeleteResult represents the result of a delete operation. To extract the |
| 145 | // the headers from the HTTP response, you can invoke the 'ExtractHeaders' |
| 146 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 147 | type DeleteResult struct { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 148 | headerResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 149 | } |