1package floatingips
2
3import (
4	"encoding/json"
5	"strconv"
6
7	"github.com/gophercloud/gophercloud"
8	"github.com/gophercloud/gophercloud/pagination"
9)
10
11// A FloatingIP is an IP that can be associated with a server.
12type FloatingIP struct {
13	// ID is a unique ID of the Floating IP
14	ID string `json:"-"`
15
16	// FixedIP is a specific IP on the server to pair the Floating IP with.
17	FixedIP string `json:"fixed_ip,omitempty"`
18
19	// InstanceID is the ID of the server that is using the Floating IP.
20	InstanceID string `json:"instance_id"`
21
22	// IP is the actual Floating IP.
23	IP string `json:"ip"`
24
25	// Pool is the pool of Floating IPs that this Floating IP belongs to.
26	Pool string `json:"pool"`
27}
28
29func (r *FloatingIP) UnmarshalJSON(b []byte) error {
30	type tmp FloatingIP
31	var s struct {
32		tmp
33		ID interface{} `json:"id"`
34	}
35	err := json.Unmarshal(b, &s)
36	if err != nil {
37		return err
38	}
39
40	*r = FloatingIP(s.tmp)
41
42	switch t := s.ID.(type) {
43	case float64:
44		r.ID = strconv.FormatFloat(t, 'f', -1, 64)
45	case string:
46		r.ID = t
47	}
48
49	return err
50}
51
52// FloatingIPPage stores a single page of FloatingIPs from a List call.
53type FloatingIPPage struct {
54	pagination.SinglePageBase
55}
56
57// IsEmpty determines whether or not a FloatingIPsPage is empty.
58func (page FloatingIPPage) IsEmpty() (bool, error) {
59	va, err := ExtractFloatingIPs(page)
60	return len(va) == 0, err
61}
62
63// ExtractFloatingIPs interprets a page of results as a slice of FloatingIPs.
64func ExtractFloatingIPs(r pagination.Page) ([]FloatingIP, error) {
65	var s struct {
66		FloatingIPs []FloatingIP `json:"floating_ips"`
67	}
68	err := (r.(FloatingIPPage)).ExtractInto(&s)
69	return s.FloatingIPs, err
70}
71
72// FloatingIPResult is the raw result from a FloatingIP request.
73type FloatingIPResult struct {
74	gophercloud.Result
75}
76
77// Extract is a method that attempts to interpret any FloatingIP resource
78// response as a FloatingIP struct.
79func (r FloatingIPResult) Extract() (*FloatingIP, error) {
80	var s struct {
81		FloatingIP *FloatingIP `json:"floating_ip"`
82	}
83	err := r.ExtractInto(&s)
84	return s.FloatingIP, err
85}
86
87// CreateResult is the response from a Create operation. Call its Extract method
88// to interpret it as a FloatingIP.
89type CreateResult struct {
90	FloatingIPResult
91}
92
93// GetResult is the response from a Get operation. Call its Extract method to
94// interpret it as a FloatingIP.
95type GetResult struct {
96	FloatingIPResult
97}
98
99// DeleteResult is the response from a Delete operation. Call its ExtractErr
100// method to determine if the call succeeded or failed.
101type DeleteResult struct {
102	gophercloud.ErrResult
103}
104
105// AssociateResult is the response from a Delete operation. Call its ExtractErr
106// method to determine if the call succeeded or failed.
107type AssociateResult struct {
108	gophercloud.ErrResult
109}
110
111// DisassociateResult is the response from a Delete operation. Call its
112// ExtractErr method to determine if the call succeeded or failed.
113type DisassociateResult struct {
114	gophercloud.ErrResult
115}
116