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