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 ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
| 7 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 8 | // AllocationPool is a sub-struct that represents an allocation pool |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 9 | type AllocationPool struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 10 | Start string `json:"start"` |
| 11 | End string `json:"end"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 12 | } |
| 13 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 14 | // Subnet represents a subnet. See package documentation for a top-level |
| 15 | // description of what this is. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 16 | type Subnet struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 17 | // UUID representing the subnet |
| 18 | ID string `mapstructure:"id" json:"id"` |
| 19 | // UUID of the parent network |
| 20 | NetworkID string `mapstructure:"network_id" json:"network_id"` |
| 21 | // Human-readable name for the subnet. Might not be unique. |
| 22 | Name string `mapstructure:"name" json:"name"` |
| 23 | // IP version, either `4' or `6' |
| 24 | IPVersion int `mapstructure:"ip_version" json:"ip_version"` |
| 25 | // CIDR representing IP range for this subnet, based on IP version |
| 26 | CIDR string `mapstructure:"cidr" json:"cidr"` |
| 27 | // Default gateway used by devices in this subnet |
| 28 | GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"` |
| 29 | // DNS name servers used by hosts in this subnet. |
| 30 | DNSNameservers []string `mapstructure:"dns_nameservers" json:"dns_nameservers"` |
| 31 | // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 32 | AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 33 | // Routes that should be used by devices with IPs from this subnet (not including local subnet route). |
| 34 | HostRoutes []interface{} `mapstructure:"host_routes" json:"host_routes"` |
| 35 | // Specifies whether DHCP is enabled for this subnet or not. |
| 36 | EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"` |
| 37 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 38 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 41 | // SubnetPage is the page returned by a pager when traversing over a collection |
| 42 | // of subnets. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 43 | type SubnetPage struct { |
| 44 | pagination.LinkedPageBase |
| 45 | } |
| 46 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 47 | // NextPageURL is invoked when a paginated collection of subnets has reached |
| 48 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 49 | // to do this, it needs to construct the next page's URL. |
| 50 | func (p SubnetPage) NextPageURL() (string, error) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 51 | type link struct { |
| 52 | Href string `mapstructure:"href"` |
| 53 | Rel string `mapstructure:"rel"` |
| 54 | } |
| 55 | type resp struct { |
| 56 | Links []link `mapstructure:"subnets_links"` |
| 57 | } |
| 58 | |
| 59 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 60 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 61 | if err != nil { |
| 62 | return "", err |
| 63 | } |
| 64 | |
| 65 | var url string |
| 66 | for _, l := range r.Links { |
| 67 | if l.Rel == "next" { |
| 68 | url = l.Href |
| 69 | } |
| 70 | } |
| 71 | if url == "" { |
| 72 | return "", nil |
| 73 | } |
| 74 | |
| 75 | return url, nil |
| 76 | } |
| 77 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 78 | // IsEmpty checks whether a SubnetPage struct is empty. |
| 79 | func (p SubnetPage) IsEmpty() (bool, error) { |
| 80 | is, err := ExtractSubnets(p) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 81 | if err != nil { |
| 82 | return true, nil |
| 83 | } |
| 84 | return len(is) == 0, nil |
| 85 | } |
| 86 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 87 | // ExtractSubnets accepts a Page struct, specifically a SubnetPage struct, |
| 88 | // and extracts the elements into a slice of Subnet structs. In other words, |
| 89 | // a generic collection is mapped into a relevant slice. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 90 | func ExtractSubnets(page pagination.Page) ([]Subnet, error) { |
| 91 | var resp struct { |
| 92 | Subnets []Subnet `mapstructure:"subnets" json:"subnets"` |
| 93 | } |
| 94 | |
| 95 | err := mapstructure.Decode(page.(SubnetPage).Body, &resp) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | return resp.Subnets, nil |
| 101 | } |