Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame^] | 8 | type Container map[string]interface{} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 9 | |
| 10 | type ListOpts struct { |
| 11 | Full bool |
| 12 | Params map[string]string |
| 13 | } |
| 14 | |
| 15 | type CreateOpts struct { |
| 16 | Name string |
| 17 | Metadata map[string]string |
| 18 | Headers map[string]string |
| 19 | } |
| 20 | |
| 21 | type DeleteOpts struct { |
| 22 | Name string |
| 23 | Params map[string]string |
| 24 | } |
| 25 | |
| 26 | type UpdateOpts struct { |
| 27 | Name string |
| 28 | Metadata map[string]string |
| 29 | Headers map[string]string |
| 30 | } |
| 31 | |
| 32 | type GetOpts struct { |
| 33 | Name string |
| 34 | Metadata map[string]string |
| 35 | } |
| 36 | |
| 37 | // GetInfo is a function that takes a ListResult (of type *perigee.Response) |
| 38 | // and returns the containers' information. |
| 39 | func GetInfo(lr ListResult) ([]Container, error) { |
| 40 | var ci []Container |
| 41 | err := json.Unmarshal(lr.JsonResult, &ci) |
| 42 | return ci, err |
| 43 | } |
| 44 | |
| 45 | // GetNames is a function that takes a ListResult (of type *perigee.Response) |
| 46 | // and returns the containers' names. |
| 47 | func GetNames(lr ListResult) ([]string, error) { |
| 48 | jr := string(lr.JsonResult) |
| 49 | cns := strings.Split(jr, "\n") |
| 50 | cns = cns[:len(cns)-1] |
| 51 | return cns, nil |
| 52 | } |
| 53 | |
| 54 | // GetMetadata is a function that takes a GetResult (of type *perigee.Response) |
| 55 | // and returns the custom metadata associated with the container. |
| 56 | func GetMetadata(gr GetResult) map[string]string { |
| 57 | metadata := make(map[string]string) |
| 58 | for k, v := range gr.HttpResponse.Header { |
| 59 | if strings.HasPrefix(k, "X-Container-Meta-") { |
| 60 | key := strings.TrimPrefix(k, "X-Container-Meta-") |
| 61 | metadata[key] = v[0] |
| 62 | } |
| 63 | } |
| 64 | return metadata |
| 65 | } |