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" |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame^] | 5 | |
| 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 10 | // VIP represents a Virtual IP API resource. |
| 11 | type VIP struct { |
| 12 | Address string `json:"address,omitempty"` |
| 13 | ID int `json:"id,omitempty"` |
| 14 | Type string `json:"type,omitempty"` |
| 15 | Version string `json:"ipVersion,omitempty" mapstructure:"ipVersion"` |
| 16 | } |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 17 | |
| 18 | // VIPPage is the page returned by a pager when traversing over a collection |
| 19 | // of VIPs. |
| 20 | type VIPPage struct { |
| 21 | pagination.SinglePageBase |
| 22 | } |
| 23 | |
| 24 | // IsEmpty checks whether a VIPPage struct is empty. |
| 25 | func (p VIPPage) IsEmpty() (bool, error) { |
| 26 | is, err := ExtractVIPs(p) |
| 27 | if err != nil { |
| 28 | return true, nil |
| 29 | } |
| 30 | return len(is) == 0, nil |
| 31 | } |
| 32 | |
| 33 | // ExtractVIPs accepts a Page struct, specifically a VIPPage struct, and |
| 34 | // extracts the elements into a slice of VIP structs. In other words, a |
| 35 | // generic collection is mapped into a relevant slice. |
| 36 | func ExtractVIPs(page pagination.Page) ([]VIP, error) { |
| 37 | var resp struct { |
| 38 | VIPs []VIP `mapstructure:"virtualIps" json:"virtualIps"` |
| 39 | } |
| 40 | |
| 41 | err := mapstructure.Decode(page.(VIPPage).Body, &resp) |
| 42 | |
| 43 | return resp.VIPs, err |
| 44 | } |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame^] | 45 | |
| 46 | type commonResult struct { |
| 47 | gophercloud.Result |
| 48 | } |
| 49 | |
| 50 | func (r commonResult) Extract() (*VIP, error) { |
| 51 | if r.Err != nil { |
| 52 | return nil, r.Err |
| 53 | } |
| 54 | |
| 55 | resp := &VIP{} |
| 56 | err := mapstructure.Decode(r.Body, resp) |
| 57 | |
| 58 | return resp, err |
| 59 | } |
| 60 | |
| 61 | type CreateResult struct { |
| 62 | commonResult |
| 63 | } |