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