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