forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
214 lines (181 loc) · 5.25 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
package roles
import (
"encoding/json"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/internal"
"github.com/gophercloud/gophercloud/pagination"
)
// Role grants permissions to a user.
type Role struct {
// DomainID is the domain ID the role belongs to.
DomainID string `json:"domain_id"`
// ID is the unique ID of the role.
ID string `json:"id"`
// Links contains referencing links to the role.
Links map[string]interface{} `json:"links"`
// Name is the role name
Name string `json:"name"`
// Extra is a collection of miscellaneous key/values.
Extra map[string]interface{} `json:"-"`
}
func (r *Role) UnmarshalJSON(b []byte) error {
type tmp Role
var s struct {
tmp
Extra map[string]interface{} `json:"extra"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Role(s.tmp)
// Collect other fields and bundle them into Extra
// but only if a field titled "extra" wasn't sent.
if s.Extra != nil {
r.Extra = s.Extra
} else {
var result interface{}
err := json.Unmarshal(b, &result)
if err != nil {
return err
}
if resultMap, ok := result.(map[string]interface{}); ok {
r.Extra = internal.RemainingKeys(Role{}, resultMap)
}
}
return err
}
type roleResult struct {
gophercloud.Result
}
// GetResult is the response from a Get operation. Call its Extract method
// to interpret it as a Role.
type GetResult struct {
roleResult
}
// CreateResult is the response from a Create operation. Call its Extract method
// to interpret it as a Role
type CreateResult struct {
roleResult
}
// UpdateResult is the response from an Update operation. Call its Extract
// method to interpret it as a Role.
type UpdateResult struct {
roleResult
}
// DeleteResult is the response from a Delete operation. Call its ExtractErr to
// determine if the request succeeded or failed.
type DeleteResult struct {
gophercloud.ErrResult
}
// RolePage is a single page of Role results.
type RolePage struct {
pagination.LinkedPageBase
}
// IsEmpty determines whether or not a page of Roles contains any results.
func (r RolePage) IsEmpty() (bool, error) {
roles, err := ExtractRoles(r)
return len(roles) == 0, err
}
// NextPageURL extracts the "next" link from the links section of the result.
func (r RolePage) NextPageURL() (string, error) {
var s struct {
Links struct {
Next string `json:"next"`
Previous string `json:"previous"`
} `json:"links"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return s.Links.Next, err
}
// ExtractProjects returns a slice of Roles contained in a single page of
// results.
func ExtractRoles(r pagination.Page) ([]Role, error) {
var s struct {
Roles []Role `json:"roles"`
}
err := (r.(RolePage)).ExtractInto(&s)
return s.Roles, err
}
// Extract interprets any roleResults as a Role.
func (r roleResult) Extract() (*Role, error) {
var s struct {
Role *Role `json:"role"`
}
err := r.ExtractInto(&s)
return s.Role, err
}
// RoleAssignment is the result of a role assignments query.
type RoleAssignment struct {
Role AssignedRole `json:"role,omitempty"`
Scope Scope `json:"scope,omitempty"`
User User `json:"user,omitempty"`
Group Group `json:"group,omitempty"`
}
// AssignedRole represents a Role in an assignment.
type AssignedRole struct {
ID string `json:"id,omitempty"`
}
// Scope represents a scope in a Role assignment.
type Scope struct {
Domain Domain `json:"domain,omitempty"`
Project Project `json:"project,omitempty"`
}
// Domain represents a domain in a role assignment scope.
type Domain struct {
ID string `json:"id,omitempty"`
}
// Project represents a project in a role assignment scope.
type Project struct {
ID string `json:"id,omitempty"`
}
// User represents a user in a role assignment scope.
type User struct {
ID string `json:"id,omitempty"`
}
// Group represents a group in a role assignment scope.
type Group struct {
ID string `json:"id,omitempty"`
}
// RoleAssignmentPage is a single page of RoleAssignments results.
type RoleAssignmentPage struct {
pagination.LinkedPageBase
}
// IsEmpty returns true if the RoleAssignmentPage contains no results.
func (r RoleAssignmentPage) IsEmpty() (bool, error) {
roleAssignments, err := ExtractRoleAssignments(r)
return len(roleAssignments) == 0, err
}
// NextPageURL uses the response's embedded link reference to navigate to
// the next page of results.
func (r RoleAssignmentPage) NextPageURL() (string, error) {
var s struct {
Links struct {
Next string `json:"next"`
} `json:"links"`
}
err := r.ExtractInto(&s)
return s.Links.Next, err
}
// ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection
// acquired from List.
func ExtractRoleAssignments(r pagination.Page) ([]RoleAssignment, error) {
var s struct {
RoleAssignments []RoleAssignment `json:"role_assignments"`
}
err := (r.(RoleAssignmentPage)).ExtractInto(&s)
return s.RoleAssignments, err
}
// AssignmentResult represents the result of an assign operation.
// Call ExtractErr method to determine if the request succeeded or failed.
type AssignmentResult struct {
gophercloud.ErrResult
}
// UnassignmentResult represents the result of an unassign operation.
// Call ExtractErr method to determine if the request succeeded or failed.
type UnassignmentResult struct {
gophercloud.ErrResult
}