forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
80 lines (65 loc) · 2.14 KB
/
results.go
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
package endpoints
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
type commonResult struct {
gophercloud.Result
}
// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete
// Endpoint. An error is returned if the original call or the extraction failed.
func (r commonResult) Extract() (*Endpoint, error) {
var s struct {
Endpoint *Endpoint `json:"endpoint"`
}
err := r.ExtractInto(&s)
return s.Endpoint, err
}
// CreateResult is the response from a Create operation. Call its Extract
// method to interpret it as an Endpoint.
type CreateResult struct {
commonResult
}
// UpdateResult is the response from an Update operation. Call its Extract
// method to interpret it as an Endpoint.
type UpdateResult struct {
commonResult
}
// DeleteResult is the response from a Delete operation. Call its ExtractErr
// method to determine if the call succeeded or failed.
type DeleteResult struct {
gophercloud.ErrResult
}
// Endpoint describes the entry point for another service's API.
type Endpoint struct {
// ID is the unique ID of the endpoint.
ID string `json:"id"`
// Availability is the interface type of the Endpoint (admin, internal,
// or public), referenced by the gophercloud.Availability type.
Availability gophercloud.Availability `json:"interface"`
// Name is the name of the Endpoint.
Name string `json:"name"`
// Region is the region the Endpoint is located in.
Region string `json:"region"`
// ServiceID is the ID of the service the Endpoint refers to.
ServiceID string `json:"service_id"`
// URL is the url of the Endpoint.
URL string `json:"url"`
}
// EndpointPage is a single page of Endpoint results.
type EndpointPage struct {
pagination.LinkedPageBase
}
// IsEmpty returns true if no Endpoints were returned.
func (r EndpointPage) IsEmpty() (bool, error) {
es, err := ExtractEndpoints(r)
return len(es) == 0, err
}
// ExtractEndpoints extracts an Endpoint slice from a Page.
func ExtractEndpoints(r pagination.Page) ([]Endpoint, error) {
var s struct {
Endpoints []Endpoint `json:"endpoints"`
}
err := (r.(EndpointPage)).ExtractInto(&s)
return s.Endpoints, err
}