Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | package subnets |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 11 | type commonResult struct { |
| 12 | gophercloud.CommonResult |
| 13 | } |
| 14 | |
| 15 | func (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 | |
| 32 | type CreateResult struct { |
| 33 | commonResult |
| 34 | } |
| 35 | |
| 36 | type GetResult struct { |
| 37 | commonResult |
| 38 | } |
| 39 | |
| 40 | type UpdateResult struct { |
| 41 | commonResult |
| 42 | } |
| 43 | |
| 44 | type DeleteResult commonResult |
| 45 | |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 46 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 48 | type AllocationPool struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 49 | Start string `json:"start"` |
| 50 | End string `json:"end"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 53 | // HostRoute represents a route that should be used by devices with IPs from |
| 54 | // a subnet (not including local subnet route). |
| 55 | type HostRoute struct { |
| 56 | DestinationCIDR string `json:"destination"` |
| 57 | NextHop string `json:"nexthop"` |
| 58 | } |
| 59 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 60 | // Subnet represents a subnet. See package documentation for a top-level |
| 61 | // description of what this is. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 62 | type Subnet struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 63 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 78 | AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 79 | // Routes that should be used by devices with IPs from this subnet (not including local subnet route). |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 80 | HostRoutes []HostRoute `mapstructure:"host_routes" json:"host_routes"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 81 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 87 | // SubnetPage is the page returned by a pager when traversing over a collection |
| 88 | // of subnets. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 89 | type SubnetPage struct { |
| 90 | pagination.LinkedPageBase |
| 91 | } |
| 92 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 93 | // 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. |
| 96 | func (p SubnetPage) NextPageURL() (string, error) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 97 | 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 Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 106 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 107 | 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 Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 124 | // IsEmpty checks whether a SubnetPage struct is empty. |
| 125 | func (p SubnetPage) IsEmpty() (bool, error) { |
| 126 | is, err := ExtractSubnets(p) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 127 | if err != nil { |
| 128 | return true, nil |
| 129 | } |
| 130 | return len(is) == 0, nil |
| 131 | } |
| 132 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 133 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 136 | func 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 | } |