forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
128 lines (101 loc) · 2.81 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
package users
import (
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// User represents a user resource that exists on the API.
type User struct {
// The UUID for this user.
ID string
// The human name for this user.
Name string
// The username for this user.
Username string
// Indicates whether the user is enabled (true) or disabled (false).
Enabled bool
// The email address for this user.
Email string
// The ID of the tenant to which this user belongs.
TenantID string `mapstructure:"tenant_id"`
}
// Role assigns specific responsibilities to users, allowing them to accomplish
// certain API operations whilst scoped to a service.
type Role struct {
// UUID of the role
ID string
// Name of the role
Name string
}
// UserPage is a single page of a User collection.
type UserPage struct {
pagination.SinglePageBase
}
// RolePage is a single page of a user Role collection.
type RolePage struct {
pagination.SinglePageBase
}
// IsEmpty determines whether or not a page of Tenants contains any results.
func (page UserPage) IsEmpty() (bool, error) {
users, err := ExtractUsers(page)
if err != nil {
return false, err
}
return len(users) == 0, nil
}
// ExtractUsers returns a slice of Tenants contained in a single page of results.
func ExtractUsers(page pagination.Page) ([]User, error) {
casted := page.(UserPage).Body
var response struct {
Users []User `mapstructure:"users"`
}
err := mapstructure.Decode(casted, &response)
return response.Users, err
}
// IsEmpty determines whether or not a page of Tenants contains any results.
func (page RolePage) IsEmpty() (bool, error) {
users, err := ExtractRoles(page)
if err != nil {
return false, err
}
return len(users) == 0, nil
}
// ExtractRoles returns a slice of Roles contained in a single page of results.
func ExtractRoles(page pagination.Page) ([]Role, error) {
casted := page.(RolePage).Body
var response struct {
Roles []Role `mapstructure:"roles"`
}
err := mapstructure.Decode(casted, &response)
return response.Roles, err
}
type commonResult struct {
gophercloud.Result
}
// Extract interprets any commonResult as a User, if possible.
func (r commonResult) Extract() (*User, error) {
if r.Err != nil {
return nil, r.Err
}
var response struct {
User User `mapstructure:"user"`
}
err := mapstructure.Decode(r.Body, &response)
return &response.User, err
}
// CreateResult represents the result of a Create operation
type CreateResult struct {
commonResult
}
// GetResult represents the result of a Get operation
type GetResult struct {
commonResult
}
// UpdateResult represents the result of an Update operation
type UpdateResult struct {
commonResult
}
// DeleteResult represents the result of a Delete operation
type DeleteResult struct {
commonResult
}