Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | package subnets |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 9 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 10 | gophercloud.Result |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 11 | } |
| 12 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 13 | // Extract is a function that accepts a result and extracts a subnet resource. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 14 | func (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 Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 23 | err := mapstructure.Decode(r.Body, &res) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 24 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 25 | return res.Subnet, err |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 26 | } |
| 27 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 28 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 29 | type CreateResult struct { |
| 30 | commonResult |
| 31 | } |
| 32 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 33 | // GetResult represents the result of a get operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 34 | type GetResult struct { |
| 35 | commonResult |
| 36 | } |
| 37 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 38 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 39 | type UpdateResult struct { |
| 40 | commonResult |
| 41 | } |
| 42 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 43 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | d6c81b2 | 2014-10-27 14:02:53 +0100 | [diff] [blame] | 44 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 45 | gophercloud.ErrResult |
Jamie Hannaford | d6c81b2 | 2014-10-27 14:02:53 +0100 | [diff] [blame] | 46 | } |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 47 | |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 48 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 50 | type AllocationPool struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 51 | Start string `json:"start"` |
| 52 | End string `json:"end"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 55 | // HostRoute represents a route that should be used by devices with IPs from |
| 56 | // a subnet (not including local subnet route). |
| 57 | type HostRoute struct { |
Keith Byrne | c1afb43 | 2015-09-10 13:43:12 +0100 | [diff] [blame^] | 58 | DestinationCIDR string `mapstructure:"destination" json:"destination"` |
| 59 | NextHop string `mapstructure:"nexthop" json:"nexthop"` |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 62 | // Subnet represents a subnet. See package documentation for a top-level |
| 63 | // description of what this is. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 64 | type Subnet struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 65 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 80 | AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 81 | // Routes that should be used by devices with IPs from this subnet (not including local subnet route). |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 82 | HostRoutes []HostRoute `mapstructure:"host_routes" json:"host_routes"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 83 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 89 | // SubnetPage is the page returned by a pager when traversing over a collection |
| 90 | // of subnets. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 91 | type SubnetPage struct { |
| 92 | pagination.LinkedPageBase |
| 93 | } |
| 94 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 95 | // 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. |
| 98 | func (p SubnetPage) NextPageURL() (string, error) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 99 | type resp struct { |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 100 | Links []gophercloud.Link `mapstructure:"subnets_links"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 104 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 105 | if err != nil { |
| 106 | return "", err |
| 107 | } |
| 108 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 109 | return gophercloud.ExtractNextURL(r.Links) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 112 | // IsEmpty checks whether a SubnetPage struct is empty. |
| 113 | func (p SubnetPage) IsEmpty() (bool, error) { |
| 114 | is, err := ExtractSubnets(p) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 115 | if err != nil { |
| 116 | return true, nil |
| 117 | } |
| 118 | return len(is) == 0, nil |
| 119 | } |
| 120 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 121 | // 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 124 | func 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 Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 130 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 131 | return resp.Subnets, err |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 132 | } |