forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
139 lines (119 loc) · 3.73 KB
/
results.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package containers
import (
"fmt"
"strings"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
"github.com/mitchellh/mapstructure"
)
// Container represents a container resource.
type Container struct {
// The total number of bytes stored in the container.
Bytes int `json:"bytes" mapstructure:"bytes"`
// The total number of objects stored in the container.
Count int `json:"count" mapstructure:"count"`
// The name of the container.
Name string `json:"name" mapstructure:"name"`
}
// ContainerPage is the page returned by a pager when traversing over a
// collection of containers.
type ContainerPage struct {
pagination.MarkerPageBase
}
// IsEmpty returns true if a ListResult contains no container names.
func (r ContainerPage) IsEmpty() (bool, error) {
names, err := ExtractNames(r)
if err != nil {
return true, err
}
return len(names) == 0, nil
}
// LastMarker returns the last container name in a ListResult.
func (r ContainerPage) LastMarker() (string, error) {
names, err := ExtractNames(r)
if err != nil {
return "", err
}
if len(names) == 0 {
return "", nil
}
return names[len(names)-1], nil
}
// ExtractInfo is a function that takes a ListResult and returns the containers' information.
func ExtractInfo(page pagination.Page) ([]Container, error) {
untyped := page.(ContainerPage).Body.([]interface{})
results := make([]Container, len(untyped))
for index, each := range untyped {
container := each.(map[string]interface{})
err := mapstructure.Decode(container, &results[index])
if err != nil {
return results, err
}
}
return results, nil
}
// ExtractNames is a function that takes a ListResult and returns the containers' names.
func ExtractNames(page pagination.Page) ([]string, error) {
casted := page.(ContainerPage)
ct := casted.Header.Get("Content-Type")
switch {
case strings.HasPrefix(ct, "application/json"):
parsed, err := ExtractInfo(page)
if err != nil {
return nil, err
}
names := make([]string, 0, len(parsed))
for _, container := range parsed {
names = append(names, container.Name)
}
return names, nil
case strings.HasPrefix(ct, "text/plain"):
names := make([]string, 0, 50)
body := string(page.(ContainerPage).Body.([]uint8))
for _, name := range strings.Split(body, "\n") {
if len(name) > 0 {
names = append(names, name)
}
}
return names, nil
default:
return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
}
}
// GetResult represents the result of a get operation.
type GetResult struct {
gophercloud.HeaderResult
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metadata associated with the container.
func (gr GetResult) ExtractMetadata() (map[string]string, error) {
if gr.Err != nil {
return nil, gr.Err
}
metadata := make(map[string]string)
for k, v := range gr.Header {
if strings.HasPrefix(k, "X-Container-Meta-") {
key := strings.TrimPrefix(k, "X-Container-Meta-")
metadata[key] = v[0]
}
}
return metadata, nil
}
// CreateResult represents the result of a create operation. To extract the
// the headers from the HTTP response, you can invoke the 'ExtractHeader'
// method on the result struct.
type CreateResult struct {
gophercloud.HeaderResult
}
// UpdateResult represents the result of an update operation. To extract the
// the headers from the HTTP response, you can invoke the 'ExtractHeader'
// method on the result struct.
type UpdateResult struct {
gophercloud.HeaderResult
}
// DeleteResult represents the result of a delete operation. To extract the
// the headers from the HTTP response, you can invoke the 'ExtractHeader'
// method on the result struct.
type DeleteResult struct {
gophercloud.HeaderResult
}