blob: 3a006476d0441d80b5d335ea37dee576d08ebfee [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 Perrittbef727e2014-05-12 22:41:55 -05009// Container is a structure that holds information related to a storage container.
Jon Perrittc19adea2014-04-15 16:56:01 -050010type Container map[string]interface{}
Jon Perritt816d2a02014-03-11 20:49:46 -050011
Jon Perrittbef727e2014-05-12 22:41:55 -050012// ListOpts is a structure that holds parameters for listing containers.
Jon Perritt816d2a02014-03-11 20:49:46 -050013type ListOpts struct {
14 Full bool
15 Params map[string]string
16}
17
Jon Perrittbef727e2014-05-12 22:41:55 -050018// CreateOpts is a structure that holds parameters for creating a container.
Jon Perritt816d2a02014-03-11 20:49:46 -050019type CreateOpts struct {
20 Name string
21 Metadata map[string]string
22 Headers map[string]string
23}
24
Jon Perrittbef727e2014-05-12 22:41:55 -050025// DeleteOpts is a structure that holds parameters for deleting a container.
Jon Perritt816d2a02014-03-11 20:49:46 -050026type DeleteOpts struct {
27 Name string
28 Params map[string]string
29}
30
Jon Perrittbef727e2014-05-12 22:41:55 -050031// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
32// container's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050033type UpdateOpts struct {
34 Name string
35 Metadata map[string]string
36 Headers map[string]string
37}
38
Jon Perrittbef727e2014-05-12 22:41:55 -050039// GetOpts is a structure that holds parameters for getting a container's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050040type GetOpts struct {
41 Name string
42 Metadata map[string]string
43}
44
Jon Perritteb575642014-04-24 15:16:31 -050045// ExtractInfo is a function that takes a ListResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050046// and returns the containers' information.
Jon Perritteb575642014-04-24 15:16:31 -050047func ExtractInfo(lr ListResult) ([]Container, error) {
Jon Perritt816d2a02014-03-11 20:49:46 -050048 var ci []Container
Jon Perritteb575642014-04-24 15:16:31 -050049 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 Perritt816d2a02014-03-11 20:49:46 -050055 return ci, err
56}
57
Jon Perritteb575642014-04-24 15:16:31 -050058// ExtractNames is a function that takes a ListResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050059// and returns the containers' names.
Jon Perritteb575642014-04-24 15:16:31 -050060func 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 Perritt816d2a02014-03-11 20:49:46 -050069 cns = cns[:len(cns)-1]
70 return cns, nil
71}
72
Jon Perritteb575642014-04-24 15:16:31 -050073// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050074// and returns the custom metadata associated with the container.
Jon Perritteb575642014-04-24 15:16:31 -050075func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -050076 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -050077 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -050078 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}