Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 1 | package networks |
| 2 | |
| 3 | import ( |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | type commonResult struct { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 10 | gophercloud.Result |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | // Extract is a function that accepts a result and extracts a network resource. |
| 14 | func (r commonResult) Extract() (*Network, error) { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 15 | if r.Err != nil { |
| 16 | return nil, r.Err |
| 17 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 18 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 19 | var res struct { |
| 20 | Network *Network `json:"network"` |
| 21 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 22 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 23 | err := mapstructure.Decode(r.Body, &res) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 24 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 25 | return res.Network, err |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // CreateResult represents the result of a create operation. |
| 29 | type CreateResult struct { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 30 | commonResult |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // GetResult represents the result of a get operation. |
| 34 | type GetResult struct { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 35 | commonResult |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | b65675f | 2014-10-27 14:03:28 +0100 | [diff] [blame] | 39 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 40 | gophercloud.ErrResult |
Jamie Hannaford | b65675f | 2014-10-27 14:03:28 +0100 | [diff] [blame] | 41 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 42 | |
| 43 | // Network represents, well, a network. |
| 44 | type Network struct { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 45 | // UUID for the network |
| 46 | ID string `mapstructure:"id" json:"id"` |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 47 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 48 | // Human-readable name for the network. Might not be unique. |
| 49 | Label string `mapstructure:"label" json:"label"` |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 50 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 51 | // Classless Inter-Domain Routing |
| 52 | CIDR string `mapstructure:"cidr" json:"cidr"` |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // NetworkPage is the page returned by a pager when traversing over a |
| 56 | // collection of networks. |
| 57 | type NetworkPage struct { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 58 | pagination.SinglePageBase |
| 59 | } |
| 60 | |
| 61 | // IsEmpty returns true if the NetworkPage contains no Networks. |
| 62 | func (r NetworkPage) IsEmpty() (bool, error) { |
| 63 | networks, err := ExtractNetworks(r) |
| 64 | if err != nil { |
| 65 | return true, err |
| 66 | } |
| 67 | return len(networks) == 0, nil |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct, |
| 71 | // and extracts the elements into a slice of Network structs. In other words, |
| 72 | // a generic collection is mapped into a relevant slice. |
| 73 | func ExtractNetworks(page pagination.Page) ([]Network, error) { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 74 | var resp struct { |
| 75 | Networks []Network `mapstructure:"networks" json:"networks"` |
| 76 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 77 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 78 | err := mapstructure.Decode(page.(NetworkPage).Body, &resp) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 79 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 80 | return resp.Networks, err |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 81 | } |