blob: 3c9ef71370ad49c62394c36f259bc6b35922d035 [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"
Jamie Hannafordd9036422014-09-23 17:50:24 +02005 "github.com/rackspace/gophercloud"
Jamie Hannaford0708c002014-09-17 16:08:49 +02006 "github.com/rackspace/gophercloud/pagination"
7)
8
Jamie Hannafordd9036422014-09-23 17:50:24 +02009type commonResult struct {
10 gophercloud.CommonResult
11}
12
Jamie Hannafordf3114832014-09-24 11:00:43 +020013// Extract is a function that accepts a result and extracts a subnet resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020014func (r commonResult) Extract() (*Subnet, error) {
15 if r.Err != nil {
16 return nil, r.Err
17 }
18
19 var res struct {
20 Subnet *Subnet `json:"subnet"`
21 }
22
23 err := mapstructure.Decode(r.Resp, &res)
Jamie Hannafordd9036422014-09-23 17:50:24 +020024
Jamie Hannafordc1f36492014-10-08 15:48:48 +020025 return res.Subnet, err
Jamie Hannafordd9036422014-09-23 17:50:24 +020026}
27
Jamie Hannafordf3114832014-09-24 11:00:43 +020028// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020029type CreateResult struct {
30 commonResult
31}
32
Jamie Hannafordf3114832014-09-24 11:00:43 +020033// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020034type GetResult struct {
35 commonResult
36}
37
Jamie Hannafordf3114832014-09-24 11:00:43 +020038// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020039type UpdateResult struct {
40 commonResult
41}
42
Jamie Hannafordf3114832014-09-24 11:00:43 +020043// DeleteResult represents the result of a delete operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020044type DeleteResult commonResult
45
Jamie Hannafordf2835402014-09-23 11:01:21 +020046// 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 Hannaford0708c002014-09-17 16:08:49 +020048type AllocationPool struct {
Jamie Hannaford63631432014-09-18 11:40:09 +020049 Start string `json:"start"`
50 End string `json:"end"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020051}
52
Jamie Hannafordf2835402014-09-23 11:01:21 +020053// HostRoute represents a route that should be used by devices with IPs from
54// a subnet (not including local subnet route).
55type HostRoute struct {
56 DestinationCIDR string `json:"destination"`
57 NextHop string `json:"nexthop"`
58}
59
Jamie Hannaford686c4962014-09-23 10:46:20 +020060// Subnet represents a subnet. See package documentation for a top-level
61// description of what this is.
Jamie Hannaford0708c002014-09-17 16:08:49 +020062type Subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020063 // 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 Hannaford0708c002014-09-17 16:08:49 +020078 AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020079 // Routes that should be used by devices with IPs from this subnet (not including local subnet route).
Jamie Hannafordf2835402014-09-23 11:01:21 +020080 HostRoutes []HostRoute `mapstructure:"host_routes" json:"host_routes"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020081 // 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 Hannaford0708c002014-09-17 16:08:49 +020085}
86
Jamie Hannaford686c4962014-09-23 10:46:20 +020087// SubnetPage is the page returned by a pager when traversing over a collection
88// of subnets.
Jamie Hannaford0708c002014-09-17 16:08:49 +020089type SubnetPage struct {
90 pagination.LinkedPageBase
91}
92
Jamie Hannaford686c4962014-09-23 10:46:20 +020093// 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.
96func (p SubnetPage) NextPageURL() (string, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020097 type resp struct {
Jamie Hannafordc1f36492014-10-08 15:48:48 +020098 Links []gophercloud.Link `mapstructure:"subnets_links"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020099 }
100
101 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +0200102 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200103 if err != nil {
104 return "", err
105 }
106
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200107 return gophercloud.ExtractNextURL(r.Links)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200108}
109
Jamie Hannaford686c4962014-09-23 10:46:20 +0200110// IsEmpty checks whether a SubnetPage struct is empty.
111func (p SubnetPage) IsEmpty() (bool, error) {
112 is, err := ExtractSubnets(p)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200113 if err != nil {
114 return true, nil
115 }
116 return len(is) == 0, nil
117}
118
Jamie Hannaford686c4962014-09-23 10:46:20 +0200119// ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
120// and extracts the elements into a slice of Subnet structs. In other words,
121// a generic collection is mapped into a relevant slice.
Jamie Hannaford0708c002014-09-17 16:08:49 +0200122func ExtractSubnets(page pagination.Page) ([]Subnet, error) {
123 var resp struct {
124 Subnets []Subnet `mapstructure:"subnets" json:"subnets"`
125 }
126
127 err := mapstructure.Decode(page.(SubnetPage).Body, &resp)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200128
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200129 return resp.Subnets, err
Jamie Hannaford0708c002014-09-17 16:08:49 +0200130}