blob: aa5ef376ff50b491321015d1e15d589d860ef379 [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"
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +01005
6 "github.com/rackspace/gophercloud"
Jamie Hannaford2841a532014-11-04 11:05:42 +01007 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannaford1c817312014-11-04 10:56:58 +010010// VIP represents a Virtual IP API resource.
11type 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 Hannaford2841a532014-11-04 11:05:42 +010017
18// VIPPage is the page returned by a pager when traversing over a collection
19// of VIPs.
20type VIPPage struct {
21 pagination.SinglePageBase
22}
23
24// IsEmpty checks whether a VIPPage struct is empty.
25func (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.
36func 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 Hannafordf7e8e1a2014-11-04 11:48:46 +010045
46type commonResult struct {
47 gophercloud.Result
48}
49
50func (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
61type CreateResult struct {
62 commonResult
63}