github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/roles/results.go (about) 1 package roles 2 3 import ( 4 "encoding/json" 5 6 "github.com/opentelekomcloud/gophertelekomcloud" 7 "github.com/opentelekomcloud/gophertelekomcloud/internal" 8 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 9 ) 10 11 // Role grants permissions to a user. 12 type Role struct { 13 // DomainID is the domain ID the role belongs to. 14 DomainID string `json:"domain_id"` 15 16 // ID is the unique ID of the role. 17 ID string `json:"id"` 18 19 // Links contains referencing links to the role. 20 Links map[string]interface{} `json:"links"` 21 22 // Name is the role name 23 Name string `json:"name"` 24 25 // Extra is a collection of miscellaneous key/values. 26 Extra map[string]interface{} `json:"-"` 27 } 28 29 func (r *Role) UnmarshalJSON(b []byte) error { 30 type tmp Role 31 var s struct { 32 tmp 33 Extra map[string]interface{} `json:"extra"` 34 } 35 err := json.Unmarshal(b, &s) 36 if err != nil { 37 return err 38 } 39 *r = Role(s.tmp) 40 41 // Collect other fields and bundle them into Extra 42 // but only if a field titled "extra" wasn't sent. 43 if s.Extra != nil { 44 r.Extra = s.Extra 45 } else { 46 var result interface{} 47 err := json.Unmarshal(b, &result) 48 if err != nil { 49 return err 50 } 51 if resultMap, ok := result.(map[string]interface{}); ok { 52 r.Extra = internal.RemainingKeys(Role{}, resultMap) 53 } 54 } 55 56 return err 57 } 58 59 type roleResult struct { 60 golangsdk.Result 61 } 62 63 // GetResult is the response from a Get operation. Call its Extract method 64 // to interpret it as a Role. 65 type GetResult struct { 66 roleResult 67 } 68 69 // CreateResult is the response from a Create operation. Call its Extract method 70 // to interpret it as a Role 71 type CreateResult struct { 72 roleResult 73 } 74 75 // UpdateResult is the response from an Update operation. Call its Extract 76 // method to interpret it as a Role. 77 type UpdateResult struct { 78 roleResult 79 } 80 81 // DeleteResult is the response from a Delete operation. Call its ExtractErr to 82 // determine if the request succeeded or failed. 83 type DeleteResult struct { 84 golangsdk.ErrResult 85 } 86 87 // RolePage is a single page of Role results. 88 type RolePage struct { 89 pagination.LinkedPageBase 90 } 91 92 // IsEmpty determines whether or not a page of Roles contains any results. 93 func (r RolePage) IsEmpty() (bool, error) { 94 roles, err := ExtractRoles(r) 95 return len(roles) == 0, err 96 } 97 98 // NextPageURL extracts the "next" link from the links section of the result. 99 func (r RolePage) NextPageURL() (string, error) { 100 var s struct { 101 Links struct { 102 Next string `json:"next"` 103 Previous string `json:"previous"` 104 } `json:"links"` 105 } 106 err := r.ExtractInto(&s) 107 if err != nil { 108 return "", err 109 } 110 return s.Links.Next, err 111 } 112 113 // ExtractProjects returns a slice of Roles contained in a single page of 114 // results. 115 func ExtractRoles(r pagination.Page) ([]Role, error) { 116 var s struct { 117 Roles []Role `json:"roles"` 118 } 119 err := (r.(RolePage)).ExtractInto(&s) 120 return s.Roles, err 121 } 122 123 // Extract interprets any roleResults as a Role. 124 func (r roleResult) Extract() (*Role, error) { 125 var s struct { 126 Role *Role `json:"role"` 127 } 128 err := r.ExtractInto(&s) 129 return s.Role, err 130 } 131 132 // RoleAssignment is the result of a role assignments query. 133 type RoleAssignment struct { 134 Catalog string `json:"catalog"` 135 Description string `json:"description"` 136 DisplayName string `json:"display_name"` 137 ID string `json:"id"` 138 Name string `json:"name"` 139 Type string `json:"type"` 140 Policy Policy `json:"policy"` 141 } 142 143 type Policy struct { 144 Statement []Statement `json:"Statement"` 145 Version string `json:"Version"` 146 } 147 148 type Statement struct { 149 Action []string `json:"Action"` 150 Effect string `json:"Effect"` 151 } 152 153 // RoleAssignmentPage is a single page of RoleAssignments results. 154 type RoleAssignmentPage struct { 155 pagination.LinkedPageBase 156 } 157 158 // IsEmpty returns true if the RoleAssignmentPage contains no results. 159 func (r RoleAssignmentPage) IsEmpty() (bool, error) { 160 roleAssignments, err := ExtractRoleAssignments(r) 161 return len(roleAssignments) == 0, err 162 } 163 164 // NextPageURL uses the response's embedded link reference to navigate to 165 // the next page of results. 166 func (r RoleAssignmentPage) NextPageURL() (string, error) { 167 var s struct { 168 Links struct { 169 Next string `json:"next"` 170 } `json:"links"` 171 } 172 err := r.ExtractInto(&s) 173 return s.Links.Next, err 174 } 175 176 // ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection 177 // acquired from List. 178 func ExtractRoleAssignments(r pagination.Page) ([]RoleAssignment, error) { 179 var s struct { 180 RoleAssignments []RoleAssignment `json:"roles"` 181 } 182 err := (r.(RoleAssignmentPage)).ExtractInto(&s) 183 return s.RoleAssignments, err 184 } 185 186 // AssignmentResult represents the result of an assign operation. 187 // Call ExtractErr method to determine if the request succeeded or failed. 188 type AssignmentResult struct { 189 golangsdk.ErrResult 190 } 191 192 // UnassignmentResult represents the result of an unassign operation. 193 // Call ExtractErr method to determine if the request succeeded or failed. 194 type UnassignmentResult struct { 195 golangsdk.ErrResult 196 }