blob: a3bfed7700f72a0d5a2c3e3c49f0296193319152 [file] [log] [blame]
Jamie Hannafordf0cd1652014-11-04 10:54:11 +01001package vips
Jamie Hannaford1c817312014-11-04 10:56:58 +01002
Jamie Hannaford2841a532014-11-04 11:05:42 +01003import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud/pagination"
6)
7
Jamie Hannaford1c817312014-11-04 10:56:58 +01008// VIP represents a Virtual IP API resource.
9type 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 Hannaford2841a532014-11-04 11:05:42 +010015
16// VIPPage is the page returned by a pager when traversing over a collection
17// of VIPs.
18type VIPPage struct {
19 pagination.SinglePageBase
20}
21
22// IsEmpty checks whether a VIPPage struct is empty.
23func (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.
34func 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}