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 ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 5 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 8 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 9 | gophercloud.Result |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 10 | } |
| 11 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 12 | // Extract is a function that accepts a result and extracts a network resource. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 13 | func (r commonResult) Extract() (*Network, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 14 | var s struct { |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 15 | Network *Network `json:"network"` |
| 16 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 17 | err := r.ExtractInto(&s) |
| 18 | return s.Network, err |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 19 | } |
| 20 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 21 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 22 | type CreateResult struct { |
| 23 | commonResult |
| 24 | } |
| 25 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 26 | // GetResult represents the result of a get operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 27 | type GetResult struct { |
| 28 | commonResult |
| 29 | } |
| 30 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 31 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 32 | type UpdateResult struct { |
| 33 | commonResult |
| 34 | } |
| 35 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 36 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | d6c81b2 | 2014-10-27 14:02:53 +0100 | [diff] [blame] | 37 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 38 | gophercloud.ErrResult |
Jamie Hannaford | d6c81b2 | 2014-10-27 14:02:53 +0100 | [diff] [blame] | 39 | } |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 40 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 41 | // Network represents, well, a network. |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 42 | type Network struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 43 | // UUID for the network |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 44 | ID string `json:"id"` |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 45 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 46 | // Human-readable name for the network. Might not be unique. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 47 | Name string `json:"name"` |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 48 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 49 | // The administrative state of network. If false (down), the network does not forward packets. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 50 | AdminStateUp bool `json:"admin_state_up"` |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 51 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 52 | // Indicates whether network is currently operational. Possible values include |
| 53 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 54 | Status string `json:"status"` |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 55 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 56 | // Subnets associated with this network. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 57 | Subnets []string `json:"subnets"` |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 58 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 59 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 60 | TenantID string `json:"tenant_id"` |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 61 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 62 | // Specifies whether the network resource can be accessed by any tenant or not. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 63 | Shared bool `json:"shared"` |
Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 64 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 65 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 66 | // NetworkPage is the page returned by a pager when traversing over a |
| 67 | // collection of networks. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 68 | type NetworkPage struct { |
| 69 | pagination.LinkedPageBase |
| 70 | } |
| 71 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 72 | // NextPageURL is invoked when a paginated collection of networks has reached |
| 73 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 74 | // to do this, it needs to construct the next page's URL. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 75 | func (r NetworkPage) NextPageURL() (string, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 76 | var s struct { |
| 77 | Links []gophercloud.Link `json:"networks_links"` |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 78 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 79 | err := r.ExtractInto(&s) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 80 | if err != nil { |
| 81 | return "", err |
| 82 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 83 | return gophercloud.ExtractNextURL(s.Links) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 86 | // IsEmpty checks whether a NetworkPage struct is empty. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 87 | func (r NetworkPage) IsEmpty() (bool, error) { |
| 88 | is, err := ExtractNetworks(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 89 | return len(is) == 0, err |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 92 | // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct, |
| 93 | // and extracts the elements into a slice of Network structs. In other words, |
| 94 | // a generic collection is mapped into a relevant slice. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 95 | func ExtractNetworks(r pagination.Page) ([]Network, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 96 | var s struct { |
| 97 | Networks []Network `json:"networks"` |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 98 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 99 | err := (r.(NetworkPage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 100 | return s.Networks, err |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 101 | } |