blob: 2f5b33844e7c39a49a1b8e5d45d2db43fee7c35a [file] [log] [blame]
Jon Perritt12395212016-02-24 10:41:17 -06001package floatingips
Joe Topjiandee32222015-02-09 23:56:26 +00002
3import (
Joe Topjian4d2266c2017-01-19 16:57:50 -07004 "encoding/json"
5 "strconv"
6
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/pagination"
Joe Topjiandee32222015-02-09 23:56:26 +00009)
10
11// A FloatingIP is an IP that can be associated with an instance
12type FloatingIP struct {
13 // ID is a unique ID of the Floating IP
Joe Topjian4d2266c2017-01-19 16:57:50 -070014 ID string `json:"-"`
Joe Topjiandee32222015-02-09 23:56:26 +000015
16 // FixedIP is the IP of the instance related to the Floating IP
Jon Perritt12395212016-02-24 10:41:17 -060017 FixedIP string `json:"fixed_ip,omitempty"`
Joe Topjiandee32222015-02-09 23:56:26 +000018
19 // InstanceID is the ID of the instance that is using the Floating IP
Jon Perritt12395212016-02-24 10:41:17 -060020 InstanceID string `json:"instance_id"`
Joe Topjiandee32222015-02-09 23:56:26 +000021
22 // IP is the actual Floating IP
Jon Perritt12395212016-02-24 10:41:17 -060023 IP string `json:"ip"`
Joe Topjiandee32222015-02-09 23:56:26 +000024
25 // Pool is the pool of floating IPs that this floating IP belongs to
Jon Perritt12395212016-02-24 10:41:17 -060026 Pool string `json:"pool"`
Joe Topjiandee32222015-02-09 23:56:26 +000027}
28
Joe Topjian4d2266c2017-01-19 16:57:50 -070029func (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
Jon Perritt12395212016-02-24 10:41:17 -060052// FloatingIPPage stores a single, only page of FloatingIPs
Joe Topjiandee32222015-02-09 23:56:26 +000053// results from a List call.
Jon Perritt12395212016-02-24 10:41:17 -060054type FloatingIPPage struct {
Joe Topjiandee32222015-02-09 23:56:26 +000055 pagination.SinglePageBase
56}
57
58// IsEmpty determines whether or not a FloatingIPsPage is empty.
Jon Perritt12395212016-02-24 10:41:17 -060059func (page FloatingIPPage) IsEmpty() (bool, error) {
Joe Topjiandee32222015-02-09 23:56:26 +000060 va, err := ExtractFloatingIPs(page)
61 return len(va) == 0, err
62}
63
64// ExtractFloatingIPs interprets a page of results as a slice of
65// FloatingIPs.
Jon Perritt31b66462016-02-25 22:25:30 -060066func ExtractFloatingIPs(r pagination.Page) ([]FloatingIP, error) {
Jon Perritt12395212016-02-24 10:41:17 -060067 var s struct {
68 FloatingIPs []FloatingIP `json:"floating_ips"`
Joe Topjiandee32222015-02-09 23:56:26 +000069 }
Jon Perritt31b66462016-02-25 22:25:30 -060070 err := (r.(FloatingIPPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060071 return s.FloatingIPs, err
Joe Topjiandee32222015-02-09 23:56:26 +000072}
73
Jon Perritt12395212016-02-24 10:41:17 -060074// FloatingIPResult is the raw result from a FloatingIP request.
Joe Topjiandee32222015-02-09 23:56:26 +000075type FloatingIPResult struct {
76 gophercloud.Result
77}
78
79// Extract is a method that attempts to interpret any FloatingIP resource
80// response as a FloatingIP struct.
81func (r FloatingIPResult) Extract() (*FloatingIP, error) {
Jon Perritt12395212016-02-24 10:41:17 -060082 var s struct {
83 FloatingIP *FloatingIP `json:"floating_ip"`
Joe Topjiandee32222015-02-09 23:56:26 +000084 }
Jon Perritt12395212016-02-24 10:41:17 -060085 err := r.ExtractInto(&s)
86 return s.FloatingIP, err
Joe Topjiandee32222015-02-09 23:56:26 +000087}
88
89// CreateResult is the response from a Create operation. Call its Extract method to interpret it
90// as a FloatingIP.
91type CreateResult struct {
92 FloatingIPResult
93}
94
95// GetResult is the response from a Get operation. Call its Extract method to interpret it
96// as a FloatingIP.
97type GetResult struct {
98 FloatingIPResult
99}
100
101// DeleteResult is the response from a Delete operation. Call its Extract method to determine if
102// the call succeeded or failed.
103type DeleteResult struct {
104 gophercloud.ErrResult
105}
106
107// AssociateResult is the response from a Delete operation. Call its Extract method to determine if
108// the call succeeded or failed.
109type AssociateResult struct {
110 gophercloud.ErrResult
111}
112
113// DisassociateResult is the response from a Delete operation. Call its Extract method to determine if
114// the call succeeded or failed.
115type DisassociateResult struct {
116 gophercloud.ErrResult
117}