Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 1 | package networks |
| 2 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 8 | // Network represents, well, a network. |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 9 | type Network struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 10 | // UUID for the network |
| 11 | ID string `mapstructure:"id" json:"id"` |
| 12 | // Human-readable name for the network. Might not be unique. |
| 13 | Name string `mapstructure:"name" json:"name"` |
| 14 | // The administrative state of network. If false (down), the network does not forward packets. |
| 15 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 16 | // Indicates whether network is currently operational. Possible values include |
| 17 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
| 18 | Status string `mapstructure:"status" json:"status"` |
| 19 | // Subnets associated with this network. |
| 20 | Subnets []string `mapstructure:"subnets" json:"subnets"` |
| 21 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 22 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 23 | // Specifies whether the network resource can be accessed by any tenant or not. |
| 24 | Shared bool `mapstructure:"shared" json:"shared"` |
Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 25 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 26 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 27 | // NetworkPage is the page returned by a pager when traversing over a |
| 28 | // collection of networks. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 29 | type NetworkPage struct { |
| 30 | pagination.LinkedPageBase |
| 31 | } |
| 32 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 33 | // NextPageURL is invoked when a paginated collection of networks has reached |
| 34 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 35 | // to do this, it needs to construct the next page's URL. |
| 36 | func (p NetworkPage) NextPageURL() (string, error) { |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 37 | type link struct { |
| 38 | Href string `mapstructure:"href"` |
| 39 | Rel string `mapstructure:"rel"` |
| 40 | } |
| 41 | type resp struct { |
| 42 | Links []link `mapstructure:"networks_links"` |
| 43 | } |
| 44 | |
| 45 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 46 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 47 | if err != nil { |
| 48 | return "", err |
| 49 | } |
| 50 | |
| 51 | var url string |
| 52 | for _, l := range r.Links { |
| 53 | if l.Rel == "next" { |
| 54 | url = l.Href |
| 55 | } |
| 56 | } |
| 57 | if url == "" { |
| 58 | return "", nil |
| 59 | } |
| 60 | |
| 61 | return url, nil |
| 62 | } |
| 63 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 64 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 65 | func (p NetworkPage) IsEmpty() (bool, error) { |
| 66 | is, err := ExtractNetworks(p) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 67 | if err != nil { |
| 68 | return true, nil |
| 69 | } |
| 70 | return len(is) == 0, nil |
| 71 | } |
| 72 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 73 | // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct, |
| 74 | // and extracts the elements into a slice of Network structs. In other words, |
| 75 | // a generic collection is mapped into a relevant slice. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 76 | func ExtractNetworks(page pagination.Page) ([]Network, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 77 | var resp struct { |
| 78 | Networks []Network `mapstructure:"networks" json:"networks"` |
| 79 | } |
| 80 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 81 | err := mapstructure.Decode(page.(NetworkPage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | return resp.Networks, nil |
| 87 | } |