blob: 8fa19aae0fc78ab8f887cb01e632650462e190c9 [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 {
42 Name string
43 Metadata map[string]string
44}
45
Ash Wilson0faafcc2014-09-16 15:20:17 -040046// ExtractInfo is a function that takes a ListResult and returns the containers' information.
47func ExtractInfo(page pagination.Page) ([]Container, error) {
48 untyped := page.(ListResult).Body.([]interface{})
49 results := make([]Container, len(untyped))
50 for index, each := range untyped {
51 results[index] = Container(each.(map[string]interface{}))
Jon Perritteb575642014-04-24 15:16:31 -050052 }
Ash Wilson0faafcc2014-09-16 15:20:17 -040053 return results, nil
Jon Perritt816d2a02014-03-11 20:49:46 -050054}
55
Ash Wilson0faafcc2014-09-16 15:20:17 -040056// ExtractNames is a function that takes a ListResult and returns the containers' names.
57func ExtractNames(page pagination.Page) ([]string, error) {
58 casted := page.(ListResult)
59 ct := casted.Header.Get("Content-Type")
60
61 switch {
62 case strings.HasPrefix(ct, "application/json"):
63 parsed, err := ExtractInfo(page)
64 if err != nil {
65 return nil, err
66 }
67
68 names := make([]string, 0, len(parsed))
69 for _, container := range parsed {
70 names = append(names, container["name"].(string))
71 }
72 return names, nil
73 case strings.HasPrefix(ct, "text/plain"):
74 names := make([]string, 0, 50)
75
76 body := string(page.(ListResult).Body.([]uint8))
77 for _, name := range strings.Split(body, "\n") {
78 if len(name) > 0 {
79 names = append(names, name)
80 }
81 }
82
83 return names, nil
84 default:
85 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
Jon Perritteb575642014-04-24 15:16:31 -050086 }
Jon Perritt816d2a02014-03-11 20:49:46 -050087}
88
Jon Perritteb575642014-04-24 15:16:31 -050089// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
Jon Perritt816d2a02014-03-11 20:49:46 -050090// and returns the custom metadata associated with the container.
Jon Perritteb575642014-04-24 15:16:31 -050091func ExtractMetadata(gr GetResult) map[string]string {
Jon Perritt816d2a02014-03-11 20:49:46 -050092 metadata := make(map[string]string)
Jon Perritteb575642014-04-24 15:16:31 -050093 for k, v := range gr.Header {
Jon Perritt816d2a02014-03-11 20:49:46 -050094 if strings.HasPrefix(k, "X-Container-Meta-") {
95 key := strings.TrimPrefix(k, "X-Container-Meta-")
96 metadata[key] = v[0]
97 }
98 }
99 return metadata
100}