blob: 9ae344ba427988546a554636ca33818f59e5a4e1 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
Ash Wilson0faafcc2014-09-16 15:20:17 -04004 "fmt"
Jon Perritt816d2a02014-03-11 20:49:46 -05005 "strings"
Ash Wilson0faafcc2014-09-16 15:20:17 -04006
7 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -05008)
9
Jon Perrittbef727e2014-05-12 22:41:55 -050010// Container is a structure that holds information related to a storage container.
Jon Perrittc19adea2014-04-15 16:56:01 -050011type Container map[string]interface{}
Jon Perritt816d2a02014-03-11 20:49:46 -050012
Jon Perrittbef727e2014-05-12 22:41:55 -050013// ListOpts is a structure that holds parameters for listing containers.
Jon Perritt816d2a02014-03-11 20:49:46 -050014type ListOpts struct {
15 Full bool
16 Params map[string]string
17}
18
Jon Perrittbef727e2014-05-12 22:41:55 -050019// CreateOpts is a structure that holds parameters for creating a container.
Jon Perritt816d2a02014-03-11 20:49:46 -050020type CreateOpts struct {
21 Name string
22 Metadata map[string]string
23 Headers map[string]string
24}
25
Jon Perrittbef727e2014-05-12 22:41:55 -050026// DeleteOpts is a structure that holds parameters for deleting a container.
Jon Perritt816d2a02014-03-11 20:49:46 -050027type DeleteOpts struct {
28 Name string
29 Params map[string]string
30}
31
Jon Perrittbef727e2014-05-12 22:41:55 -050032// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
33// container's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050034type UpdateOpts struct {
35 Name string
36 Metadata map[string]string
37 Headers map[string]string
38}
39
Jon Perrittbef727e2014-05-12 22:41:55 -050040// GetOpts is a structure that holds parameters for getting a container's metadata.
Jon Perritt816d2a02014-03-11 20:49:46 -050041type GetOpts struct {
Jon Perrittcc10e312014-09-16 00:06:31 -050042 Name string
Jon Perritt816d2a02014-03-11 20:49:46 -050043}
44
Ash Wilson0faafcc2014-09-16 15:20:17 -040045// ExtractInfo is a function that takes a ListResult and returns the containers' information.
46func ExtractInfo(page pagination.Page) ([]Container, error) {
47 untyped := page.(ListResult).Body.([]interface{})
48 results := make([]Container, len(untyped))
49 for index, each := range untyped {
50 results[index] = Container(each.(map[string]interface{}))
Jon Perritteb575642014-04-24 15:16:31 -050051 }
Ash Wilson0faafcc2014-09-16 15:20:17 -040052 return results, nil
Jon Perritt816d2a02014-03-11 20:49:46 -050053}
54
Ash Wilson0faafcc2014-09-16 15:20:17 -040055// ExtractNames is a function that takes a ListResult and returns the containers' names.
56func ExtractNames(page pagination.Page) ([]string, error) {
57 casted := page.(ListResult)
58 ct := casted.Header.Get("Content-Type")
59
60 switch {
61 case strings.HasPrefix(ct, "application/json"):
62 parsed, err := ExtractInfo(page)
63 if err != nil {
64 return nil, err
65 }
66
67 names := make([]string, 0, len(parsed))
68 for _, container := range parsed {
69 names = append(names, container["name"].(string))
70 }
71 return names, nil
72 case strings.HasPrefix(ct, "text/plain"):
73 names := make([]string, 0, 50)
74
75 body := string(page.(ListResult).Body.([]uint8))
76 for _, name := range strings.Split(body, "\n") {
77 if len(name) > 0 {
78 names = append(names, name)
79 }
80 }
81
82 return names, nil
83 default:
84 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
Jon Perritteb575642014-04-24 15:16:31 -050085 }
Jon Perritt816d2a02014-03-11 20:49:46 -050086}
87
Jon Perritteb575642014-04-24 15:16:31 -050088// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050089// and returns the custom metadata associated with the container.
Jon Perritteb575642014-04-24 15:16:31 -050090func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -050091 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -050092 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -050093 if strings.HasPrefix(k, "X-Container-Meta-") {
94 key := strings.TrimPrefix(k, "X-Container-Meta-")
95 metadata[key] = v[0]
96 }
97 }
98 return metadata
99}