results.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package regions
  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. // Region helps manage related users.
  9. type Region struct {
  10. // Description describes the region purpose.
  11. Description string `json:"description"`
  12. // ID is the unique ID of the region.
  13. ID string `json:"id"`
  14. // Extra is a collection of miscellaneous key/values.
  15. Extra map[string]interface{} `json:"-"`
  16. // Links contains referencing links to the region.
  17. Links map[string]interface{} `json:"links"`
  18. // ParentRegionID is the ID of the parent region.
  19. ParentRegionID string `json:"parent_region_id"`
  20. }
  21. func (r *Region) UnmarshalJSON(b []byte) error {
  22. type tmp Region
  23. var s struct {
  24. tmp
  25. Extra map[string]interface{} `json:"extra"`
  26. }
  27. err := json.Unmarshal(b, &s)
  28. if err != nil {
  29. return err
  30. }
  31. *r = Region(s.tmp)
  32. // Collect other fields and bundle them into Extra
  33. // but only if a field titled "extra" wasn't sent.
  34. if s.Extra != nil {
  35. r.Extra = s.Extra
  36. } else {
  37. var result interface{}
  38. err := json.Unmarshal(b, &result)
  39. if err != nil {
  40. return err
  41. }
  42. if resultMap, ok := result.(map[string]interface{}); ok {
  43. r.Extra = internal.RemainingKeys(Region{}, resultMap)
  44. }
  45. }
  46. return err
  47. }
  48. type regionResult struct {
  49. gophercloud.Result
  50. }
  51. // GetResult is the response from a Get operation. Call its Extract method
  52. // to interpret it as a Region.
  53. type GetResult struct {
  54. regionResult
  55. }
  56. // CreateResult is the response from a Create operation. Call its Extract method
  57. // to interpret it as a Region.
  58. type CreateResult struct {
  59. regionResult
  60. }
  61. // UpdateResult is the response from an Update operation. Call its Extract
  62. // method to interpret it as a Region.
  63. type UpdateResult struct {
  64. regionResult
  65. }
  66. // DeleteResult is the response from a Delete operation. Call its ExtractErr to
  67. // determine if the request succeeded or failed.
  68. type DeleteResult struct {
  69. gophercloud.ErrResult
  70. }
  71. // RegionPage is a single page of Region results.
  72. type RegionPage struct {
  73. pagination.LinkedPageBase
  74. }
  75. // IsEmpty determines whether or not a page of Regions contains any results.
  76. func (r RegionPage) IsEmpty() (bool, error) {
  77. regions, err := ExtractRegions(r)
  78. return len(regions) == 0, err
  79. }
  80. // NextPageURL extracts the "next" link from the links section of the result.
  81. func (r RegionPage) NextPageURL() (string, error) {
  82. var s struct {
  83. Links struct {
  84. Next string `json:"next"`
  85. Previous string `json:"previous"`
  86. } `json:"links"`
  87. }
  88. err := r.ExtractInto(&s)
  89. if err != nil {
  90. return "", err
  91. }
  92. return s.Links.Next, err
  93. }
  94. // ExtractRegions returns a slice of Regions contained in a single page of results.
  95. func ExtractRegions(r pagination.Page) ([]Region, error) {
  96. var s struct {
  97. Regions []Region `json:"regions"`
  98. }
  99. err := (r.(RegionPage)).ExtractInto(&s)
  100. return s.Regions, err
  101. }
  102. // Extract interprets any region results as a Region.
  103. func (r regionResult) Extract() (*Region, error) {
  104. var s struct {
  105. Region *Region `json:"region"`
  106. }
  107. err := r.ExtractInto(&s)
  108. return s.Region, err
  109. }