github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/roles/results.go (about) 1 package roles 2 3 import ( 4 "encoding/json" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/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]interface{} `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]interface{} `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]interface{} `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 interface{} 46 err := json.Unmarshal(b, &result) 47 if err != nil { 48 return err 49 } 50 if resultMap, ok := result.(map[string]interface{}); 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 }