blob: 77b956a17effbc974dd5133e7b06a87e534bfa16 [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 {
Ash Wilsonf548aad2014-10-20 08:35:34 -040010 gophercloud.Result
Jamie Hannafordd9036422014-09-23 17:50:24 +020011}
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
Ash Wilsond3dc2542014-10-20 10:10:48 -040023 err := mapstructure.Decode(r.Body, &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 Hannafordd6c81b22014-10-27 14:02:53 +010044type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050045 gophercloud.ErrResult
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010046}
Jamie Hannafordd9036422014-09-23 17:50:24 +020047
Jamie Hannafordf2835402014-09-23 11:01:21 +020048// AllocationPool represents a sub-range of cidr available for dynamic
49// allocation to ports, e.g. {Start: "10.0.0.2", End: "10.0.0.254"}
Jamie Hannaford0708c002014-09-17 16:08:49 +020050type AllocationPool struct {
Jamie Hannaford63631432014-09-18 11:40:09 +020051 Start string `json:"start"`
52 End string `json:"end"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020053}
54
Jamie Hannafordf2835402014-09-23 11:01:21 +020055// HostRoute represents a route that should be used by devices with IPs from
56// a subnet (not including local subnet route).
57type HostRoute struct {
Keith Byrnec1afb432015-09-10 13:43:12 +010058 DestinationCIDR string `mapstructure:"destination" json:"destination"`
59 NextHop string `mapstructure:"nexthop" json:"nexthop"`
Jamie Hannafordf2835402014-09-23 11:01:21 +020060}
61
Jamie Hannaford686c4962014-09-23 10:46:20 +020062// Subnet represents a subnet. See package documentation for a top-level
63// description of what this is.
Jamie Hannaford0708c002014-09-17 16:08:49 +020064type Subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020065 // UUID representing the subnet
66 ID string `mapstructure:"id" json:"id"`
67 // UUID of the parent network
68 NetworkID string `mapstructure:"network_id" json:"network_id"`
69 // Human-readable name for the subnet. Might not be unique.
70 Name string `mapstructure:"name" json:"name"`
71 // IP version, either `4' or `6'
72 IPVersion int `mapstructure:"ip_version" json:"ip_version"`
73 // CIDR representing IP range for this subnet, based on IP version
74 CIDR string `mapstructure:"cidr" json:"cidr"`
75 // Default gateway used by devices in this subnet
76 GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"`
77 // DNS name servers used by hosts in this subnet.
78 DNSNameservers []string `mapstructure:"dns_nameservers" json:"dns_nameservers"`
79 // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool.
Jamie Hannaford0708c002014-09-17 16:08:49 +020080 AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020081 // Routes that should be used by devices with IPs from this subnet (not including local subnet route).
Jamie Hannafordf2835402014-09-23 11:01:21 +020082 HostRoutes []HostRoute `mapstructure:"host_routes" json:"host_routes"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020083 // Specifies whether DHCP is enabled for this subnet or not.
84 EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"`
85 // Owner of network. Only admin users can specify a tenant_id other than its own.
86 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020087}
88
Jamie Hannaford686c4962014-09-23 10:46:20 +020089// SubnetPage is the page returned by a pager when traversing over a collection
90// of subnets.
Jamie Hannaford0708c002014-09-17 16:08:49 +020091type SubnetPage struct {
92 pagination.LinkedPageBase
93}
94
Jamie Hannaford686c4962014-09-23 10:46:20 +020095// NextPageURL is invoked when a paginated collection of subnets has reached
96// the end of a page and the pager seeks to traverse over a new one. In order
97// to do this, it needs to construct the next page's URL.
98func (p SubnetPage) NextPageURL() (string, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020099 type resp struct {
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200100 Links []gophercloud.Link `mapstructure:"subnets_links"`
Jamie Hannaford0708c002014-09-17 16:08:49 +0200101 }
102
103 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +0200104 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200105 if err != nil {
106 return "", err
107 }
108
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200109 return gophercloud.ExtractNextURL(r.Links)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200110}
111
Jamie Hannaford686c4962014-09-23 10:46:20 +0200112// IsEmpty checks whether a SubnetPage struct is empty.
113func (p SubnetPage) IsEmpty() (bool, error) {
114 is, err := ExtractSubnets(p)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200115 if err != nil {
116 return true, nil
117 }
118 return len(is) == 0, nil
119}
120
Jamie Hannaford686c4962014-09-23 10:46:20 +0200121// ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
122// and extracts the elements into a slice of Subnet structs. In other words,
123// a generic collection is mapped into a relevant slice.
Jamie Hannaford0708c002014-09-17 16:08:49 +0200124func ExtractSubnets(page pagination.Page) ([]Subnet, error) {
125 var resp struct {
126 Subnets []Subnet `mapstructure:"subnets" json:"subnets"`
127 }
128
129 err := mapstructure.Decode(page.(SubnetPage).Body, &resp)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200130
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200131 return resp.Subnets, err
Jamie Hannaford0708c002014-09-17 16:08:49 +0200132}