Krzysztof Szukiełojć | e28b2e2 | 2017-07-31 11:31:06 +0200 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "time" |
| 6 | |
| 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
| 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
| 9 | ) |
| 10 | |
| 11 | type Snapshot struct { |
| 12 | // Unique identifier for the snapshot. |
| 13 | ID string `json:"id"` |
| 14 | // Current status of the snapshot. |
| 15 | Status string `json:"status"` |
| 16 | // Size of the snapshot in GB. |
| 17 | Size int `json:"size"` |
| 18 | // The date when this snapshot was created. |
| 19 | CreatedAt time.Time `json:"-"` |
| 20 | // Human-readable display name for the snapshot. |
| 21 | Name string `json:"name"` |
| 22 | } |
| 23 | |
| 24 | func (r *Snapshot) UnmarshalJSON(b []byte) error { |
| 25 | type tmp Snapshot |
| 26 | var s struct { |
| 27 | tmp |
| 28 | CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` |
| 29 | } |
| 30 | err := json.Unmarshal(b, &s) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | *r = Snapshot(s.tmp) |
| 35 | |
| 36 | r.CreatedAt = time.Time(s.CreatedAt) |
| 37 | |
| 38 | return err |
| 39 | } |
| 40 | |
| 41 | type SnapshotPage struct { |
| 42 | pagination.SinglePageBase |
| 43 | } |
| 44 | |
| 45 | // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call. |
| 46 | func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) { |
| 47 | var s []Snapshot |
| 48 | err := ExtractSnapshotInto(r, &s) |
| 49 | return s, err |
| 50 | } |
| 51 | |
| 52 | func ExtractSnapshotInto(r pagination.Page, v interface{}) error { |
| 53 | return r.(SnapshotPage).Result.ExtractIntoSlicePtr(v, "snapshots") |
| 54 | } |