blob: 2bb14d730a1bb8d9f21ed079d230cca5ba802bd4 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
Jamie Hannaford0708c002014-09-17 16:08:49 +02002
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jamie Hannaford0708c002014-09-17 16:08:49 +02006)
7
Jamie Hannafordd9036422014-09-23 17:50:24 +02008type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -04009 gophercloud.Result
Jamie Hannafordd9036422014-09-23 17:50:24 +020010}
11
Jamie Hannafordf3114832014-09-24 11:00:43 +020012// Extract is a function that accepts a result and extracts a subnet resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020013func (r commonResult) Extract() (*Subnet, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060014 var s struct {
Jamie Hannafordd9036422014-09-23 17:50:24 +020015 Subnet *Subnet `json:"subnet"`
16 }
Jon Perritt3c166472016-02-25 03:07:41 -060017 err := r.ExtractInto(&s)
18 return s.Subnet, err
Jamie Hannafordd9036422014-09-23 17:50:24 +020019}
20
Jamie Hannafordf3114832014-09-24 11:00:43 +020021// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020022type CreateResult struct {
23 commonResult
24}
25
Jamie Hannafordf3114832014-09-24 11:00:43 +020026// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020027type GetResult struct {
28 commonResult
29}
30
Jamie Hannafordf3114832014-09-24 11:00:43 +020031// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020032type UpdateResult struct {
33 commonResult
34}
35
Jamie Hannafordf3114832014-09-24 11:00:43 +020036// DeleteResult represents the result of a delete operation.
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010037type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050038 gophercloud.ErrResult
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010039}
Jamie Hannafordd9036422014-09-23 17:50:24 +020040
Jamie Hannafordf2835402014-09-23 11:01:21 +020041// AllocationPool represents a sub-range of cidr available for dynamic
42// allocation to ports, e.g. {Start: "10.0.0.2", End: "10.0.0.254"}
Jamie Hannaford0708c002014-09-17 16:08:49 +020043type AllocationPool struct {
Jamie Hannaford63631432014-09-18 11:40:09 +020044 Start string `json:"start"`
45 End string `json:"end"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020046}
47
Jamie Hannafordf2835402014-09-23 11:01:21 +020048// HostRoute represents a route that should be used by devices with IPs from
49// a subnet (not including local subnet route).
50type HostRoute struct {
Jon Perritt3c166472016-02-25 03:07:41 -060051 DestinationCIDR string `json:"destination"`
52 NextHop string `json:"nexthop"`
Jamie Hannafordf2835402014-09-23 11:01:21 +020053}
54
Jamie Hannaford686c4962014-09-23 10:46:20 +020055// Subnet represents a subnet. See package documentation for a top-level
56// description of what this is.
Jamie Hannaford0708c002014-09-17 16:08:49 +020057type Subnet struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020058 // UUID representing the subnet
Jon Perritt3c166472016-02-25 03:07:41 -060059 ID string `json:"id"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020060 // UUID of the parent network
Jon Perritt3c166472016-02-25 03:07:41 -060061 NetworkID string `json:"network_id"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020062 // Human-readable name for the subnet. Might not be unique.
Jon Perritt3c166472016-02-25 03:07:41 -060063 Name string `json:"name"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020064 // IP version, either `4' or `6'
Jon Perritt3c166472016-02-25 03:07:41 -060065 IPVersion int `json:"ip_version"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020066 // CIDR representing IP range for this subnet, based on IP version
Jon Perritt3c166472016-02-25 03:07:41 -060067 CIDR string `json:"cidr"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020068 // Default gateway used by devices in this subnet
Jon Perritt3c166472016-02-25 03:07:41 -060069 GatewayIP string `json:"gateway_ip"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020070 // DNS name servers used by hosts in this subnet.
Jon Perritt3c166472016-02-25 03:07:41 -060071 DNSNameservers []string `json:"dns_nameservers"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020072 // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool.
Jon Perritt3c166472016-02-25 03:07:41 -060073 AllocationPools []AllocationPool `json:"allocation_pools"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020074 // Routes that should be used by devices with IPs from this subnet (not including local subnet route).
Jon Perritt3c166472016-02-25 03:07:41 -060075 HostRoutes []HostRoute `json:"host_routes"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020076 // Specifies whether DHCP is enabled for this subnet or not.
Jon Perritt3c166472016-02-25 03:07:41 -060077 EnableDHCP bool `json:"enable_dhcp"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020078 // Owner of network. Only admin users can specify a tenant_id other than its own.
Jon Perritt3c166472016-02-25 03:07:41 -060079 TenantID string `json:"tenant_id"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020080}
81
Jamie Hannaford686c4962014-09-23 10:46:20 +020082// SubnetPage is the page returned by a pager when traversing over a collection
83// of subnets.
Jamie Hannaford0708c002014-09-17 16:08:49 +020084type SubnetPage struct {
85 pagination.LinkedPageBase
86}
87
Jamie Hannaford686c4962014-09-23 10:46:20 +020088// NextPageURL is invoked when a paginated collection of subnets has reached
89// the end of a page and the pager seeks to traverse over a new one. In order
90// to do this, it needs to construct the next page's URL.
Jon Perritt31b66462016-02-25 22:25:30 -060091func (r SubnetPage) NextPageURL() (string, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060092 var s struct {
93 Links []gophercloud.Link `json:"subnets_links"`
Jamie Hannaford0708c002014-09-17 16:08:49 +020094 }
Jon Perritt31b66462016-02-25 22:25:30 -060095 err := r.ExtractInto(&s)
Jamie Hannaford0708c002014-09-17 16:08:49 +020096 if err != nil {
97 return "", err
98 }
Jon Perritt3c166472016-02-25 03:07:41 -060099 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200100}
101
Jamie Hannaford686c4962014-09-23 10:46:20 +0200102// IsEmpty checks whether a SubnetPage struct is empty.
Jon Perritt31b66462016-02-25 22:25:30 -0600103func (r SubnetPage) IsEmpty() (bool, error) {
104 is, err := ExtractSubnets(r)
Jon Perritt3c166472016-02-25 03:07:41 -0600105 return len(is) == 0, err
Jamie Hannaford0708c002014-09-17 16:08:49 +0200106}
107
Jamie Hannaford686c4962014-09-23 10:46:20 +0200108// ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
109// and extracts the elements into a slice of Subnet structs. In other words,
110// a generic collection is mapped into a relevant slice.
Jon Perritt31b66462016-02-25 22:25:30 -0600111func ExtractSubnets(r pagination.Page) ([]Subnet, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600112 var s struct {
113 Subnets []Subnet `json:"subnets"`
Jamie Hannaford0708c002014-09-17 16:08:49 +0200114 }
Jon Perritt31b66462016-02-25 22:25:30 -0600115 err := (r.(SubnetPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -0600116 return s.Subnets, err
Jamie Hannaford0708c002014-09-17 16:08:49 +0200117}