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