requests.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package roles
  2. import (
  3. "net/url"
  4. "strings"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  7. )
  8. // ListOptsBuilder allows extensions to add additional parameters to
  9. // the List request
  10. type ListOptsBuilder interface {
  11. ToRoleListQuery() (string, error)
  12. }
  13. // ListOpts provides options to filter the List results.
  14. type ListOpts struct {
  15. // DomainID filters the response by a domain ID.
  16. DomainID string `q:"domain_id"`
  17. // Name filters the response by role name.
  18. Name string `q:"name"`
  19. // Filters filters the response by custom filters such as
  20. // 'name__contains=foo'
  21. Filters map[string]string `q:"-"`
  22. }
  23. // ToRoleListQuery formats a ListOpts into a query string.
  24. func (opts ListOpts) ToRoleListQuery() (string, error) {
  25. q, err := gophercloud.BuildQueryString(opts)
  26. if err != nil {
  27. return "", err
  28. }
  29. params := q.Query()
  30. for k, v := range opts.Filters {
  31. i := strings.Index(k, "__")
  32. if i > 0 && i < len(k)-2 {
  33. params.Add(k, v)
  34. } else {
  35. return "", InvalidListFilter{FilterName: k}
  36. }
  37. }
  38. q = &url.URL{RawQuery: params.Encode()}
  39. return q.String(), err
  40. }
  41. // List enumerates the roles to which the current token has access.
  42. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  43. url := listURL(client)
  44. if opts != nil {
  45. query, err := opts.ToRoleListQuery()
  46. if err != nil {
  47. return pagination.Pager{Err: err}
  48. }
  49. url += query
  50. }
  51. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  52. return RolePage{pagination.LinkedPageBase{PageResult: r}}
  53. })
  54. }
  55. // Get retrieves details on a single role, by ID.
  56. func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
  57. _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
  58. return
  59. }
  60. // CreateOptsBuilder allows extensions to add additional parameters to
  61. // the Create request.
  62. type CreateOptsBuilder interface {
  63. ToRoleCreateMap() (map[string]interface{}, error)
  64. }
  65. // CreateOpts provides options used to create a role.
  66. type CreateOpts struct {
  67. // Name is the name of the new role.
  68. Name string `json:"name" required:"true"`
  69. // DomainID is the ID of the domain the role belongs to.
  70. DomainID string `json:"domain_id,omitempty"`
  71. // Extra is free-form extra key/value pairs to describe the role.
  72. Extra map[string]interface{} `json:"-"`
  73. }
  74. // ToRoleCreateMap formats a CreateOpts into a create request.
  75. func (opts CreateOpts) ToRoleCreateMap() (map[string]interface{}, error) {
  76. b, err := gophercloud.BuildRequestBody(opts, "role")
  77. if err != nil {
  78. return nil, err
  79. }
  80. if opts.Extra != nil {
  81. if v, ok := b["role"].(map[string]interface{}); ok {
  82. for key, value := range opts.Extra {
  83. v[key] = value
  84. }
  85. }
  86. }
  87. return b, nil
  88. }
  89. // Create creates a new Role.
  90. func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  91. b, err := opts.ToRoleCreateMap()
  92. if err != nil {
  93. r.Err = err
  94. return
  95. }
  96. _, r.Err = client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
  97. OkCodes: []int{201},
  98. })
  99. return
  100. }
  101. // UpdateOptsBuilder allows extensions to add additional parameters to
  102. // the Update request.
  103. type UpdateOptsBuilder interface {
  104. ToRoleUpdateMap() (map[string]interface{}, error)
  105. }
  106. // UpdateOpts provides options for updating a role.
  107. type UpdateOpts struct {
  108. // Name is the name of the new role.
  109. Name string `json:"name,omitempty"`
  110. // Extra is free-form extra key/value pairs to describe the role.
  111. Extra map[string]interface{} `json:"-"`
  112. }
  113. // ToRoleUpdateMap formats a UpdateOpts into an update request.
  114. func (opts UpdateOpts) ToRoleUpdateMap() (map[string]interface{}, error) {
  115. b, err := gophercloud.BuildRequestBody(opts, "role")
  116. if err != nil {
  117. return nil, err
  118. }
  119. if opts.Extra != nil {
  120. if v, ok := b["role"].(map[string]interface{}); ok {
  121. for key, value := range opts.Extra {
  122. v[key] = value
  123. }
  124. }
  125. }
  126. return b, nil
  127. }
  128. // Update updates an existing Role.
  129. func Update(client *gophercloud.ServiceClient, roleID string, opts UpdateOptsBuilder) (r UpdateResult) {
  130. b, err := opts.ToRoleUpdateMap()
  131. if err != nil {
  132. r.Err = err
  133. return
  134. }
  135. _, r.Err = client.Patch(updateURL(client, roleID), &b, &r.Body, &gophercloud.RequestOpts{
  136. OkCodes: []int{200},
  137. })
  138. return
  139. }
  140. // Delete deletes a role.
  141. func Delete(client *gophercloud.ServiceClient, roleID string) (r DeleteResult) {
  142. _, r.Err = client.Delete(deleteURL(client, roleID), nil)
  143. return
  144. }
  145. // ListAssignmentsOptsBuilder allows extensions to add additional parameters to
  146. // the ListAssignments request.
  147. type ListAssignmentsOptsBuilder interface {
  148. ToRolesListAssignmentsQuery() (string, error)
  149. }
  150. // ListAssignmentsOpts allows you to query the ListAssignments method.
  151. // Specify one of or a combination of GroupId, RoleId, ScopeDomainId,
  152. // ScopeProjectId, and/or UserId to search for roles assigned to corresponding
  153. // entities.
  154. type ListAssignmentsOpts struct {
  155. // GroupID is the group ID to query.
  156. GroupID string `q:"group.id"`
  157. // RoleID is the specific role to query assignments to.
  158. RoleID string `q:"role.id"`
  159. // ScopeDomainID filters the results by the given domain ID.
  160. ScopeDomainID string `q:"scope.domain.id"`
  161. // ScopeProjectID filters the results by the given Project ID.
  162. ScopeProjectID string `q:"scope.project.id"`
  163. // UserID filterst he results by the given User ID.
  164. UserID string `q:"user.id"`
  165. // Effective lists effective assignments at the user, project, and domain
  166. // level, allowing for the effects of group membership.
  167. Effective *bool `q:"effective"`
  168. }
  169. // ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string.
  170. func (opts ListAssignmentsOpts) ToRolesListAssignmentsQuery() (string, error) {
  171. q, err := gophercloud.BuildQueryString(opts)
  172. return q.String(), err
  173. }
  174. // ListAssignments enumerates the roles assigned to a specified resource.
  175. func ListAssignments(client *gophercloud.ServiceClient, opts ListAssignmentsOptsBuilder) pagination.Pager {
  176. url := listAssignmentsURL(client)
  177. if opts != nil {
  178. query, err := opts.ToRolesListAssignmentsQuery()
  179. if err != nil {
  180. return pagination.Pager{Err: err}
  181. }
  182. url += query
  183. }
  184. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  185. return RoleAssignmentPage{pagination.LinkedPageBase{PageResult: r}}
  186. })
  187. }
  188. // ListAssignmentsOnResourceOpts provides options to list role assignments
  189. // for a user/group on a project/domain
  190. type ListAssignmentsOnResourceOpts struct {
  191. // UserID is the ID of a user to assign a role
  192. // Note: exactly one of UserID or GroupID must be provided
  193. UserID string `xor:"GroupID"`
  194. // GroupID is the ID of a group to assign a role
  195. // Note: exactly one of UserID or GroupID must be provided
  196. GroupID string `xor:"UserID"`
  197. // ProjectID is the ID of a project to assign a role on
  198. // Note: exactly one of ProjectID or DomainID must be provided
  199. ProjectID string `xor:"DomainID"`
  200. // DomainID is the ID of a domain to assign a role on
  201. // Note: exactly one of ProjectID or DomainID must be provided
  202. DomainID string `xor:"ProjectID"`
  203. }
  204. // AssignOpts provides options to assign a role
  205. type AssignOpts struct {
  206. // UserID is the ID of a user to assign a role
  207. // Note: exactly one of UserID or GroupID must be provided
  208. UserID string `xor:"GroupID"`
  209. // GroupID is the ID of a group to assign a role
  210. // Note: exactly one of UserID or GroupID must be provided
  211. GroupID string `xor:"UserID"`
  212. // ProjectID is the ID of a project to assign a role on
  213. // Note: exactly one of ProjectID or DomainID must be provided
  214. ProjectID string `xor:"DomainID"`
  215. // DomainID is the ID of a domain to assign a role on
  216. // Note: exactly one of ProjectID or DomainID must be provided
  217. DomainID string `xor:"ProjectID"`
  218. }
  219. // UnassignOpts provides options to unassign a role
  220. type UnassignOpts struct {
  221. // UserID is the ID of a user to unassign a role
  222. // Note: exactly one of UserID or GroupID must be provided
  223. UserID string `xor:"GroupID"`
  224. // GroupID is the ID of a group to unassign a role
  225. // Note: exactly one of UserID or GroupID must be provided
  226. GroupID string `xor:"UserID"`
  227. // ProjectID is the ID of a project to unassign a role on
  228. // Note: exactly one of ProjectID or DomainID must be provided
  229. ProjectID string `xor:"DomainID"`
  230. // DomainID is the ID of a domain to unassign a role on
  231. // Note: exactly one of ProjectID or DomainID must be provided
  232. DomainID string `xor:"ProjectID"`
  233. }
  234. // ListAssignmentsOnResource is the operation responsible for listing role
  235. // assignments for a user/group on a project/domain.
  236. func ListAssignmentsOnResource(client *gophercloud.ServiceClient, opts ListAssignmentsOnResourceOpts) pagination.Pager {
  237. // Check xor conditions
  238. _, err := gophercloud.BuildRequestBody(opts, "")
  239. if err != nil {
  240. return pagination.Pager{Err: err}
  241. }
  242. // Get corresponding URL
  243. var targetID string
  244. var targetType string
  245. if opts.ProjectID != "" {
  246. targetID = opts.ProjectID
  247. targetType = "projects"
  248. } else {
  249. targetID = opts.DomainID
  250. targetType = "domains"
  251. }
  252. var actorID string
  253. var actorType string
  254. if opts.UserID != "" {
  255. actorID = opts.UserID
  256. actorType = "users"
  257. } else {
  258. actorID = opts.GroupID
  259. actorType = "groups"
  260. }
  261. url := listAssignmentsOnResourceURL(client, targetType, targetID, actorType, actorID)
  262. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  263. return RolePage{pagination.LinkedPageBase{PageResult: r}}
  264. })
  265. }
  266. // Assign is the operation responsible for assigning a role
  267. // to a user/group on a project/domain.
  268. func Assign(client *gophercloud.ServiceClient, roleID string, opts AssignOpts) (r AssignmentResult) {
  269. // Check xor conditions
  270. _, err := gophercloud.BuildRequestBody(opts, "")
  271. if err != nil {
  272. r.Err = err
  273. return
  274. }
  275. // Get corresponding URL
  276. var targetID string
  277. var targetType string
  278. if opts.ProjectID != "" {
  279. targetID = opts.ProjectID
  280. targetType = "projects"
  281. } else {
  282. targetID = opts.DomainID
  283. targetType = "domains"
  284. }
  285. var actorID string
  286. var actorType string
  287. if opts.UserID != "" {
  288. actorID = opts.UserID
  289. actorType = "users"
  290. } else {
  291. actorID = opts.GroupID
  292. actorType = "groups"
  293. }
  294. _, r.Err = client.Put(assignURL(client, targetType, targetID, actorType, actorID, roleID), nil, nil, &gophercloud.RequestOpts{
  295. OkCodes: []int{204},
  296. })
  297. return
  298. }
  299. // Unassign is the operation responsible for unassigning a role
  300. // from a user/group on a project/domain.
  301. func Unassign(client *gophercloud.ServiceClient, roleID string, opts UnassignOpts) (r UnassignmentResult) {
  302. // Check xor conditions
  303. _, err := gophercloud.BuildRequestBody(opts, "")
  304. if err != nil {
  305. r.Err = err
  306. return
  307. }
  308. // Get corresponding URL
  309. var targetID string
  310. var targetType string
  311. if opts.ProjectID != "" {
  312. targetID = opts.ProjectID
  313. targetType = "projects"
  314. } else {
  315. targetID = opts.DomainID
  316. targetType = "domains"
  317. }
  318. var actorID string
  319. var actorType string
  320. if opts.UserID != "" {
  321. actorID = opts.UserID
  322. actorType = "users"
  323. } else {
  324. actorID = opts.GroupID
  325. actorType = "groups"
  326. }
  327. _, r.Err = client.Delete(assignURL(client, targetType, targetID, actorType, actorID, roleID), &gophercloud.RequestOpts{
  328. OkCodes: []int{204},
  329. })
  330. return
  331. }