Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 5 | "io/ioutil" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 6 | "strings" |
| 7 | ) |
| 8 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 9 | // Container is a structure that holds information related to a storage container. |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 10 | type Container map[string]interface{} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 11 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 12 | // ListOpts is a structure that holds parameters for listing containers. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 13 | type ListOpts struct { |
| 14 | Full bool |
| 15 | Params map[string]string |
| 16 | } |
| 17 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 18 | // CreateOpts is a structure that holds parameters for creating a container. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 19 | type CreateOpts struct { |
| 20 | Name string |
| 21 | Metadata map[string]string |
| 22 | Headers map[string]string |
| 23 | } |
| 24 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 25 | // DeleteOpts is a structure that holds parameters for deleting a container. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 26 | type DeleteOpts struct { |
| 27 | Name string |
| 28 | Params map[string]string |
| 29 | } |
| 30 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 31 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting a |
| 32 | // container's metadata. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 33 | type UpdateOpts struct { |
| 34 | Name string |
| 35 | Metadata map[string]string |
| 36 | Headers map[string]string |
| 37 | } |
| 38 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 39 | // 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] | 40 | type GetOpts struct { |
| 41 | Name string |
| 42 | Metadata map[string]string |
| 43 | } |
| 44 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 45 | // ExtractInfo is a function that takes a ListResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 46 | // and returns the containers' information. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 47 | func ExtractInfo(lr ListResult) ([]Container, error) { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 48 | var ci []Container |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 49 | defer lr.Body.Close() |
| 50 | body, err := ioutil.ReadAll(lr.Body) |
| 51 | if err != nil { |
| 52 | return ci, err |
| 53 | } |
| 54 | err = json.Unmarshal(body, &ci) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 55 | return ci, err |
| 56 | } |
| 57 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 58 | // ExtractNames is a function that takes a ListResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 59 | // and returns the containers' names. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 60 | func ExtractNames(lr ListResult) ([]string, error) { |
| 61 | var cns []string |
| 62 | defer lr.Body.Close() |
| 63 | body, err := ioutil.ReadAll(lr.Body) |
| 64 | if err != nil { |
| 65 | return cns, err |
| 66 | } |
| 67 | jr := string(body) |
| 68 | cns = strings.Split(jr, "\n") |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 69 | cns = cns[:len(cns)-1] |
| 70 | return cns, nil |
| 71 | } |
| 72 | |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 73 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 74 | // and returns the custom metadata associated with the container. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 75 | func ExtractMetadata(gr GetResult) map[string]string { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 76 | metadata := make(map[string]string) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 77 | for k, v := range gr.Header { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 78 | if strings.HasPrefix(k, "X-Container-Meta-") { |
| 79 | key := strings.TrimPrefix(k, "X-Container-Meta-") |
| 80 | metadata[key] = v[0] |
| 81 | } |
| 82 | } |
| 83 | return metadata |
| 84 | } |