results.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package apiversions
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  4. )
  5. // APIVersion represents an API version for Neutron. It contains the status of
  6. // the API, and its unique ID.
  7. type APIVersion struct {
  8. Status string `son:"status"`
  9. ID string `json:"id"`
  10. }
  11. // APIVersionPage is the page returned by a pager when traversing over a
  12. // collection of API versions.
  13. type APIVersionPage struct {
  14. pagination.SinglePageBase
  15. }
  16. // IsEmpty checks whether an APIVersionPage struct is empty.
  17. func (r APIVersionPage) IsEmpty() (bool, error) {
  18. is, err := ExtractAPIVersions(r)
  19. return len(is) == 0, err
  20. }
  21. // ExtractAPIVersions takes a collection page, extracts all of the elements,
  22. // and returns them a slice of APIVersion structs. It is effectively a cast.
  23. func ExtractAPIVersions(r pagination.Page) ([]APIVersion, error) {
  24. var s struct {
  25. Versions []APIVersion `json:"versions"`
  26. }
  27. err := (r.(APIVersionPage)).ExtractInto(&s)
  28. return s.Versions, err
  29. }
  30. // APIVersionResource represents a generic API resource. It contains the name
  31. // of the resource and its plural collection name.
  32. type APIVersionResource struct {
  33. Name string `json:"name"`
  34. Collection string `json:"collection"`
  35. }
  36. // APIVersionResourcePage is a concrete type which embeds the common
  37. // SinglePageBase struct, and is used when traversing API versions collections.
  38. type APIVersionResourcePage struct {
  39. pagination.SinglePageBase
  40. }
  41. // IsEmpty is a concrete function which indicates whether an
  42. // APIVersionResourcePage is empty or not.
  43. func (r APIVersionResourcePage) IsEmpty() (bool, error) {
  44. is, err := ExtractVersionResources(r)
  45. return len(is) == 0, err
  46. }
  47. // ExtractVersionResources accepts a Page struct, specifically a
  48. // APIVersionResourcePage struct, and extracts the elements into a slice of
  49. // APIVersionResource structs. In other words, the collection is mapped into
  50. // a relevant slice.
  51. func ExtractVersionResources(r pagination.Page) ([]APIVersionResource, error) {
  52. var s struct {
  53. APIVersionResources []APIVersionResource `json:"resources"`
  54. }
  55. err := (r.(APIVersionResourcePage)).ExtractInto(&s)
  56. return s.APIVersionResources, err
  57. }