Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 1 | package tenantnetworks |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // A Network represents a nova-network that an instance communicates on |
| 9 | type Network struct { |
| 10 | // CIDR is the IPv4 subnet. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 11 | CIDR string `json:"cidr"` |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 12 | |
| 13 | // ID is the UUID of the network. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 14 | ID string `json:"id"` |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 15 | |
| 16 | // Name is the common name that the network has. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 17 | Name string `json:"label"` |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // NetworkPage stores a single, only page of Networks |
| 21 | // results from a List call. |
| 22 | type NetworkPage struct { |
| 23 | pagination.SinglePageBase |
| 24 | } |
| 25 | |
| 26 | // IsEmpty determines whether or not a NetworkPage is empty. |
| 27 | func (page NetworkPage) IsEmpty() (bool, error) { |
| 28 | va, err := ExtractNetworks(page) |
| 29 | return len(va) == 0, err |
| 30 | } |
| 31 | |
| 32 | // ExtractNetworks interprets a page of results as a slice of Networks |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 33 | func ExtractNetworks(r pagination.Page) ([]Network, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 34 | var s struct { |
| 35 | Networks []Network `json:"networks"` |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 36 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 37 | err := (r.(NetworkPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 38 | return s.Networks, err |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type NetworkResult struct { |
| 42 | gophercloud.Result |
| 43 | } |
| 44 | |
| 45 | // Extract is a method that attempts to interpret any Network resource |
| 46 | // response as a Network struct. |
| 47 | func (r NetworkResult) Extract() (*Network, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 48 | var s struct { |
| 49 | Network *Network `json:"network"` |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 50 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 51 | err := r.ExtractInto(&s) |
| 52 | return s.Network, err |
Joe Topjian | c21202d | 2015-02-27 21:32:58 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 56 | // as a Network. |
| 57 | type GetResult struct { |
| 58 | NetworkResult |
| 59 | } |