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 { |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 12 | 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 Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 16 | } |
Jamie Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 17 | |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 18 | // Version represents the version of a VIP. |
| 19 | type Version string |
| 20 | |
| 21 | // Convenient constants to use for type |
| 22 | const ( |
| 23 | IPV4 Version = "IPV4" |
| 24 | IPV6 Version = "IPV6" |
| 25 | ) |
| 26 | |
| 27 | // Type represents the type of a VIP. |
| 28 | type Type string |
| 29 | |
| 30 | const ( |
| 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 Hannaford | 2841a53 | 2014-11-04 11:05:42 +0100 | [diff] [blame] | 38 | // VIPPage is the page returned by a pager when traversing over a collection |
| 39 | // of VIPs. |
| 40 | type VIPPage struct { |
| 41 | pagination.SinglePageBase |
| 42 | } |
| 43 | |
| 44 | // IsEmpty checks whether a VIPPage struct is empty. |
| 45 | func (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. |
| 56 | func 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 Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 65 | |
| 66 | type commonResult struct { |
| 67 | gophercloud.Result |
| 68 | } |
| 69 | |
| 70 | func (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 Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 81 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | f7e8e1a | 2014-11-04 11:48:46 +0100 | [diff] [blame] | 82 | type CreateResult struct { |
| 83 | commonResult |
| 84 | } |
Jamie Hannaford | 491ea5d | 2014-11-04 11:59:37 +0100 | [diff] [blame] | 85 | |
| 86 | // DeleteResult represents the result of a delete operation. |
| 87 | type DeleteResult struct { |
| 88 | gophercloud.ErrResult |
| 89 | } |