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