forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
123 lines (101 loc) · 3.09 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
package datastores
import (
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// Version represents a version API resource. Multiple versions belong to a Datastore.
type Version struct {
ID string
Links []gophercloud.Link
Name string
}
// Datastore represents a Datastore API resource.
type Datastore struct {
DefaultVersion string `json:"default_version" mapstructure:"default_version"`
ID string
Links []gophercloud.Link
Name string
Versions []Version
}
// DatastorePartial is a meta structure which is used in various API responses.
// It is a lightweight and truncated version of a full Datastore resource,
// offering details of the Version, Type and VersionID only.
type DatastorePartial struct {
Version string
Type string
VersionID string `json:"version_id" mapstructure:"version_id"`
}
// GetResult represents the result of a Get operation.
type GetResult struct {
gophercloud.Result
}
// GetVersionResult represents the result of getting a version.
type GetVersionResult struct {
gophercloud.Result
}
// DatastorePage represents a page of datastore resources.
type DatastorePage struct {
pagination.SinglePageBase
}
// IsEmpty indicates whether a Datastore collection is empty.
func (r DatastorePage) IsEmpty() (bool, error) {
is, err := ExtractDatastores(r)
if err != nil {
return true, err
}
return len(is) == 0, nil
}
// ExtractDatastores retrieves a slice of datastore structs from a paginated
// collection.
func ExtractDatastores(page pagination.Page) ([]Datastore, error) {
casted := page.(DatastorePage).Body
var resp struct {
Datastores []Datastore `mapstructure:"datastores" json:"datastores"`
}
err := mapstructure.Decode(casted, &resp)
return resp.Datastores, err
}
// Extract retrieves a single Datastore struct from an operation result.
func (r GetResult) Extract() (*Datastore, error) {
if r.Err != nil {
return nil, r.Err
}
var response struct {
Datastore Datastore `mapstructure:"datastore"`
}
err := mapstructure.Decode(r.Body, &response)
return &response.Datastore, err
}
// DatastorePage represents a page of version resources.
type VersionPage struct {
pagination.SinglePageBase
}
// IsEmpty indicates whether a collection of version resources is empty.
func (r VersionPage) IsEmpty() (bool, error) {
is, err := ExtractVersions(r)
if err != nil {
return true, err
}
return len(is) == 0, nil
}
// ExtractVersions retrieves a slice of versions from a paginated collection.
func ExtractVersions(page pagination.Page) ([]Version, error) {
casted := page.(VersionPage).Body
var resp struct {
Versions []Version `mapstructure:"versions" json:"versions"`
}
err := mapstructure.Decode(casted, &resp)
return resp.Versions, err
}
// Extract retrieves a single Version struct from an operation result.
func (r GetVersionResult) Extract() (*Version, error) {
if r.Err != nil {
return nil, r.Err
}
var response struct {
Version Version `mapstructure:"version"`
}
err := mapstructure.Decode(r.Body, &response)
return &response.Version, err
}