forked from kubernetes/kubernetes
/
results.go
197 lines (162 loc) · 4.84 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package configurations
import (
"fmt"
"reflect"
"time"
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// Config represents a configuration group API resource.
type Config struct {
Created time.Time `mapstructure:"-"`
Updated time.Time `mapstructure:"-"`
DatastoreName string `mapstructure:"datastore_name"`
DatastoreVersionID string `mapstructure:"datastore_version_id"`
DatastoreVersionName string `mapstructure:"datastore_version_name"`
Description string
ID string
Name string
Values map[string]interface{}
}
// ConfigPage contains a page of Config resources in a paginated collection.
type ConfigPage struct {
pagination.SinglePageBase
}
// IsEmpty indicates whether a ConfigPage is empty.
func (r ConfigPage) IsEmpty() (bool, error) {
is, err := ExtractConfigs(r)
if err != nil {
return true, err
}
return len(is) == 0, nil
}
// ExtractConfigs will retrieve a slice of Config structs from a page.
func ExtractConfigs(page pagination.Page) ([]Config, error) {
casted := page.(ConfigPage).Body
var resp struct {
Configs []Config `mapstructure:"configurations" json:"configurations"`
}
if err := mapstructure.Decode(casted, &resp); err != nil {
return nil, err
}
var vals []interface{}
switch casted.(type) {
case map[string]interface{}:
vals = casted.(map[string]interface{})["configurations"].([]interface{})
case map[string][]interface{}:
vals = casted.(map[string][]interface{})["configurations"]
default:
return resp.Configs, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
}
for i, v := range vals {
val := v.(map[string]interface{})
if t, ok := val["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return resp.Configs, err
}
resp.Configs[i].Created = creationTime
}
if t, ok := val["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return resp.Configs, err
}
resp.Configs[i].Updated = updatedTime
}
}
return resp.Configs, nil
}
type commonResult struct {
gophercloud.Result
}
// Extract will retrieve a Config resource from an operation result.
func (r commonResult) Extract() (*Config, error) {
if r.Err != nil {
return nil, r.Err
}
var response struct {
Config Config `mapstructure:"configuration"`
}
err := mapstructure.Decode(r.Body, &response)
val := r.Body.(map[string]interface{})["configuration"].(map[string]interface{})
if t, ok := val["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return &response.Config, err
}
response.Config.Created = creationTime
}
if t, ok := val["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return &response.Config, err
}
response.Config.Updated = updatedTime
}
return &response.Config, err
}
// GetResult represents the result of a Get operation.
type GetResult struct {
commonResult
}
// CreateResult represents the result of a Create operation.
type CreateResult struct {
commonResult
}
// UpdateResult represents the result of an Update operation.
type UpdateResult struct {
gophercloud.ErrResult
}
// ReplaceResult represents the result of a Replace operation.
type ReplaceResult struct {
gophercloud.ErrResult
}
// DeleteResult represents the result of a Delete operation.
type DeleteResult struct {
gophercloud.ErrResult
}
// Param represents a configuration parameter API resource.
type Param struct {
Max int
Min int
Name string
RestartRequired bool `mapstructure:"restart_required" json:"restart_required"`
Type string
}
// ParamPage contains a page of Param resources in a paginated collection.
type ParamPage struct {
pagination.SinglePageBase
}
// IsEmpty indicates whether a ParamPage is empty.
func (r ParamPage) IsEmpty() (bool, error) {
is, err := ExtractParams(r)
if err != nil {
return true, err
}
return len(is) == 0, nil
}
// ExtractParams will retrieve a slice of Param structs from a page.
func ExtractParams(page pagination.Page) ([]Param, error) {
casted := page.(ParamPage).Body
var resp struct {
Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"`
}
err := mapstructure.Decode(casted, &resp)
return resp.Params, err
}
// ParamResult represents the result of an operation which retrieves details
// about a particular configuration param.
type ParamResult struct {
gophercloud.Result
}
// Extract will retrieve a param from an operation result.
func (r ParamResult) Extract() (*Param, error) {
if r.Err != nil {
return nil, r.Err
}
var param Param
err := mapstructure.Decode(r.Body, ¶m)
return ¶m, err
}