results.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package messages
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  5. )
  6. // commonResult is the response of a base result.
  7. type commonResult struct {
  8. gophercloud.Result
  9. }
  10. // CreateResult is the response of a Create operations.
  11. type CreateResult struct {
  12. gophercloud.Result
  13. }
  14. // MessagePage contains a single page of all clusters from a ListDetails call.
  15. type MessagePage struct {
  16. pagination.LinkedPageBase
  17. }
  18. // DeleteResult is the result from a Delete operation. Call its ExtractErr
  19. // method to determine if the call succeeded or failed.
  20. type DeleteResult struct {
  21. gophercloud.ErrResult
  22. }
  23. // CreateResult is the response of a Create operations.
  24. type PopResult struct {
  25. gophercloud.Result
  26. }
  27. // GetMessagesResult is the response of a GetMessages operations.
  28. type GetMessagesResult struct {
  29. gophercloud.Result
  30. }
  31. // GetResult is the response of a Get operations.
  32. type GetResult struct {
  33. gophercloud.Result
  34. }
  35. // Message represents a message on a queue.
  36. type Message struct {
  37. Body map[string]interface{} `json:"body"`
  38. Age int `json:"age"`
  39. Href string `json:"href"`
  40. ID string `json:"id"`
  41. TTL int `json:"ttl"`
  42. Checksum string `json:"checksum"`
  43. }
  44. // PopMessage represents a message returned from PopMessages.
  45. type PopMessage struct {
  46. Body map[string]interface{} `json:"body"`
  47. Age int `json:"age"`
  48. ID string `json:"id"`
  49. TTL int `json:"ttl"`
  50. ClaimCount int `json:"claim_count"`
  51. ClaimID string `json:"claim_id"`
  52. }
  53. // ResourceList represents the result of creating a message.
  54. type ResourceList struct {
  55. Resources []string `json:"resources"`
  56. }
  57. // Extract interprets any CreateResult as a ResourceList.
  58. func (r CreateResult) Extract() (ResourceList, error) {
  59. var s ResourceList
  60. err := r.ExtractInto(&s)
  61. return s, err
  62. }
  63. // Extract interprets any PopResult as a list of PopMessage.
  64. func (r PopResult) Extract() ([]PopMessage, error) {
  65. var s struct {
  66. PopMessages []PopMessage `json:"messages"`
  67. }
  68. err := r.ExtractInto(&s)
  69. return s.PopMessages, err
  70. }
  71. // Extract interprets any GetMessagesResult as a list of Message.
  72. func (r GetMessagesResult) Extract() ([]Message, error) {
  73. var s struct {
  74. Messages []Message `json:"messages"`
  75. }
  76. err := r.ExtractInto(&s)
  77. return s.Messages, err
  78. }
  79. // Extract interprets any GetResult as a Message.
  80. func (r GetResult) Extract() (Message, error) {
  81. var s Message
  82. err := r.ExtractInto(&s)
  83. return s, err
  84. }
  85. // ExtractMessage extracts message into a list of Message.
  86. func ExtractMessages(r pagination.Page) ([]Message, error) {
  87. var s struct {
  88. Messages []Message `json:"messages"`
  89. }
  90. err := (r.(MessagePage)).ExtractInto(&s)
  91. return s.Messages, err
  92. }
  93. // IsEmpty determines if a MessagePage contains any results.
  94. func (r MessagePage) IsEmpty() (bool, error) {
  95. s, err := ExtractMessages(r)
  96. return len(s) == 0, err
  97. }
  98. // NextPageURL uses the response's embedded link reference to navigate to the
  99. // next page of results.
  100. func (r MessagePage) NextPageURL() (string, error) {
  101. var s struct {
  102. Links []gophercloud.Link `json:"links"`
  103. }
  104. err := r.ExtractInto(&s)
  105. if err != nil {
  106. return "", err
  107. }
  108. next, err := gophercloud.ExtractNextURL(s.Links)
  109. if err != nil {
  110. return "", err
  111. }
  112. return nextPageURL(r.URL.String(), next)
  113. }