forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
221 lines (199 loc) · 6.01 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package publicips
import (
"fmt"
"reflect"
"time"
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// PublicIP represents a public IP address.
type PublicIP struct {
// The unique ID of the public IP.
ID string `mapstructure:"id"`
// The IPv4 address of the public IP.
PublicIPv4 string `mapstructure:"public_ip_v4"`
// The cloud server (node) of the public IP.
CloudServer struct {
// The cloud server ID.
ID string `mapstructure:"id"`
// The name of the server.
Name string `mapstructure:"name"`
// The cloud network for the cloud server.
CloudNetwork struct {
// The network ID.
ID string `mapstructure:"id"`
// The network name.
Name string `mapstructure:"name"`
// The network's private IPv4 address.
PrivateIPv4 string `mapstructure:"private_ip_v4"`
// The IP range for the network.
CIDR string `mapstructure:"cidr"`
// The datetime the network was created.
CreatedAt time.Time `mapstructure:"-"`
// The last datetime the network was updated.
UpdatedAt time.Time `mapstructure:"-"`
} `mapstructure:"cloud_network"`
// The datetime the server was created.
CreatedAt time.Time `mapstructure:"-"`
// The datetime the server was last updated.
UpdatedAt time.Time `mapstructure:"-"`
} `mapstructure:"cloud_server"`
// The status of the public IP.
Status string `mapstructure:"status"`
// The details of the status of the public IP.
StatusDetail string `mapstructure:"status_detail"`
// The time the public IP was created.
CreatedAt time.Time `mapstructure:"-"`
// The time the public IP was last updated.
UpdatedAt time.Time `mapstructure:"-"`
}
// PublicIPPage is the page returned by a pager when traversing over a
// collection of PublicIPs.
type PublicIPPage struct {
pagination.SinglePageBase
}
// IsEmpty returns true if a PublicIPPage contains no PublicIPs.
func (r PublicIPPage) IsEmpty() (bool, error) {
n, err := ExtractPublicIPs(r)
if err != nil {
return true, err
}
return len(n) == 0, nil
}
// ExtractPublicIPs extracts and returns a slice of PublicIPs. It is used while iterating over
// a publicips.List call.
func ExtractPublicIPs(page pagination.Page) ([]PublicIP, error) {
var res []PublicIP
casted := page.(PublicIPPage).Body
err := mapstructure.Decode(casted, &res)
var rawNodesDetails []interface{}
switch casted.(type) {
case interface{}:
rawNodesDetails = casted.([]interface{})
default:
return res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
}
for i := range rawNodesDetails {
thisNodeDetails := (rawNodesDetails[i]).(map[string]interface{})
if t, ok := thisNodeDetails["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].CreatedAt = creationTime
}
if t, ok := thisNodeDetails["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].UpdatedAt = updatedTime
}
if cs, ok := thisNodeDetails["cloud_server"].(map[string]interface{}); ok {
if t, ok := cs["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].CloudServer.CreatedAt = creationTime
}
if t, ok := cs["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].CloudServer.UpdatedAt = updatedTime
}
if cn, ok := cs["cloud_network"].(map[string]interface{}); ok {
if t, ok := cn["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].CloudServer.CloudNetwork.CreatedAt = creationTime
}
if t, ok := cn["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].CloudServer.CloudNetwork.UpdatedAt = updatedTime
}
}
}
}
return res, err
}
// PublicIPResult represents a result that can be extracted into a PublicIP.
type PublicIPResult struct {
gophercloud.Result
}
// CreateResult represents the result of a Create operation.
type CreateResult struct {
PublicIPResult
}
// GetResult represents the result of a Get operation.
type GetResult struct {
PublicIPResult
}
// Extract is a function that extracts a PublicIP from a PublicIPResult.
func (r PublicIPResult) Extract() (*PublicIP, error) {
if r.Err != nil {
return nil, r.Err
}
var res PublicIP
err := mapstructure.Decode(r.Body, &res)
b := r.Body.(map[string]interface{})
if date, ok := b["created"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
res.CreatedAt = t
}
if date, ok := b["updated"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
res.UpdatedAt = t
}
if cs, ok := b["cloud_server"].(map[string]interface{}); ok {
if t, ok := cs["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return &res, err
}
res.CloudServer.CreatedAt = creationTime
}
if t, ok := cs["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return &res, err
}
res.CloudServer.UpdatedAt = updatedTime
}
if cn, ok := cs["cloud_network"].(map[string]interface{}); ok {
if t, ok := cn["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return &res, err
}
res.CloudServer.CloudNetwork.CreatedAt = creationTime
}
if t, ok := cn["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return &res, err
}
res.CloudServer.CloudNetwork.UpdatedAt = updatedTime
}
}
}
return &res, err
}
// DeleteResult represents the result of a Delete operation.
type DeleteResult struct {
gophercloud.ErrResult
}