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