results.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package snapshots
  2. import (
  3. "encoding/json"
  4. "time"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  7. )
  8. // Snapshot contains all the information associated with a Cinder Snapshot.
  9. type Snapshot struct {
  10. // Unique identifier.
  11. ID string `json:"id"`
  12. // Date created.
  13. CreatedAt time.Time `json:"-"`
  14. // Date updated.
  15. UpdatedAt time.Time `json:"-"`
  16. // Display name.
  17. Name string `json:"name"`
  18. // Display description.
  19. Description string `json:"description"`
  20. // ID of the Volume from which this Snapshot was created.
  21. VolumeID string `json:"volume_id"`
  22. // Currect status of the Snapshot.
  23. Status string `json:"status"`
  24. // Size of the Snapshot, in GB.
  25. Size int `json:"size"`
  26. // User-defined key-value pairs.
  27. Metadata map[string]string `json:"metadata"`
  28. }
  29. // CreateResult contains the response body and error from a Create request.
  30. type CreateResult struct {
  31. commonResult
  32. }
  33. // GetResult contains the response body and error from a Get request.
  34. type GetResult struct {
  35. commonResult
  36. }
  37. // DeleteResult contains the response body and error from a Delete request.
  38. type DeleteResult struct {
  39. gophercloud.ErrResult
  40. }
  41. // SnapshotPage is a pagination.Pager that is returned from a call to the List function.
  42. type SnapshotPage struct {
  43. pagination.SinglePageBase
  44. }
  45. func (r *Snapshot) UnmarshalJSON(b []byte) error {
  46. type tmp Snapshot
  47. var s struct {
  48. tmp
  49. CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
  50. UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
  51. }
  52. err := json.Unmarshal(b, &s)
  53. if err != nil {
  54. return err
  55. }
  56. *r = Snapshot(s.tmp)
  57. r.CreatedAt = time.Time(s.CreatedAt)
  58. r.UpdatedAt = time.Time(s.UpdatedAt)
  59. return err
  60. }
  61. // IsEmpty returns true if a SnapshotPage contains no Snapshots.
  62. func (r SnapshotPage) IsEmpty() (bool, error) {
  63. volumes, err := ExtractSnapshots(r)
  64. return len(volumes) == 0, err
  65. }
  66. // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
  67. func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
  68. var s struct {
  69. Snapshots []Snapshot `json:"snapshots"`
  70. }
  71. err := (r.(SnapshotPage)).ExtractInto(&s)
  72. return s.Snapshots, err
  73. }
  74. // UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
  75. type UpdateMetadataResult struct {
  76. commonResult
  77. }
  78. // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
  79. func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
  80. if r.Err != nil {
  81. return nil, r.Err
  82. }
  83. m := r.Body.(map[string]interface{})["metadata"]
  84. return m.(map[string]interface{}), nil
  85. }
  86. type commonResult struct {
  87. gophercloud.Result
  88. }
  89. // Extract will get the Snapshot object out of the commonResult object.
  90. func (r commonResult) Extract() (*Snapshot, error) {
  91. var s struct {
  92. Snapshot *Snapshot `json:"snapshot"`
  93. }
  94. err := r.ExtractInto(&s)
  95. return s.Snapshot, err
  96. }