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 ( |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 11 | type commonResult struct { |
| 12 | gophercloud.CommonResult |
| 13 | } |
| 14 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 15 | // 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] | 16 | func (r commonResult) Extract() (*Subnet, error) { |
| 17 | if r.Err != nil { |
| 18 | return nil, r.Err |
| 19 | } |
| 20 | |
| 21 | var res struct { |
| 22 | Subnet *Subnet `json:"subnet"` |
| 23 | } |
| 24 | |
| 25 | err := mapstructure.Decode(r.Resp, &res) |
| 26 | if err != nil { |
| 27 | return nil, fmt.Errorf("Error decoding Neutron subnet: %v", err) |
| 28 | } |
| 29 | |
| 30 | return res.Subnet, nil |
| 31 | } |
| 32 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 33 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 34 | type CreateResult struct { |
| 35 | commonResult |
| 36 | } |
| 37 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 38 | // GetResult represents the result of a get operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 39 | type GetResult struct { |
| 40 | commonResult |
| 41 | } |
| 42 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 43 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 44 | type UpdateResult struct { |
| 45 | commonResult |
| 46 | } |
| 47 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 48 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 49 | type DeleteResult commonResult |
| 50 | |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 51 | // AllocationPool represents a sub-range of cidr available for dynamic |
| 52 | // 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] | 53 | type AllocationPool struct { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 54 | Start string `json:"start"` |
| 55 | End string `json:"end"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 58 | // HostRoute represents a route that should be used by devices with IPs from |
| 59 | // a subnet (not including local subnet route). |
| 60 | type HostRoute struct { |
| 61 | DestinationCIDR string `json:"destination"` |
| 62 | NextHop string `json:"nexthop"` |
| 63 | } |
| 64 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 65 | // Subnet represents a subnet. See package documentation for a top-level |
| 66 | // description of what this is. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 67 | type Subnet struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 68 | // UUID representing the subnet |
| 69 | ID string `mapstructure:"id" json:"id"` |
| 70 | // UUID of the parent network |
| 71 | NetworkID string `mapstructure:"network_id" json:"network_id"` |
| 72 | // Human-readable name for the subnet. Might not be unique. |
| 73 | Name string `mapstructure:"name" json:"name"` |
| 74 | // IP version, either `4' or `6' |
| 75 | IPVersion int `mapstructure:"ip_version" json:"ip_version"` |
| 76 | // CIDR representing IP range for this subnet, based on IP version |
| 77 | CIDR string `mapstructure:"cidr" json:"cidr"` |
| 78 | // Default gateway used by devices in this subnet |
| 79 | GatewayIP string `mapstructure:"gateway_ip" json:"gateway_ip"` |
| 80 | // DNS name servers used by hosts in this subnet. |
| 81 | DNSNameservers []string `mapstructure:"dns_nameservers" json:"dns_nameservers"` |
| 82 | // Sub-ranges of CIDR available for dynamic allocation to ports. See AllocationPool. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 83 | AllocationPools []AllocationPool `mapstructure:"allocation_pools" json:"allocation_pools"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 84 | // 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] | 85 | HostRoutes []HostRoute `mapstructure:"host_routes" json:"host_routes"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 86 | // Specifies whether DHCP is enabled for this subnet or not. |
| 87 | EnableDHCP bool `mapstructure:"enable_dhcp" json:"enable_dhcp"` |
| 88 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 89 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 92 | // SubnetPage is the page returned by a pager when traversing over a collection |
| 93 | // of subnets. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 94 | type SubnetPage struct { |
| 95 | pagination.LinkedPageBase |
| 96 | } |
| 97 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 98 | // NextPageURL is invoked when a paginated collection of subnets has reached |
| 99 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 100 | // to do this, it needs to construct the next page's URL. |
| 101 | func (p SubnetPage) NextPageURL() (string, error) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 102 | type link struct { |
| 103 | Href string `mapstructure:"href"` |
| 104 | Rel string `mapstructure:"rel"` |
| 105 | } |
| 106 | type resp struct { |
| 107 | Links []link `mapstructure:"subnets_links"` |
| 108 | } |
| 109 | |
| 110 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 111 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 112 | if err != nil { |
| 113 | return "", err |
| 114 | } |
| 115 | |
| 116 | var url string |
| 117 | for _, l := range r.Links { |
| 118 | if l.Rel == "next" { |
| 119 | url = l.Href |
| 120 | } |
| 121 | } |
| 122 | if url == "" { |
| 123 | return "", nil |
| 124 | } |
| 125 | |
| 126 | return url, nil |
| 127 | } |
| 128 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 129 | // IsEmpty checks whether a SubnetPage struct is empty. |
| 130 | func (p SubnetPage) IsEmpty() (bool, error) { |
| 131 | is, err := ExtractSubnets(p) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 132 | if err != nil { |
| 133 | return true, nil |
| 134 | } |
| 135 | return len(is) == 0, nil |
| 136 | } |
| 137 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 138 | // ExtractSubnets accepts a Page struct, specifically a SubnetPage struct, |
| 139 | // and extracts the elements into a slice of Subnet structs. In other words, |
| 140 | // a generic collection is mapped into a relevant slice. |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 141 | func ExtractSubnets(page pagination.Page) ([]Subnet, error) { |
| 142 | var resp struct { |
| 143 | Subnets []Subnet `mapstructure:"subnets" json:"subnets"` |
| 144 | } |
| 145 | |
| 146 | err := mapstructure.Decode(page.(SubnetPage).Body, &resp) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | |
| 151 | return resp.Subnets, nil |
| 152 | } |