results.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // Attachment represents a Volume Attachment record
  9. type Attachment struct {
  10. AttachedAt time.Time `json:"-"`
  11. AttachmentID string `json:"attachment_id"`
  12. Device string `json:"device"`
  13. HostName string `json:"host_name"`
  14. ID string `json:"id"`
  15. ServerID string `json:"server_id"`
  16. VolumeID string `json:"volume_id"`
  17. }
  18. // UnmarshalJSON is our unmarshalling helper
  19. func (r *Attachment) UnmarshalJSON(b []byte) error {
  20. type tmp Attachment
  21. var s struct {
  22. tmp
  23. AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
  24. }
  25. err := json.Unmarshal(b, &s)
  26. if err != nil {
  27. return err
  28. }
  29. *r = Attachment(s.tmp)
  30. r.AttachedAt = time.Time(s.AttachedAt)
  31. return err
  32. }
  33. // Volume contains all the information associated with an OpenStack Volume.
  34. type Volume struct {
  35. // Unique identifier for the volume.
  36. ID string `json:"id"`
  37. // Current status of the volume.
  38. Status string `json:"status"`
  39. // Size of the volume in GB.
  40. Size int `json:"size"`
  41. // AvailabilityZone is which availability zone the volume is in.
  42. AvailabilityZone string `json:"availability_zone"`
  43. // The date when this volume was created.
  44. CreatedAt time.Time `json:"-"`
  45. // The date when this volume was last updated
  46. UpdatedAt time.Time `json:"-"`
  47. // Instances onto which the volume is attached.
  48. Attachments []Attachment `json:"attachments"`
  49. // Human-readable display name for the volume.
  50. Name string `json:"name"`
  51. // Human-readable description for the volume.
  52. Description string `json:"description"`
  53. // The type of volume to create, either SATA or SSD.
  54. VolumeType string `json:"volume_type"`
  55. // The ID of the snapshot from which the volume was created
  56. SnapshotID string `json:"snapshot_id"`
  57. // The ID of another block storage volume from which the current volume was created
  58. SourceVolID string `json:"source_volid"`
  59. // Arbitrary key-value pairs defined by the user.
  60. Metadata map[string]string `json:"metadata"`
  61. // UserID is the id of the user who created the volume.
  62. UserID string `json:"user_id"`
  63. // Indicates whether this is a bootable volume.
  64. Bootable string `json:"bootable"`
  65. // Encrypted denotes if the volume is encrypted.
  66. Encrypted bool `json:"encrypted"`
  67. // ReplicationStatus is the status of replication.
  68. ReplicationStatus string `json:"replication_status"`
  69. // ConsistencyGroupID is the consistency group ID.
  70. ConsistencyGroupID string `json:"consistencygroup_id"`
  71. // Multiattach denotes if the volume is multi-attach capable.
  72. Multiattach bool `json:"multiattach"`
  73. }
  74. // UnmarshalJSON another unmarshalling function
  75. func (r *Volume) UnmarshalJSON(b []byte) error {
  76. type tmp Volume
  77. var s struct {
  78. tmp
  79. CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
  80. UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
  81. }
  82. err := json.Unmarshal(b, &s)
  83. if err != nil {
  84. return err
  85. }
  86. *r = Volume(s.tmp)
  87. r.CreatedAt = time.Time(s.CreatedAt)
  88. r.UpdatedAt = time.Time(s.UpdatedAt)
  89. return err
  90. }
  91. // VolumePage is a pagination.pager that is returned from a call to the List function.
  92. type VolumePage struct {
  93. pagination.LinkedPageBase
  94. }
  95. // IsEmpty returns true if a ListResult contains no Volumes.
  96. func (r VolumePage) IsEmpty() (bool, error) {
  97. volumes, err := ExtractVolumes(r)
  98. return len(volumes) == 0, err
  99. }
  100. func (page VolumePage) NextPageURL() (string, error) {
  101. var s struct {
  102. Links []gophercloud.Link `json:"volumes_links"`
  103. }
  104. err := page.ExtractInto(&s)
  105. if err != nil {
  106. return "", err
  107. }
  108. return gophercloud.ExtractNextURL(s.Links)
  109. }
  110. // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
  111. func ExtractVolumes(r pagination.Page) ([]Volume, error) {
  112. var s []Volume
  113. err := ExtractVolumesInto(r, &s)
  114. return s, err
  115. }
  116. type commonResult struct {
  117. gophercloud.Result
  118. }
  119. // Extract will get the Volume object out of the commonResult object.
  120. func (r commonResult) Extract() (*Volume, error) {
  121. var s Volume
  122. err := r.ExtractInto(&s)
  123. return &s, err
  124. }
  125. // ExtractInto converts our response data into a volume struct
  126. func (r commonResult) ExtractInto(v interface{}) error {
  127. return r.Result.ExtractIntoStructPtr(v, "volume")
  128. }
  129. // ExtractVolumesInto similar to ExtractInto but operates on a `list` of volumes
  130. func ExtractVolumesInto(r pagination.Page, v interface{}) error {
  131. return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
  132. }
  133. // CreateResult contains the response body and error from a Create request.
  134. type CreateResult struct {
  135. commonResult
  136. }
  137. // GetResult contains the response body and error from a Get request.
  138. type GetResult struct {
  139. commonResult
  140. }
  141. // UpdateResult contains the response body and error from an Update request.
  142. type UpdateResult struct {
  143. commonResult
  144. }
  145. // DeleteResult contains the response body and error from a Delete request.
  146. type DeleteResult struct {
  147. gophercloud.ErrResult
  148. }