github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/snapshots/results.go (about) 1 package snapshots 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/gophercloud/gophercloud" 8 "github.com/gophercloud/gophercloud/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 gophercloud.ErrResult 54 } 55 56 // UpdateResult contains the response body and error from an Update request. 57 type UpdateResult struct { 58 commonResult 59 } 60 61 // SnapshotPage is a pagination.Pager that is returned from a call to the List function. 62 type SnapshotPage struct { 63 pagination.LinkedPageBase 64 } 65 66 // UnmarshalJSON converts our JSON API response into our snapshot struct 67 func (r *Snapshot) UnmarshalJSON(b []byte) error { 68 type tmp Snapshot 69 var s struct { 70 tmp 71 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` 72 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"` 73 } 74 err := json.Unmarshal(b, &s) 75 if err != nil { 76 return err 77 } 78 *r = Snapshot(s.tmp) 79 80 r.CreatedAt = time.Time(s.CreatedAt) 81 r.UpdatedAt = time.Time(s.UpdatedAt) 82 83 return err 84 } 85 86 // IsEmpty returns true if a SnapshotPage contains no Snapshots. 87 func (r SnapshotPage) IsEmpty() (bool, error) { 88 if r.StatusCode == 204 { 89 return true, nil 90 } 91 92 volumes, err := ExtractSnapshots(r) 93 return len(volumes) == 0, err 94 } 95 96 // NextPageURL uses the response's embedded link reference to navigate to the 97 // next page of results. 98 func (r SnapshotPage) NextPageURL() (string, error) { 99 var s struct { 100 Links []gophercloud.Link `json:"snapshots_links"` 101 } 102 err := r.ExtractInto(&s) 103 if err != nil { 104 return "", err 105 } 106 return gophercloud.ExtractNextURL(s.Links) 107 } 108 109 // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call. 110 func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) { 111 var s struct { 112 Snapshots []Snapshot `json:"snapshots"` 113 } 114 err := (r.(SnapshotPage)).ExtractInto(&s) 115 return s.Snapshots, err 116 } 117 118 // UpdateMetadataResult contains the response body and error from an UpdateMetadata request. 119 type UpdateMetadataResult struct { 120 commonResult 121 } 122 123 // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata. 124 func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) { 125 if r.Err != nil { 126 return nil, r.Err 127 } 128 m := r.Body.(map[string]interface{})["metadata"] 129 return m.(map[string]interface{}), nil 130 } 131 132 type commonResult struct { 133 gophercloud.Result 134 } 135 136 // Extract will get the Snapshot object out of the commonResult object. 137 func (r commonResult) Extract() (*Snapshot, error) { 138 var s struct { 139 Snapshot *Snapshot `json:"snapshot"` 140 } 141 err := r.ExtractInto(&s) 142 return s.Snapshot, err 143 } 144 145 // ResetStatusResult contains the response error from a ResetStatus request. 146 type ResetStatusResult struct { 147 gophercloud.ErrResult 148 } 149 150 // UpdateStatusResult contains the response error from an UpdateStatus request. 151 type UpdateStatusResult struct { 152 gophercloud.ErrResult 153 } 154 155 // ForceDeleteResult contains the response error from a ForceDelete request. 156 type ForceDeleteResult struct { 157 gophercloud.ErrResult 158 }