1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
package snapshots
import (
"encoding/json"
"net/url"
"strconv"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
const (
invalidMarker = "-1"
)
// Snapshot contains all information associated with an OpenStack Snapshot
type Snapshot struct {
// The UUID of the snapshot
ID string `json:"id"`
// The name of the snapshot
Name string `json:"name,omitempty"`
// A description of the snapshot
Description string `json:"description,omitempty"`
// UUID of the share from which the snapshot was created
ShareID string `json:"share_id"`
// The shared file system protocol
ShareProto string `json:"share_proto"`
// Size of the snapshot share in GB
ShareSize int `json:"share_size"`
// Size of the snapshot in GB
Size int `json:"size"`
// The snapshot status
Status string `json:"status"`
// The UUID of the project in which the snapshot was created
ProjectID string `json:"project_id"`
// Timestamp when the snapshot was created
CreatedAt time.Time `json:"-"`
// Snapshot links for pagination
Links []map[string]string `json:"links"`
}
func (r *Snapshot) UnmarshalJSON(b []byte) error {
type tmp Snapshot
var s struct {
tmp
CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Snapshot(s.tmp)
r.CreatedAt = time.Time(s.CreatedAt)
return nil
}
type commonResult struct {
gophercloud.Result
}
// Extract will get the Snapshot object from the commonResult
func (r commonResult) Extract() (*Snapshot, error) {
var s struct {
Snapshot *Snapshot `json:"snapshot"`
}
err := r.ExtractInto(&s)
return s.Snapshot, err
}
// CreateResult contains the response body and error from a Create request.
type CreateResult struct {
commonResult
}
// SnapshotPage is a pagination.pager that is returned from a call to the List function.
type SnapshotPage struct {
pagination.MarkerPageBase
}
// NextPageURL generates the URL for the page of results after this one.
func (r SnapshotPage) NextPageURL() (string, error) {
currentURL := r.URL
mark, err := r.Owner.LastMarker()
if err != nil {
return "", err
}
if mark == invalidMarker {
return "", nil
}
q := currentURL.Query()
q.Set("offset", mark)
currentURL.RawQuery = q.Encode()
return currentURL.String(), nil
}
// LastMarker returns the last offset in a ListResult.
func (r SnapshotPage) LastMarker() (string, error) {
snapshots, err := ExtractSnapshots(r)
if err != nil {
return invalidMarker, err
}
if len(snapshots) == 0 {
return invalidMarker, nil
}
u, err := url.Parse(r.URL.String())
if err != nil {
return invalidMarker, err
}
queryParams := u.Query()
offset := queryParams.Get("offset")
limit := queryParams.Get("limit")
// Limit is not present, only one page required
if limit == "" {
return invalidMarker, nil
}
iOffset := 0
if offset != "" {
iOffset, err = strconv.Atoi(offset)
if err != nil {
return invalidMarker, err
}
}
iLimit, err := strconv.Atoi(limit)
if err != nil {
return invalidMarker, err
}
iOffset = iOffset + iLimit
offset = strconv.Itoa(iOffset)
return offset, nil
}
// IsEmpty satisifies the IsEmpty method of the Page interface
func (r SnapshotPage) IsEmpty() (bool, error) {
snapshots, err := ExtractSnapshots(r)
return len(snapshots) == 0, err
}
// ExtractSnapshots extracts and returns a Snapshot slice. It is used while
// iterating over a snapshots.List call.
func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
var s struct {
Snapshots []Snapshot `json:"snapshots"`
}
err := (r.(SnapshotPage)).ExtractInto(&s)
return s.Snapshots, err
}
// DeleteResult contains the response body and error from a Delete request.
type DeleteResult struct {
gophercloud.ErrResult
}
// GetResult contains the response body and error from a Get request.
type GetResult struct {
commonResult
}
// UpdateResult contains the response body and error from an Update request.
type UpdateResult struct {
commonResult
}
|