/
results.go
84 lines (72 loc) · 2.37 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
81
82
83
84
package attachinterfaces
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
type attachInterfaceResult struct {
gophercloud.Result
}
// Extract interprets any attachInterfaceResult as an Interface, if possible.
func (r attachInterfaceResult) Extract() (*Interface, error) {
var s struct {
Interface *Interface `json:"interfaceAttachment"`
}
err := r.ExtractInto(&s)
return s.Interface, err
}
// GetResult is the response from a Get operation. Call its Extract
// method to interpret it as an Interface.
type GetResult struct {
attachInterfaceResult
}
// CreateResult is the response from a Create operation. Call its Extract
// method to interpret it as an Interface.
type CreateResult struct {
attachInterfaceResult
}
// 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
}
// FixedIP represents a Fixed IP Address.
// This struct is also used when creating an attachment,
// but it is not possible to specify a SubnetID.
type FixedIP struct {
SubnetID string `json:"subnet_id,omitempty"`
IPAddress string `json:"ip_address"`
}
// Interface represents a network interface on a server.
type Interface struct {
PortState string `json:"port_state"`
FixedIPs []FixedIP `json:"fixed_ips"`
PortID string `json:"port_id"`
NetID string `json:"net_id"`
MACAddr string `json:"mac_addr"`
}
// InterfacePage abstracts the raw results of making a List() request against
// the API.
//
// As OpenStack extensions may freely alter the response bodies of structures
// returned to the client, you may only safely access the data provided through
// the ExtractInterfaces call.
type InterfacePage struct {
pagination.SinglePageBase
}
// IsEmpty returns true if an InterfacePage contains no interfaces.
func (r InterfacePage) IsEmpty() (bool, error) {
if r.StatusCode == 204 {
return true, nil
}
interfaces, err := ExtractInterfaces(r)
return len(interfaces) == 0, err
}
// ExtractInterfaces interprets the results of a single page from a List() call,
// producing a slice of Interface structs.
func ExtractInterfaces(r pagination.Page) ([]Interface, error) {
var s struct {
Interfaces []Interface `json:"interfaceAttachments"`
}
err := (r.(InterfacePage)).ExtractInto(&s)
return s.Interfaces, err
}