blob: 678b2aff797547f1498c83e67622184dc2f75426 [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 {
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010012 Address string `json:"address,omitempty"`
13 ID int `json:"id,omitempty"`
14 Type Type `json:"type,omitempty"`
15 Version Version `json:"ipVersion,omitempty" mapstructure:"ipVersion"`
Jamie Hannaford1c817312014-11-04 10:56:58 +010016}
Jamie Hannaford2841a532014-11-04 11:05:42 +010017
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010018// Version represents the version of a VIP.
19type Version string
20
21// Convenient constants to use for type
22const (
23 IPV4 Version = "IPV4"
24 IPV6 Version = "IPV6"
25)
26
27// Type represents the type of a VIP.
28type Type string
29
30const (
31 // PUBLIC indicates a VIP type that is routable on the public Internet.
32 PUBLIC Type = "PUBLIC"
33
34 // PRIVATE indicates a VIP type that is routable only on ServiceNet.
35 PRIVATE Type = "SERVICENET"
36)
37
Jamie Hannaford2841a532014-11-04 11:05:42 +010038// VIPPage is the page returned by a pager when traversing over a collection
39// of VIPs.
40type VIPPage struct {
41 pagination.SinglePageBase
42}
43
44// IsEmpty checks whether a VIPPage struct is empty.
45func (p VIPPage) IsEmpty() (bool, error) {
46 is, err := ExtractVIPs(p)
47 if err != nil {
48 return true, nil
49 }
50 return len(is) == 0, nil
51}
52
53// ExtractVIPs accepts a Page struct, specifically a VIPPage struct, and
54// extracts the elements into a slice of VIP structs. In other words, a
55// generic collection is mapped into a relevant slice.
56func ExtractVIPs(page pagination.Page) ([]VIP, error) {
57 var resp struct {
58 VIPs []VIP `mapstructure:"virtualIps" json:"virtualIps"`
59 }
60
61 err := mapstructure.Decode(page.(VIPPage).Body, &resp)
62
63 return resp.VIPs, err
64}
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010065
66type commonResult struct {
67 gophercloud.Result
68}
69
70func (r commonResult) Extract() (*VIP, error) {
71 if r.Err != nil {
72 return nil, r.Err
73 }
74
75 resp := &VIP{}
76 err := mapstructure.Decode(r.Body, resp)
77
78 return resp, err
79}
80
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010081// CreateResult represents the result of a create operation.
Jamie Hannafordf7e8e1a2014-11-04 11:48:46 +010082type CreateResult struct {
83 commonResult
84}
Jamie Hannaford491ea5d2014-11-04 11:59:37 +010085
86// DeleteResult represents the result of a delete operation.
87type DeleteResult struct {
88 gophercloud.ErrResult
89}