results.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package volumes
  2. import (
  3. "encoding/json"
  4. "time"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  7. )
  8. // Volume contains all the information associated with an OpenStack Volume.
  9. type Volume struct {
  10. // Current status of the volume.
  11. Status string `json:"status"`
  12. // Human-readable display name for the volume.
  13. Name string `json:"display_name"`
  14. // Instances onto which the volume is attached.
  15. Attachments []map[string]interface{} `json:"attachments"`
  16. // This parameter is no longer used.
  17. AvailabilityZone string `json:"availability_zone"`
  18. // Indicates whether this is a bootable volume.
  19. Bootable string `json:"bootable"`
  20. // The date when this volume was created.
  21. CreatedAt time.Time `json:"-"`
  22. // Human-readable description for the volume.
  23. Description string `json:"display_description"`
  24. // The type of volume to create, either SATA or SSD.
  25. VolumeType string `json:"volume_type"`
  26. // The ID of the snapshot from which the volume was created
  27. SnapshotID string `json:"snapshot_id"`
  28. // The ID of another block storage volume from which the current volume was created
  29. SourceVolID string `json:"source_volid"`
  30. // Arbitrary key-value pairs defined by the user.
  31. Metadata map[string]string `json:"metadata"`
  32. // Unique identifier for the volume.
  33. ID string `json:"id"`
  34. // Size of the volume in GB.
  35. Size int `json:"size"`
  36. }
  37. func (r *Volume) UnmarshalJSON(b []byte) error {
  38. type tmp Volume
  39. var s struct {
  40. tmp
  41. CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
  42. }
  43. err := json.Unmarshal(b, &s)
  44. if err != nil {
  45. return err
  46. }
  47. *r = Volume(s.tmp)
  48. r.CreatedAt = time.Time(s.CreatedAt)
  49. return err
  50. }
  51. // CreateResult contains the response body and error from a Create request.
  52. type CreateResult struct {
  53. commonResult
  54. }
  55. // GetResult contains the response body and error from a Get request.
  56. type GetResult struct {
  57. commonResult
  58. }
  59. // DeleteResult contains the response body and error from a Delete request.
  60. type DeleteResult struct {
  61. gophercloud.ErrResult
  62. }
  63. // VolumePage is a pagination.pager that is returned from a call to the List function.
  64. type VolumePage struct {
  65. pagination.SinglePageBase
  66. }
  67. // IsEmpty returns true if a VolumePage contains no Volumes.
  68. func (r VolumePage) IsEmpty() (bool, error) {
  69. volumes, err := ExtractVolumes(r)
  70. return len(volumes) == 0, err
  71. }
  72. // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
  73. func ExtractVolumes(r pagination.Page) ([]Volume, error) {
  74. var s struct {
  75. Volumes []Volume `json:"volumes"`
  76. }
  77. err := (r.(VolumePage)).ExtractInto(&s)
  78. return s.Volumes, err
  79. }
  80. // UpdateResult contains the response body and error from an Update request.
  81. type UpdateResult struct {
  82. commonResult
  83. }
  84. type commonResult struct {
  85. gophercloud.Result
  86. }
  87. // Extract will get the Volume object out of the commonResult object.
  88. func (r commonResult) Extract() (*Volume, error) {
  89. var s struct {
  90. Volume *Volume `json:"volume"`
  91. }
  92. err := r.ExtractInto(&s)
  93. return s.Volume, err
  94. }