Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 4 | "fmt" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 5 | "strings" |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 6 | |
| 7 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 10 | // Container is a structure that holds information related to a storage container. |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 11 | type Container map[string]interface{} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 12 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 13 | // ListOpts is a structure that holds parameters for listing containers. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 14 | type ListOpts struct { |
| 15 | Full bool |
| 16 | Params map[string]string |
| 17 | } |
| 18 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 19 | // CreateOpts is a structure that holds parameters for creating a container. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 20 | type CreateOpts struct { |
| 21 | Name string |
| 22 | Metadata map[string]string |
| 23 | Headers map[string]string |
| 24 | } |
| 25 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 26 | // DeleteOpts is a structure that holds parameters for deleting a container. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 27 | type DeleteOpts struct { |
| 28 | Name string |
| 29 | Params map[string]string |
| 30 | } |
| 31 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 32 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting a |
| 33 | // container's metadata. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 34 | type UpdateOpts struct { |
| 35 | Name string |
| 36 | Metadata map[string]string |
| 37 | Headers map[string]string |
| 38 | } |
| 39 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 40 | // GetOpts is a structure that holds parameters for getting a container's metadata. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 41 | type GetOpts struct { |
| 42 | Name string |
| 43 | Metadata map[string]string |
| 44 | } |
| 45 | |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 46 | // ExtractInfo is a function that takes a ListResult and returns the containers' information. |
| 47 | func ExtractInfo(page pagination.Page) ([]Container, error) { |
| 48 | untyped := page.(ListResult).Body.([]interface{}) |
| 49 | results := make([]Container, len(untyped)) |
| 50 | for index, each := range untyped { |
| 51 | results[index] = Container(each.(map[string]interface{})) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 52 | } |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 53 | return results, nil |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 56 | // ExtractNames is a function that takes a ListResult and returns the containers' names. |
| 57 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 58 | casted := page.(ListResult) |
| 59 | ct := casted.Header.Get("Content-Type") |
| 60 | |
| 61 | switch { |
| 62 | case strings.HasPrefix(ct, "application/json"): |
| 63 | parsed, err := ExtractInfo(page) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | names := make([]string, 0, len(parsed)) |
| 69 | for _, container := range parsed { |
| 70 | names = append(names, container["name"].(string)) |
| 71 | } |
| 72 | return names, nil |
| 73 | case strings.HasPrefix(ct, "text/plain"): |
| 74 | names := make([]string, 0, 50) |
| 75 | |
| 76 | body := string(page.(ListResult).Body.([]uint8)) |
| 77 | for _, name := range strings.Split(body, "\n") { |
| 78 | if len(name) > 0 { |
| 79 | names = append(names, name) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return names, nil |
| 84 | default: |
| 85 | 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] | 86 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 89 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 90 | // and returns the custom metadata associated with the container. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 91 | func ExtractMetadata(gr GetResult) map[string]string { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 92 | metadata := make(map[string]string) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 93 | for k, v := range gr.Header { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 94 | if strings.HasPrefix(k, "X-Container-Meta-") { |
| 95 | key := strings.TrimPrefix(k, "X-Container-Meta-") |
| 96 | metadata[key] = v[0] |
| 97 | } |
| 98 | } |
| 99 | return metadata |
| 100 | } |