blob: 1790028af0f20f4536d19c79fcf08de6c353733f [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud/pagination"
6)
7
8type AllocationPool struct {
9 Start string
10 End string
11}
12
13type 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
27type SubnetPage struct {
28 pagination.LinkedPageBase
29}
30
31func (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
59func (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
67func 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}