github.com/gophercloud/gophercloud@v1.11.0/openstack/sharedfilesystems/v2/replicas/results.go (about) 1 package replicas 2 3 import ( 4 "encoding/json" 5 "net/url" 6 "strconv" 7 "time" 8 9 "github.com/gophercloud/gophercloud" 10 "github.com/gophercloud/gophercloud/pagination" 11 ) 12 13 const ( 14 invalidMarker = "-1" 15 ) 16 17 // Replica contains all information associated with an OpenStack Share Replica. 18 type Replica struct { 19 // ID of the share replica 20 ID string `json:"id"` 21 // The availability zone of the share replica. 22 AvailabilityZone string `json:"availability_zone"` 23 // Indicates whether existing access rules will be cast to read/only. 24 CastRulesToReadonly bool `json:"cast_rules_to_readonly"` 25 // The host name of the share replica. 26 Host string `json:"host"` 27 // The UUID of the share to which a share replica belongs. 28 ShareID string `json:"share_id"` 29 // The UUID of the share network where the resource is exported to. 30 ShareNetworkID string `json:"share_network_id"` 31 // The UUID of the share server. 32 ShareServerID string `json:"share_server_id"` 33 // The share replica status. 34 Status string `json:"status"` 35 // The share replica state. 36 State string `json:"replica_state"` 37 // Timestamp when the replica was created. 38 CreatedAt time.Time `json:"-"` 39 // Timestamp when the replica was updated. 40 UpdatedAt time.Time `json:"-"` 41 } 42 43 func (r *Replica) UnmarshalJSON(b []byte) error { 44 type tmp Replica 45 var s struct { 46 tmp 47 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` 48 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"` 49 } 50 err := json.Unmarshal(b, &s) 51 if err != nil { 52 return err 53 } 54 *r = Replica(s.tmp) 55 56 r.CreatedAt = time.Time(s.CreatedAt) 57 r.UpdatedAt = time.Time(s.UpdatedAt) 58 59 return nil 60 } 61 62 type commonResult struct { 63 gophercloud.Result 64 } 65 66 // Extract will get the Replica object from the commonResult. 67 func (r commonResult) Extract() (*Replica, error) { 68 var s struct { 69 Replica *Replica `json:"share_replica"` 70 } 71 err := r.ExtractInto(&s) 72 return s.Replica, err 73 } 74 75 // CreateResult contains the response body and error from a Create request. 76 type CreateResult struct { 77 commonResult 78 } 79 80 // ReplicaPage is a pagination.pager that is returned from a call to the List function. 81 type ReplicaPage struct { 82 pagination.MarkerPageBase 83 } 84 85 // NextPageURL generates the URL for the page of results after this one. 86 func (r ReplicaPage) NextPageURL() (string, error) { 87 currentURL := r.URL 88 mark, err := r.Owner.LastMarker() 89 if err != nil { 90 return "", err 91 } 92 if mark == invalidMarker { 93 return "", nil 94 } 95 96 q := currentURL.Query() 97 q.Set("offset", mark) 98 currentURL.RawQuery = q.Encode() 99 return currentURL.String(), nil 100 } 101 102 // LastMarker returns the last offset in a ListResult. 103 func (r ReplicaPage) LastMarker() (string, error) { 104 replicas, err := ExtractReplicas(r) 105 if err != nil { 106 return invalidMarker, err 107 } 108 if len(replicas) == 0 { 109 return invalidMarker, nil 110 } 111 112 u, err := url.Parse(r.URL.String()) 113 if err != nil { 114 return invalidMarker, err 115 } 116 queryParams := u.Query() 117 offset := queryParams.Get("offset") 118 limit := queryParams.Get("limit") 119 120 // Limit is not present, only one page required 121 if limit == "" { 122 return invalidMarker, nil 123 } 124 125 iOffset := 0 126 if offset != "" { 127 iOffset, err = strconv.Atoi(offset) 128 if err != nil { 129 return invalidMarker, err 130 } 131 } 132 iLimit, err := strconv.Atoi(limit) 133 if err != nil { 134 return invalidMarker, err 135 } 136 iOffset = iOffset + iLimit 137 offset = strconv.Itoa(iOffset) 138 139 return offset, nil 140 } 141 142 // IsEmpty satisifies the IsEmpty method of the Page interface. 143 func (r ReplicaPage) IsEmpty() (bool, error) { 144 if r.StatusCode == 204 { 145 return true, nil 146 } 147 148 replicas, err := ExtractReplicas(r) 149 return len(replicas) == 0, err 150 } 151 152 // ExtractReplicas extracts and returns Replicas. It is used while iterating 153 // over a replicas.List or replicas.ListDetail calls. 154 func ExtractReplicas(r pagination.Page) ([]Replica, error) { 155 var s []Replica 156 err := ExtractReplicasInto(r, &s) 157 return s, err 158 } 159 160 // ExtractReplicasInto similar to ExtractReplicas but operates on a `list` of 161 // replicas. 162 func ExtractReplicasInto(r pagination.Page, v interface{}) error { 163 return r.(ReplicaPage).Result.ExtractIntoSlicePtr(v, "share_replicas") 164 } 165 166 // DeleteResult contains the response body and error from a Delete request. 167 type DeleteResult struct { 168 gophercloud.ErrResult 169 } 170 171 // GetResult contains the response body and error from a Get request. 172 type GetResult struct { 173 commonResult 174 } 175 176 // ListExportLocationsResult contains the result body and error from a 177 // ListExportLocations request. 178 type ListExportLocationsResult struct { 179 gophercloud.Result 180 } 181 182 // GetExportLocationResult contains the result body and error from a 183 // GetExportLocation request. 184 type GetExportLocationResult struct { 185 gophercloud.Result 186 } 187 188 // ExportLocation contains all information associated with a share export location 189 type ExportLocation struct { 190 // The share replica export location UUID. 191 ID string `json:"id"` 192 // The export location path that should be used for mount operation. 193 Path string `json:"path"` 194 // The UUID of the share instance that this export location belongs to. 195 ShareInstanceID string `json:"share_instance_id"` 196 // Defines purpose of an export location. If set to true, then it is 197 // expected to be used for service needs and by administrators only. If 198 // it is set to false, then this export location can be used by end users. 199 IsAdminOnly bool `json:"is_admin_only"` 200 // Drivers may use this field to identify which export locations are 201 // most efficient and should be used preferentially by clients. 202 // By default it is set to false value. New in version 2.14. 203 Preferred bool `json:"preferred"` 204 // The availability zone of the share replica. 205 AvailabilityZone string `json:"availability_zone"` 206 // The share replica state. 207 State string `json:"replica_state"` 208 // Timestamp when the export location was created. 209 CreatedAt time.Time `json:"-"` 210 // Timestamp when the export location was updated. 211 UpdatedAt time.Time `json:"-"` 212 } 213 214 func (r *ExportLocation) UnmarshalJSON(b []byte) error { 215 type tmp ExportLocation 216 var s struct { 217 tmp 218 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` 219 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"` 220 } 221 err := json.Unmarshal(b, &s) 222 if err != nil { 223 return err 224 } 225 *r = ExportLocation(s.tmp) 226 227 r.CreatedAt = time.Time(s.CreatedAt) 228 r.UpdatedAt = time.Time(s.UpdatedAt) 229 230 return nil 231 } 232 233 // Extract will get the Export Locations from the ListExportLocationsResult 234 func (r ListExportLocationsResult) Extract() ([]ExportLocation, error) { 235 var s struct { 236 ExportLocations []ExportLocation `json:"export_locations"` 237 } 238 err := r.ExtractInto(&s) 239 return s.ExportLocations, err 240 } 241 242 // Extract will get the Export Location from the GetExportLocationResult 243 func (r GetExportLocationResult) Extract() (*ExportLocation, error) { 244 var s struct { 245 ExportLocation *ExportLocation `json:"export_location"` 246 } 247 err := r.ExtractInto(&s) 248 return s.ExportLocation, err 249 } 250 251 // PromoteResult contains the error from an Promote request. 252 type PromoteResult struct { 253 gophercloud.ErrResult 254 } 255 256 // ResyncResult contains the error from a Resync request. 257 type ResyncResult struct { 258 gophercloud.ErrResult 259 } 260 261 // ResetStatusResult contains the error from a ResetStatus request. 262 type ResetStatusResult struct { 263 gophercloud.ErrResult 264 } 265 266 // ResetStateResult contains the error from a ResetState request. 267 type ResetStateResult struct { 268 gophercloud.ErrResult 269 } 270 271 // ForceDeleteResult contains the error from a ForceDelete request. 272 type ForceDeleteResult struct { 273 gophercloud.ErrResult 274 }