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 | |
| 8 | type AllocationPool struct { |
| 9 | Start string |
| 10 | End string |
| 11 | } |
| 12 | |
| 13 | type Subnet struct { |
| 14 | Name string `mapstructure:"name" json:"name"` |
| 15 | EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"` |
| 16 | NetworkID string `mapstructure:"network_id" json:"network_id"` |
| 17 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 18 | DNSNameservers []interface{} `mapstructure:"dns_nameservers" json:"dns_nameservers"` |
| 19 | AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"` |
| 20 | HostRoutes []interface{} `mapstructure:"host_routes" json:"host_routes"` |
| 21 | IPVersion int `mapstructure:"ip_version" json:"ip_version"` |
| 22 | GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"` |
| 23 | CIDR string `mapstructure:"cidr" json:"cidr"` |
| 24 | ID string `mapstructure:"id" json:"id"` |
| 25 | } |
| 26 | |
| 27 | type SubnetPage struct { |
| 28 | pagination.LinkedPageBase |
| 29 | } |
| 30 | |
| 31 | func (current SubnetPage) NextPageURL() (string, error) { |
| 32 | type link struct { |
| 33 | Href string `mapstructure:"href"` |
| 34 | Rel string `mapstructure:"rel"` |
| 35 | } |
| 36 | type resp struct { |
| 37 | Links []link `mapstructure:"subnets_links"` |
| 38 | } |
| 39 | |
| 40 | var r resp |
| 41 | err := mapstructure.Decode(current.Body, &r) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | |
| 46 | var url string |
| 47 | for _, l := range r.Links { |
| 48 | if l.Rel == "next" { |
| 49 | url = l.Href |
| 50 | } |
| 51 | } |
| 52 | if url == "" { |
| 53 | return "", nil |
| 54 | } |
| 55 | |
| 56 | return url, nil |
| 57 | } |
| 58 | |
| 59 | func (r SubnetPage) IsEmpty() (bool, error) { |
| 60 | is, err := ExtractSubnets(r) |
| 61 | if err != nil { |
| 62 | return true, nil |
| 63 | } |
| 64 | return len(is) == 0, nil |
| 65 | } |
| 66 | |
| 67 | func ExtractSubnets(page pagination.Page) ([]Subnet, error) { |
| 68 | var resp struct { |
| 69 | Subnets []Subnet `mapstructure:"subnets" json:"subnets"` |
| 70 | } |
| 71 | |
| 72 | err := mapstructure.Decode(page.(SubnetPage).Body, &resp) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
| 77 | return resp.Subnets, nil |
| 78 | } |