Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 1 | package floatingips |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 2 | |
| 3 | import ( |
Joe Topjian | 4d2266c | 2017-01-19 16:57:50 -0700 | [diff] [blame] | 4 | "encoding/json" |
| 5 | "strconv" |
| 6 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // A FloatingIP is an IP that can be associated with an instance |
| 12 | type FloatingIP struct { |
| 13 | // ID is a unique ID of the Floating IP |
Joe Topjian | 4d2266c | 2017-01-19 16:57:50 -0700 | [diff] [blame] | 14 | ID string `json:"-"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 15 | |
| 16 | // FixedIP is the IP of the instance related to the Floating IP |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 17 | FixedIP string `json:"fixed_ip,omitempty"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 18 | |
| 19 | // InstanceID is the ID of the instance that is using the Floating IP |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 20 | InstanceID string `json:"instance_id"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 21 | |
| 22 | // IP is the actual Floating IP |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 23 | IP string `json:"ip"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 24 | |
| 25 | // Pool is the pool of floating IPs that this floating IP belongs to |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 26 | Pool string `json:"pool"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Joe Topjian | 4d2266c | 2017-01-19 16:57:50 -0700 | [diff] [blame] | 29 | func (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 Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 52 | // FloatingIPPage stores a single, only page of FloatingIPs |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 53 | // results from a List call. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 54 | type FloatingIPPage struct { |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 55 | pagination.SinglePageBase |
| 56 | } |
| 57 | |
| 58 | // IsEmpty determines whether or not a FloatingIPsPage is empty. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 59 | func (page FloatingIPPage) IsEmpty() (bool, error) { |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 60 | 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 Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 66 | func ExtractFloatingIPs(r pagination.Page) ([]FloatingIP, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 67 | var s struct { |
| 68 | FloatingIPs []FloatingIP `json:"floating_ips"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 69 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 70 | err := (r.(FloatingIPPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 71 | return s.FloatingIPs, err |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 74 | // FloatingIPResult is the raw result from a FloatingIP request. |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 75 | type 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. |
| 81 | func (r FloatingIPResult) Extract() (*FloatingIP, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 82 | var s struct { |
| 83 | FloatingIP *FloatingIP `json:"floating_ip"` |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 84 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 85 | err := r.ExtractInto(&s) |
| 86 | return s.FloatingIP, err |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 90 | // as a FloatingIP. |
| 91 | type 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. |
| 97 | type 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. |
| 103 | type 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. |
| 109 | type 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. |
| 115 | type DisassociateResult struct { |
| 116 | gophercloud.ErrResult |
| 117 | } |