github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/roles/results.go (about) 1 package roles 2 3 import ( 4 "encoding/json" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // Role grants permissions to a user. 11 type Role struct { 12 // DomainID is the domain ID the role belongs to. 13 DomainID string `json:"domain_id"` 14 15 // ID is the unique ID of the role. 16 ID string `json:"id"` 17 18 // Links contains referencing links to the role. 19 Links map[string]any `json:"links"` 20 21 // Name is the role name 22 Name string `json:"name"` 23 24 // Extra is a collection of miscellaneous key/values. 25 Extra map[string]any `json:"-"` 26 } 27 28 func (r *Role) UnmarshalJSON(b []byte) error { 29 type tmp Role 30 var s struct { 31 tmp 32 Extra map[string]any `json:"extra"` 33 } 34 err := json.Unmarshal(b, &s) 35 if err != nil { 36 return err 37 } 38 *r = Role(s.tmp) 39 40 // Collect other fields and bundle them into Extra 41 // but only if a field titled "extra" wasn't sent. 42 if s.Extra != nil { 43 r.Extra = s.Extra 44 } else { 45 var result any 46 err := json.Unmarshal(b, &result) 47 if err != nil { 48 return err 49 } 50 if resultMap, ok := result.(map[string]any); ok { 51 r.Extra = gophercloud.RemainingKeys(Role{}, resultMap) 52 } 53 } 54 55 return err 56 } 57 58 type roleResult struct { 59 gophercloud.Result 60 } 61 62 // GetResult is the response from a Get operation. Call its Extract method 63 // to interpret it as a Role. 64 type GetResult struct { 65 roleResult 66 } 67 68 // CreateResult is the response from a Create operation. Call its Extract method 69 // to interpret it as a Role 70 type CreateResult struct { 71 roleResult 72 } 73 74 // UpdateResult is the response from an Update operation. Call its Extract 75 // method to interpret it as a Role. 76 type UpdateResult struct { 77 roleResult 78 } 79 80 // DeleteResult is the response from a Delete operation. Call its ExtractErr to 81 // determine if the request succeeded or failed. 82 type DeleteResult struct { 83 gophercloud.ErrResult 84 } 85 86 // RolePage is a single page of Role results. 87 type RolePage struct { 88 pagination.LinkedPageBase 89 } 90 91 // IsEmpty determines whether or not a page of Roles contains any results. 92 func (r RolePage) IsEmpty() (bool, error) { 93 if r.StatusCode == 204 { 94 return true, nil 95 } 96 97 roles, err := ExtractRoles(r) 98 return len(roles) == 0, err 99 } 100 101 // NextPageURL extracts the "next" link from the links section of the result. 102 func (r RolePage) NextPageURL() (string, error) { 103 var s struct { 104 Links struct { 105 Next string `json:"next"` 106 Previous string `json:"previous"` 107 } `json:"links"` 108 } 109 err := r.ExtractInto(&s) 110 if err != nil { 111 return "", err 112 } 113 return s.Links.Next, err 114 } 115 116 // ExtractProjects returns a slice of Roles contained in a single page of 117 // results. 118 func ExtractRoles(r pagination.Page) ([]Role, error) { 119 var s struct { 120 Roles []Role `json:"roles"` 121 } 122 err := (r.(RolePage)).ExtractInto(&s) 123 return s.Roles, err 124 } 125 126 // Extract interprets any roleResults as a Role. 127 func (r roleResult) Extract() (*Role, error) { 128 var s struct { 129 Role *Role `json:"role"` 130 } 131 err := r.ExtractInto(&s) 132 return s.Role, err 133 } 134 135 // RoleAssignment is the result of a role assignments query. 136 type RoleAssignment struct { 137 Role AssignedRole `json:"role,omitempty"` 138 Scope Scope `json:"scope,omitempty"` 139 User User `json:"user,omitempty"` 140 Group Group `json:"group,omitempty"` 141 } 142 143 // AssignedRole represents a Role in an assignment. 144 type AssignedRole struct { 145 ID string `json:"id,omitempty"` 146 Name string `json:"name,omitempty"` 147 } 148 149 // Scope represents a scope in a Role assignment. 150 type Scope struct { 151 Domain Domain `json:"domain,omitempty"` 152 Project Project `json:"project,omitempty"` 153 } 154 155 // Domain represents a domain in a role assignment scope. 156 type Domain struct { 157 ID string `json:"id,omitempty"` 158 Name string `json:"name,omitempty"` 159 } 160 161 // Project represents a project in a role assignment scope. 162 type Project struct { 163 Domain Domain `json:"domain,omitempty"` 164 ID string `json:"id,omitempty"` 165 Name string `json:"name,omitempty"` 166 } 167 168 // User represents a user in a role assignment scope. 169 type User struct { 170 Domain Domain `json:"domain,omitempty"` 171 ID string `json:"id,omitempty"` 172 Name string `json:"name,omitempty"` 173 } 174 175 // Group represents a group in a role assignment scope. 176 type Group struct { 177 Domain Domain `json:"domain,omitempty"` 178 ID string `json:"id,omitempty"` 179 Name string `json:"name,omitempty"` 180 } 181 182 // RoleAssignmentPage is a single page of RoleAssignments results. 183 type RoleAssignmentPage struct { 184 pagination.LinkedPageBase 185 } 186 187 // IsEmpty returns true if the RoleAssignmentPage contains no results. 188 func (r RoleAssignmentPage) IsEmpty() (bool, error) { 189 if r.StatusCode == 204 { 190 return true, nil 191 } 192 193 roleAssignments, err := ExtractRoleAssignments(r) 194 return len(roleAssignments) == 0, err 195 } 196 197 // NextPageURL uses the response's embedded link reference to navigate to 198 // the next page of results. 199 func (r RoleAssignmentPage) NextPageURL() (string, error) { 200 var s struct { 201 Links struct { 202 Next string `json:"next"` 203 } `json:"links"` 204 } 205 err := r.ExtractInto(&s) 206 return s.Links.Next, err 207 } 208 209 // ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection 210 // acquired from List. 211 func ExtractRoleAssignments(r pagination.Page) ([]RoleAssignment, error) { 212 var s struct { 213 RoleAssignments []RoleAssignment `json:"role_assignments"` 214 } 215 err := (r.(RoleAssignmentPage)).ExtractInto(&s) 216 return s.RoleAssignments, err 217 } 218 219 // AssignmentResult represents the result of an assign operation. 220 // Call ExtractErr method to determine if the request succeeded or failed. 221 type AssignmentResult struct { 222 gophercloud.ErrResult 223 } 224 225 // UnassignmentResult represents the result of an unassign operation. 226 // Call ExtractErr method to determine if the request succeeded or failed. 227 type UnassignmentResult struct { 228 gophercloud.ErrResult 229 } 230 231 type impliedRoleResult struct { 232 gophercloud.Result 233 } 234 235 // ImpliedRoleResult is the result of an PUT request. Call its Extract method to 236 // interpret it as a roleInference. 237 type CreateImpliedRoleResult struct { 238 impliedRoleResult 239 } 240 241 type GetImpliedRoleResult struct { 242 impliedRoleResult 243 } 244 type PriorRole struct { 245 // ID contains the ID of the role in a prior_role object. 246 ID string `json:"id,omitempty"` 247 // Name contains the name of a role in a prior_role object. 248 Name string `json:"name,omitempty"` 249 // Links contains referencing links to the prior_role. 250 Links map[string]any `json:"links"` 251 } 252 253 type ImpliedRole struct { 254 // ID contains the ID of the role in an implied_role object. 255 ID string `json:"id,omitempty"` 256 // Name contains the name of role in an implied_role. 257 Name string `json:"name,omitempty"` 258 // Links contains referencing links to the implied_role. 259 Links map[string]any `json:"links"` 260 } 261 262 type RoleInference struct { 263 // PriorRole is the role object that implies a list of implied_role objects. 264 PriorRole PriorRole `json:"prior_role"` 265 // Implies is an array of implied_role objects implied by a prior_role object. 266 ImpliedRole ImpliedRole `json:"implies"` 267 } 268 269 type RoleInferenceRule struct { 270 RoleInference RoleInference `json:"role_inference"` 271 Links map[string]any `json:"links"` 272 } 273 274 func (r impliedRoleResult) Extract() (*RoleInferenceRule, error) { 275 var s = &RoleInferenceRule{} 276 err := r.ExtractInto(s) 277 return s, err 278 } 279 280 type ListImpliedRolesResult struct { 281 gophercloud.Result 282 } 283 284 type ImpliedRoleObject struct { 285 // ID contains the ID of the role in an implied_role object. 286 ID string `json:"id,omitempty"` 287 // Name contains the name of role in an implied_role. 288 Name string `json:"name,omitempty"` 289 // Name contains the name of role in an implied_role. 290 Description string `json:"description,omitempty"` 291 // Links contains referencing links to the implied_role. 292 Links map[string]any `json:"links"` 293 } 294 295 type PriorRoleObject struct { 296 // ID contains the ID of the role in an implied_role object. 297 ID string `json:"id,omitempty"` 298 // Name contains the name of role in an implied_role. 299 Name string `json:"name,omitempty"` 300 // Name contains the name of role in an implied_role. 301 Description string `json:"description,omitempty"` 302 // Links contains referencing links to the implied_role. 303 Links map[string]any `json:"links"` 304 } 305 type RoleInferenceRules struct { 306 // PriorRole is the role object that implies a list of implied_role objects. 307 PriorRole PriorRoleObject `json:"prior_role"` 308 // Implies is an array of implied_role objects implied by a prior_role object. 309 ImpliedRoles []ImpliedRoleObject `json:"implies"` 310 } 311 312 type RoleInferenceRuleList struct { 313 RoleInferenceRuleList []RoleInferenceRules `json:"role_inferences"` 314 Links map[string]any `json:"links"` 315 } 316 317 func (r ListImpliedRolesResult) Extract() (*RoleInferenceRuleList, error) { 318 var s = &RoleInferenceRuleList{} 319 err := r.ExtractInto(s) 320 return s, err 321 } 322 323 type DeleteImpliedRoleResult struct { 324 gophercloud.ErrResult 325 }