results.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package queues
  2. import (
  3. "encoding/json"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud/internal"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  7. )
  8. // commonResult is the response of a base result.
  9. type commonResult struct {
  10. gophercloud.Result
  11. }
  12. // QueuePage contains a single page of all queues from a List operation.
  13. type QueuePage struct {
  14. pagination.LinkedPageBase
  15. }
  16. // CreateResult is the response of a Create operation.
  17. type CreateResult struct {
  18. gophercloud.ErrResult
  19. }
  20. // UpdateResult is the response of a Update operation.
  21. type UpdateResult struct {
  22. commonResult
  23. }
  24. // GetResult is the response of a Get operation.
  25. type GetResult struct {
  26. commonResult
  27. }
  28. // StatResult contains the result of a Share operation.
  29. type StatResult struct {
  30. gophercloud.Result
  31. }
  32. // DeleteResult is the result from a Delete operation. Call its ExtractErr
  33. // method to determine if the call succeeded or failed.
  34. type DeleteResult struct {
  35. gophercloud.ErrResult
  36. }
  37. // ShareResult contains the result of a Share operation.
  38. type ShareResult struct {
  39. gophercloud.Result
  40. }
  41. // PurgeResult is the response of a Purge operation.
  42. type PurgeResult struct {
  43. gophercloud.ErrResult
  44. }
  45. // Queue represents a messaging queue.
  46. type Queue struct {
  47. Href string `json:"href"`
  48. Methods []string `json:"methods"`
  49. Name string `json:"name"`
  50. Paths []string `json:"paths"`
  51. ResourceTypes []string `json:"resource_types"`
  52. Metadata QueueDetails `json:"metadata"`
  53. }
  54. // QueueDetails represents the metadata of a queue.
  55. type QueueDetails struct {
  56. // The queue the message will be moved to when the message can’t
  57. // be processed successfully after the max claim count is met.
  58. DeadLetterQueue string `json:"_dead_letter_queue"`
  59. // The TTL setting for messages when moved to dead letter queue.
  60. DeadLetterQueueMessageTTL int `json:"_dead_letter_queue_messages_ttl"`
  61. // The delay of messages defined for the queue.
  62. DefaultMessageDelay int `json:"_default_message_delay"`
  63. // The default TTL of messages defined for the queue.
  64. DefaultMessageTTL int `json:"_default_message_ttl"`
  65. // Extra is a collection of miscellaneous key/values.
  66. Extra map[string]interface{} `json:"-"`
  67. // The max number the message can be claimed from the queue.
  68. MaxClaimCount int `json:"_max_claim_count"`
  69. // The max post size of messages defined for the queue.
  70. MaxMessagesPostSize int `json:"_max_messages_post_size"`
  71. // The flavor defined for the queue.
  72. Flavor string `json:"flavor"`
  73. }
  74. // Stats represents a stats response.
  75. type Stats struct {
  76. // Number of Claimed messages for a queue
  77. Claimed int `json:"claimed"`
  78. // Total Messages for a queue
  79. Total int `json:"total"`
  80. // Number of free messages
  81. Free int `json:"free"`
  82. }
  83. // QueueShare represents a share response.
  84. type QueueShare struct {
  85. Project string `json:"project"`
  86. Paths []string `json:"paths"`
  87. Expires string `json:"expires"`
  88. Methods []string `json:"methods"`
  89. Signature string `json:"signature"`
  90. }
  91. // Extract interprets any commonResult as a Queue.
  92. func (r commonResult) Extract() (QueueDetails, error) {
  93. var s QueueDetails
  94. err := r.ExtractInto(&s)
  95. return s, err
  96. }
  97. // Extract interprets any StatResult as a Stats.
  98. func (r StatResult) Extract() (Stats, error) {
  99. var s struct {
  100. Stats Stats `json:"messages"`
  101. }
  102. err := r.ExtractInto(&s)
  103. return s.Stats, err
  104. }
  105. // Extract interprets any ShareResult as a QueueShare.
  106. func (r ShareResult) Extract() (QueueShare, error) {
  107. var s QueueShare
  108. err := r.ExtractInto(&s)
  109. return s, err
  110. }
  111. // ExtractQueues interprets the results of a single page from a
  112. // List() call, producing a map of queues.
  113. func ExtractQueues(r pagination.Page) ([]Queue, error) {
  114. var s struct {
  115. Queues []Queue `json:"queues"`
  116. }
  117. err := (r.(QueuePage)).ExtractInto(&s)
  118. return s.Queues, err
  119. }
  120. // IsEmpty determines if a QueuesPage contains any results.
  121. func (r QueuePage) IsEmpty() (bool, error) {
  122. s, err := ExtractQueues(r)
  123. return len(s) == 0, err
  124. }
  125. // NextPageURL uses the response's embedded link reference to navigate to the
  126. // next page of results.
  127. func (r QueuePage) NextPageURL() (string, error) {
  128. var s struct {
  129. Links []gophercloud.Link `json:"links"`
  130. }
  131. err := r.ExtractInto(&s)
  132. if err != nil {
  133. return "", err
  134. }
  135. next, err := gophercloud.ExtractNextURL(s.Links)
  136. if err != nil {
  137. return "", err
  138. }
  139. return nextPageURL(r.URL.String(), next)
  140. }
  141. func (r *QueueDetails) UnmarshalJSON(b []byte) error {
  142. type tmp QueueDetails
  143. var s struct {
  144. tmp
  145. Extra map[string]interface{} `json:"extra"`
  146. }
  147. err := json.Unmarshal(b, &s)
  148. if err != nil {
  149. return err
  150. }
  151. *r = QueueDetails(s.tmp)
  152. // Collect other fields and bundle them into Extra
  153. // but only if a field titled "extra" wasn't sent.
  154. if s.Extra != nil {
  155. r.Extra = s.Extra
  156. } else {
  157. var result interface{}
  158. err := json.Unmarshal(b, &result)
  159. if err != nil {
  160. return err
  161. }
  162. if resultMap, ok := result.(map[string]interface{}); ok {
  163. r.Extra = internal.RemainingKeys(QueueDetails{}, resultMap)
  164. }
  165. }
  166. return err
  167. }