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