requests.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package policies
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  5. )
  6. const policyTypeMaxLength = 255
  7. // ListOptsBuilder allows extensions to add additional parameters to
  8. // the List request
  9. type ListOptsBuilder interface {
  10. ToPolicyListQuery() (string, error)
  11. }
  12. // ListOpts provides options to filter the List results.
  13. type ListOpts struct {
  14. // Type filters the response by MIME media type
  15. // of the serialized policy blob.
  16. Type string `q:"type"`
  17. }
  18. // ToPolicyListQuery formats a ListOpts into a query string.
  19. func (opts ListOpts) ToPolicyListQuery() (string, error) {
  20. q, err := gophercloud.BuildQueryString(opts)
  21. return q.String(), err
  22. }
  23. // List enumerates the policies to which the current token has access.
  24. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  25. url := listURL(client)
  26. if opts != nil {
  27. query, err := opts.ToPolicyListQuery()
  28. if err != nil {
  29. return pagination.Pager{Err: err}
  30. }
  31. url += query
  32. }
  33. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  34. return PolicyPage{pagination.LinkedPageBase{PageResult: r}}
  35. })
  36. }
  37. // CreateOptsBuilder allows extensions to add additional parameters to
  38. // the Create request.
  39. type CreateOptsBuilder interface {
  40. ToPolicyCreateMap() (map[string]interface{}, error)
  41. }
  42. // CreateOpts provides options used to create a policy.
  43. type CreateOpts struct {
  44. // Type is the MIME media type of the serialized policy blob.
  45. Type string `json:"type" required:"true"`
  46. // Blob is the policy rule as a serialized blob.
  47. Blob []byte `json:"-" required:"true"`
  48. // Extra is free-form extra key/value pairs to describe the policy.
  49. Extra map[string]interface{} `json:"-"`
  50. }
  51. // ToPolicyCreateMap formats a CreateOpts into a create request.
  52. func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
  53. if len(opts.Type) > policyTypeMaxLength {
  54. return nil, StringFieldLengthExceedsLimit{
  55. Field: "type",
  56. Limit: policyTypeMaxLength,
  57. }
  58. }
  59. b, err := gophercloud.BuildRequestBody(opts, "policy")
  60. if err != nil {
  61. return nil, err
  62. }
  63. if v, ok := b["policy"].(map[string]interface{}); ok {
  64. v["blob"] = string(opts.Blob)
  65. if opts.Extra != nil {
  66. for key, value := range opts.Extra {
  67. v[key] = value
  68. }
  69. }
  70. }
  71. return b, nil
  72. }
  73. // Create creates a new Policy.
  74. func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  75. b, err := opts.ToPolicyCreateMap()
  76. if err != nil {
  77. r.Err = err
  78. return
  79. }
  80. _, r.Err = client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
  81. OkCodes: []int{201},
  82. })
  83. return
  84. }
  85. // Get retrieves details on a single policy, by ID.
  86. func Get(client *gophercloud.ServiceClient, policyID string) (r GetResult) {
  87. _, r.Err = client.Get(getURL(client, policyID), &r.Body, nil)
  88. return
  89. }
  90. // UpdateOptsBuilder allows extensions to add additional parameters to
  91. // the Update request.
  92. type UpdateOptsBuilder interface {
  93. ToPolicyUpdateMap() (map[string]interface{}, error)
  94. }
  95. // UpdateOpts provides options for updating a policy.
  96. type UpdateOpts struct {
  97. // Type is the MIME media type of the serialized policy blob.
  98. Type string `json:"type,omitempty"`
  99. // Blob is the policy rule as a serialized blob.
  100. Blob []byte `json:"-"`
  101. // Extra is free-form extra key/value pairs to describe the policy.
  102. Extra map[string]interface{} `json:"-"`
  103. }
  104. // ToPolicyUpdateMap formats a UpdateOpts into an update request.
  105. func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
  106. if len(opts.Type) > policyTypeMaxLength {
  107. return nil, StringFieldLengthExceedsLimit{
  108. Field: "type",
  109. Limit: policyTypeMaxLength,
  110. }
  111. }
  112. b, err := gophercloud.BuildRequestBody(opts, "policy")
  113. if err != nil {
  114. return nil, err
  115. }
  116. if v, ok := b["policy"].(map[string]interface{}); ok {
  117. if len(opts.Blob) != 0 {
  118. v["blob"] = string(opts.Blob)
  119. }
  120. if opts.Extra != nil {
  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, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
  130. b, err := opts.ToPolicyUpdateMap()
  131. if err != nil {
  132. r.Err = err
  133. return
  134. }
  135. _, r.Err = client.Patch(updateURL(client, policyID), &b, &r.Body, &gophercloud.RequestOpts{
  136. OkCodes: []int{200},
  137. })
  138. return
  139. }
  140. // Delete deletes a policy.
  141. func Delete(client *gophercloud.ServiceClient, policyID string) (r DeleteResult) {
  142. _, r.Err = client.Delete(deleteURL(client, policyID), nil)
  143. return
  144. }