Jamie Hannaford | f0cd165 | 2014-11-04 10:54:11 +0100 | [diff] [blame] | 1 | package vips |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 2 | |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame^] | 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
| 7 | |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 8 | // VIP represents a Virtual IP API resource. |
| 9 | type VIP struct { |
| 10 | Address string `json:"address,omitempty"` |
| 11 | ID int `json:"id,omitempty"` |
| 12 | Type string `json:"type,omitempty"` |
| 13 | Version string `json:"ipVersion,omitempty" mapstructure:"ipVersion"` |
| 14 | } |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame^] | 15 | |
| 16 | // VIPPage is the page returned by a pager when traversing over a collection |
| 17 | // of VIPs. |
| 18 | type VIPPage struct { |
| 19 | pagination.SinglePageBase |
| 20 | } |
| 21 | |
| 22 | // IsEmpty checks whether a VIPPage struct is empty. |
| 23 | func (p VIPPage) IsEmpty() (bool, error) { |
| 24 | is, err := ExtractVIPs(p) |
| 25 | if err != nil { |
| 26 | return true, nil |
| 27 | } |
| 28 | return len(is) == 0, nil |
| 29 | } |
| 30 | |
| 31 | // ExtractVIPs accepts a Page struct, specifically a VIPPage struct, and |
| 32 | // extracts the elements into a slice of VIP structs. In other words, a |
| 33 | // generic collection is mapped into a relevant slice. |
| 34 | func ExtractVIPs(page pagination.Page) ([]VIP, error) { |
| 35 | var resp struct { |
| 36 | VIPs []VIP `mapstructure:"virtualIps" json:"virtualIps"` |
| 37 | } |
| 38 | |
| 39 | err := mapstructure.Decode(page.(VIPPage).Body, &resp) |
| 40 | |
| 41 | return resp.VIPs, err |
| 42 | } |