blob: 2a5efe1779bacd77dae421740c903f6fd47a399a [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
4 "encoding/json"
Jon Perritteb575642014-04-24 15:16:31 -05005 "io/ioutil"
Jon Perritt816d2a02014-03-11 20:49:46 -05006 "strings"
7)
8
Jon Perrittc19adea2014-04-15 16:56:01 -05009type Container map[string]interface{}
Jon Perritt816d2a02014-03-11 20:49:46 -050010
11type ListOpts struct {
12 Full bool
13 Params map[string]string
14}
15
16type CreateOpts struct {
17 Name string
18 Metadata map[string]string
19 Headers map[string]string
20}
21
22type DeleteOpts struct {
23 Name string
24 Params map[string]string
25}
26
27type UpdateOpts struct {
28 Name string
29 Metadata map[string]string
30 Headers map[string]string
31}
32
33type GetOpts struct {
34 Name string
35 Metadata map[string]string
36}
37
Jon Perritteb575642014-04-24 15:16:31 -050038// ExtractInfo is a function that takes a ListResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050039// and returns the containers' information.
Jon Perritteb575642014-04-24 15:16:31 -050040func ExtractInfo(lr ListResult) ([]Container, error) {
Jon Perritt816d2a02014-03-11 20:49:46 -050041 var ci []Container
Jon Perritteb575642014-04-24 15:16:31 -050042 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 Perritt816d2a02014-03-11 20:49:46 -050048 return ci, err
49}
50
Jon Perritteb575642014-04-24 15:16:31 -050051// ExtractNames is a function that takes a ListResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050052// and returns the containers' names.
Jon Perritteb575642014-04-24 15:16:31 -050053func 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 Perritt816d2a02014-03-11 20:49:46 -050062 cns = cns[:len(cns)-1]
63 return cns, nil
64}
65
Jon Perritteb575642014-04-24 15:16:31 -050066// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050067// and returns the custom metadata associated with the container.
Jon Perritteb575642014-04-24 15:16:31 -050068func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -050069 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -050070 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -050071 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}