results.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package networks
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  5. )
  6. type commonResult struct {
  7. gophercloud.Result
  8. }
  9. // Extract is a function that accepts a result and extracts a network resource.
  10. func (r commonResult) Extract() (*Network, error) {
  11. var s Network
  12. err := r.ExtractInto(&s)
  13. return &s, err
  14. }
  15. func (r commonResult) ExtractInto(v interface{}) error {
  16. return r.Result.ExtractIntoStructPtr(v, "network")
  17. }
  18. // CreateResult represents the result of a create operation. Call its Extract
  19. // method to interpret it as a Network.
  20. type CreateResult struct {
  21. commonResult
  22. }
  23. // GetResult represents the result of a get operation. Call its Extract
  24. // method to interpret it as a Network.
  25. type GetResult struct {
  26. commonResult
  27. }
  28. // UpdateResult represents the result of an update operation. Call its Extract
  29. // method to interpret it as a Network.
  30. type UpdateResult struct {
  31. commonResult
  32. }
  33. // DeleteResult represents the result of a delete operation. Call its
  34. // ExtractErr method to determine if the request succeeded or failed.
  35. type DeleteResult struct {
  36. gophercloud.ErrResult
  37. }
  38. // Network represents, well, a network.
  39. type Network struct {
  40. // UUID for the network
  41. ID string `json:"id"`
  42. // Human-readable name for the network. Might not be unique.
  43. Name string `json:"name"`
  44. // The administrative state of network. If false (down), the network does not
  45. // forward packets.
  46. AdminStateUp bool `json:"admin_state_up"`
  47. // Indicates whether network is currently operational. Possible values include
  48. // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional
  49. // values.
  50. Status string `json:"status"`
  51. // Subnets associated with this network.
  52. Subnets []string `json:"subnets"`
  53. // TenantID is the project owner of the network.
  54. TenantID string `json:"tenant_id"`
  55. // ProjectID is the project owner of the network.
  56. ProjectID string `json:"project_id"`
  57. // Specifies whether the network resource can be accessed by any tenant.
  58. Shared bool `json:"shared"`
  59. // Availability zone hints groups network nodes that run services like DHCP, L3, FW, and others.
  60. // Used to make network resources highly available.
  61. AvailabilityZoneHints []string `json:"availability_zone_hints"`
  62. }
  63. // NetworkPage is the page returned by a pager when traversing over a
  64. // collection of networks.
  65. type NetworkPage struct {
  66. pagination.LinkedPageBase
  67. }
  68. // NextPageURL is invoked when a paginated collection of networks has reached
  69. // the end of a page and the pager seeks to traverse over a new one. In order
  70. // to do this, it needs to construct the next page's URL.
  71. func (r NetworkPage) NextPageURL() (string, error) {
  72. var s struct {
  73. Links []gophercloud.Link `json:"networks_links"`
  74. }
  75. err := r.ExtractInto(&s)
  76. if err != nil {
  77. return "", err
  78. }
  79. return gophercloud.ExtractNextURL(s.Links)
  80. }
  81. // IsEmpty checks whether a NetworkPage struct is empty.
  82. func (r NetworkPage) IsEmpty() (bool, error) {
  83. is, err := ExtractNetworks(r)
  84. return len(is) == 0, err
  85. }
  86. // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
  87. // and extracts the elements into a slice of Network structs. In other words,
  88. // a generic collection is mapped into a relevant slice.
  89. func ExtractNetworks(r pagination.Page) ([]Network, error) {
  90. var s []Network
  91. err := ExtractNetworksInto(r, &s)
  92. return s, err
  93. }
  94. func ExtractNetworksInto(r pagination.Page, v interface{}) error {
  95. return r.(NetworkPage).Result.ExtractIntoSlicePtr(v, "networks")
  96. }