Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 1 | package floatingip |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | // A FloatingIP is an IP that can be associated with an instance |
| 10 | type FloatingIP struct { |
| 11 | // ID is a unique ID of the Floating IP |
| 12 | ID string `mapstructure:"id"` |
| 13 | |
| 14 | // FixedIP is the IP of the instance related to the Floating IP |
| 15 | FixedIP string `mapstructure:"fixed_ip,omitempty"` |
| 16 | |
| 17 | // InstanceID is the ID of the instance that is using the Floating IP |
| 18 | InstanceID string `mapstructure:"instance_id"` |
| 19 | |
| 20 | // IP is the actual Floating IP |
| 21 | IP string `mapstructure:"ip"` |
| 22 | |
| 23 | // Pool is the pool of floating IPs that this floating IP belongs to |
| 24 | Pool string `mapstructure:"pool"` |
| 25 | } |
| 26 | |
| 27 | // FloatingIPsPage stores a single, only page of FloatingIPs |
| 28 | // results from a List call. |
| 29 | type FloatingIPsPage struct { |
| 30 | pagination.SinglePageBase |
| 31 | } |
| 32 | |
| 33 | // IsEmpty determines whether or not a FloatingIPsPage is empty. |
| 34 | func (page FloatingIPsPage) IsEmpty() (bool, error) { |
| 35 | va, err := ExtractFloatingIPs(page) |
| 36 | return len(va) == 0, err |
| 37 | } |
| 38 | |
| 39 | // ExtractFloatingIPs interprets a page of results as a slice of |
| 40 | // FloatingIPs. |
| 41 | func ExtractFloatingIPs(page pagination.Page) ([]FloatingIP, error) { |
| 42 | casted := page.(FloatingIPsPage).Body |
| 43 | var response struct { |
| 44 | FloatingIPs []FloatingIP `mapstructure:"floating_ips"` |
| 45 | } |
| 46 | |
| 47 | err := mapstructure.WeakDecode(casted, &response) |
| 48 | |
| 49 | return response.FloatingIPs, err |
| 50 | } |
| 51 | |
| 52 | type FloatingIPResult struct { |
| 53 | gophercloud.Result |
| 54 | } |
| 55 | |
| 56 | // Extract is a method that attempts to interpret any FloatingIP resource |
| 57 | // response as a FloatingIP struct. |
| 58 | func (r FloatingIPResult) Extract() (*FloatingIP, error) { |
| 59 | if r.Err != nil { |
| 60 | return nil, r.Err |
| 61 | } |
| 62 | |
| 63 | var res struct { |
| 64 | FloatingIP *FloatingIP `json:"floating_ip" mapstructure:"floating_ip"` |
| 65 | } |
| 66 | |
| 67 | err := mapstructure.WeakDecode(r.Body, &res) |
| 68 | return res.FloatingIP, err |
| 69 | } |
| 70 | |
| 71 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 72 | // as a FloatingIP. |
| 73 | type CreateResult struct { |
| 74 | FloatingIPResult |
| 75 | } |
| 76 | |
| 77 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 78 | // as a FloatingIP. |
| 79 | type GetResult struct { |
| 80 | FloatingIPResult |
| 81 | } |
| 82 | |
| 83 | // DeleteResult is the response from a Delete operation. Call its Extract method to determine if |
| 84 | // the call succeeded or failed. |
| 85 | type DeleteResult struct { |
| 86 | gophercloud.ErrResult |
| 87 | } |
| 88 | |
| 89 | // AssociateResult is the response from a Delete operation. Call its Extract method to determine if |
| 90 | // the call succeeded or failed. |
| 91 | type AssociateResult struct { |
| 92 | gophercloud.ErrResult |
| 93 | } |
| 94 | |
| 95 | // DisassociateResult is the response from a Delete operation. Call its Extract method to determine if |
| 96 | // the call succeeded or failed. |
| 97 | type DisassociateResult struct { |
| 98 | gophercloud.ErrResult |
| 99 | } |