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