blob: d916a1c36cded71eada846c0924c2626d8d2850c [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 {
Jamie Hannaford63631432014-09-18 11:40:09 +02009 Start string `json:"start"`
10 End string `json:"end"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020011}
12
13type Subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020014 // UUID representing the subnet
15 ID string `mapstructure:"id" json:"id"`
16 // UUID of the parent network
17 NetworkID string `mapstructure:"network_id" json:"network_id"`
18 // Human-readable name for the subnet. Might not be unique.
19 Name string `mapstructure:"name" json:"name"`
20 // IP version, either `4' or `6'
21 IPVersion int `mapstructure:"ip_version" json:"ip_version"`
22 // CIDR representing IP range for this subnet, based on IP version
23 CIDR string `mapstructure:"cidr" json:"cidr"`
24 // Default gateway used by devices in this subnet
25 GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"`
26 // DNS name servers used by hosts in this subnet.
27 DNSNameservers []string `mapstructure:"dns_nameservers" json:"dns_nameservers"`
28 // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool.
Jamie Hannaford0708c002014-09-17 16:08:49 +020029 AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020030 // Routes that should be used by devices with IPs from this subnet (not including local subnet route).
31 HostRoutes []interface{} `mapstructure:"host_routes" json:"host_routes"`
32 // Specifies whether DHCP is enabled for this subnet or not.
33 EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"`
34 // Owner of network. Only admin users can specify a tenant_id other than its own.
35 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020036}
37
38type SubnetPage struct {
39 pagination.LinkedPageBase
40}
41
42func (current SubnetPage) NextPageURL() (string, error) {
43 type link struct {
44 Href string `mapstructure:"href"`
45 Rel string `mapstructure:"rel"`
46 }
47 type resp struct {
48 Links []link `mapstructure:"subnets_links"`
49 }
50
51 var r resp
52 err := mapstructure.Decode(current.Body, &r)
53 if err != nil {
54 return "", err
55 }
56
57 var url string
58 for _, l := range r.Links {
59 if l.Rel == "next" {
60 url = l.Href
61 }
62 }
63 if url == "" {
64 return "", nil
65 }
66
67 return url, nil
68}
69
70func (r SubnetPage) IsEmpty() (bool, error) {
71 is, err := ExtractSubnets(r)
72 if err != nil {
73 return true, nil
74 }
75 return len(is) == 0, nil
76}
77
78func ExtractSubnets(page pagination.Page) ([]Subnet, error) {
79 var resp struct {
80 Subnets []Subnet `mapstructure:"subnets" json:"subnets"`
81 }
82
83 err := mapstructure.Decode(page.(SubnetPage).Body, &resp)
84 if err != nil {
85 return nil, err
86 }
87
88 return resp.Subnets, nil
89}