blob: a512cd24b85bedd17305292172cada940374731b [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
Jamie Hannafordd9036422014-09-23 17:50:24 +02004 "fmt"
5
Jamie Hannaford0708c002014-09-17 16:08:49 +02006 "github.com/mitchellh/mapstructure"
Jamie Hannafordd9036422014-09-23 17:50:24 +02007 "github.com/rackspace/gophercloud"
Jamie Hannaford0708c002014-09-17 16:08:49 +02008 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannafordd9036422014-09-23 17:50:24 +020011type commonResult struct {
12 gophercloud.CommonResult
13}
14
15func (r commonResult) Extract() (*Subnet, error) {
16 if r.Err != nil {
17 return nil, r.Err
18 }
19
20 var res struct {
21 Subnet *Subnet `json:"subnet"`
22 }
23
24 err := mapstructure.Decode(r.Resp, &res)
25 if err != nil {
26 return nil, fmt.Errorf("Error decoding Neutron subnet: %v", err)
27 }
28
29 return res.Subnet, nil
30}
31
32type CreateResult struct {
33 commonResult
34}
35
36type GetResult struct {
37 commonResult
38}
39
40type UpdateResult struct {
41 commonResult
42}
43
44type DeleteResult commonResult
45
Jamie Hannafordf2835402014-09-23 11:01:21 +020046// AllocationPool represents a sub-range of cidr available for dynamic
47// allocation to ports, e.g. {Start: "10.0.0.2", End: "10.0.0.254"}
Jamie Hannaford0708c002014-09-17 16:08:49 +020048type AllocationPool struct {
Jamie Hannaford63631432014-09-18 11:40:09 +020049 Start string `json:"start"`
50 End string `json:"end"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020051}
52
Jamie Hannafordf2835402014-09-23 11:01:21 +020053// HostRoute represents a route that should be used by devices with IPs from
54// a subnet (not including local subnet route).
55type HostRoute struct {
56 DestinationCIDR string `json:"destination"`
57 NextHop string `json:"nexthop"`
58}
59
Jamie Hannaford686c4962014-09-23 10:46:20 +020060// Subnet represents a subnet. See package documentation for a top-level
61// description of what this is.
Jamie Hannaford0708c002014-09-17 16:08:49 +020062type Subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020063 // UUID representing the subnet
64 ID string `mapstructure:"id" json:"id"`
65 // UUID of the parent network
66 NetworkID string `mapstructure:"network_id" json:"network_id"`
67 // Human-readable name for the subnet. Might not be unique.
68 Name string `mapstructure:"name" json:"name"`
69 // IP version, either `4' or `6'
70 IPVersion int `mapstructure:"ip_version" json:"ip_version"`
71 // CIDR representing IP range for this subnet, based on IP version
72 CIDR string `mapstructure:"cidr" json:"cidr"`
73 // Default gateway used by devices in this subnet
74 GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"`
75 // DNS name servers used by hosts in this subnet.
76 DNSNameservers []string `mapstructure:"dns_nameservers" json:"dns_nameservers"`
77 // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool.
Jamie Hannaford0708c002014-09-17 16:08:49 +020078 AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020079 // Routes that should be used by devices with IPs from this subnet (not including local subnet route).
Jamie Hannafordf2835402014-09-23 11:01:21 +020080 HostRoutes []HostRoute `mapstructure:"host_routes" json:"host_routes"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020081 // Specifies whether DHCP is enabled for this subnet or not.
82 EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"`
83 // Owner of network. Only admin users can specify a tenant_id other than its own.
84 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020085}
86
Jamie Hannaford686c4962014-09-23 10:46:20 +020087// SubnetPage is the page returned by a pager when traversing over a collection
88// of subnets.
Jamie Hannaford0708c002014-09-17 16:08:49 +020089type SubnetPage struct {
90 pagination.LinkedPageBase
91}
92
Jamie Hannaford686c4962014-09-23 10:46:20 +020093// NextPageURL is invoked when a paginated collection of subnets has reached
94// the end of a page and the pager seeks to traverse over a new one. In order
95// to do this, it needs to construct the next page's URL.
96func (p SubnetPage) NextPageURL() (string, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020097 type link struct {
98 Href string `mapstructure:"href"`
99 Rel string `mapstructure:"rel"`
100 }
101 type resp struct {
102 Links []link `mapstructure:"subnets_links"`
103 }
104
105 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +0200106 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200107 if err != nil {
108 return "", err
109 }
110
111 var url string
112 for _, l := range r.Links {
113 if l.Rel == "next" {
114 url = l.Href
115 }
116 }
117 if url == "" {
118 return "", nil
119 }
120
121 return url, nil
122}
123
Jamie Hannaford686c4962014-09-23 10:46:20 +0200124// IsEmpty checks whether a SubnetPage struct is empty.
125func (p SubnetPage) IsEmpty() (bool, error) {
126 is, err := ExtractSubnets(p)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200127 if err != nil {
128 return true, nil
129 }
130 return len(is) == 0, nil
131}
132
Jamie Hannaford686c4962014-09-23 10:46:20 +0200133// ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
134// and extracts the elements into a slice of Subnet structs. In other words,
135// a generic collection is mapped into a relevant slice.
Jamie Hannaford0708c002014-09-17 16:08:49 +0200136func ExtractSubnets(page pagination.Page) ([]Subnet, error) {
137 var resp struct {
138 Subnets []Subnet `mapstructure:"subnets" json:"subnets"`
139 }
140
141 err := mapstructure.Decode(page.(SubnetPage).Body, &resp)
142 if err != nil {
143 return nil, err
144 }
145
146 return resp.Subnets, nil
147}