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"` |
| 55 | // Human-readable name for the network. Might not be unique. |
| 56 | Name string `mapstructure:"name" json:"name"` |
| 57 | // The administrative state of network. If false (down), the network does not forward packets. |
| 58 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 59 | // Indicates whether network is currently operational. Possible values include |
| 60 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
| 61 | Status string `mapstructure:"status" json:"status"` |
| 62 | // Subnets associated with this network. |
| 63 | Subnets []string `mapstructure:"subnets" json:"subnets"` |
| 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"` |
| 66 | // Specifies whether the network resource can be accessed by any tenant or not. |
| 67 | Shared bool `mapstructure:"shared" json:"shared"` |
Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 68 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 69 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 70 | // NetworkPage is the page returned by a pager when traversing over a |
| 71 | // collection of networks. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 72 | type NetworkPage struct { |
| 73 | pagination.LinkedPageBase |
| 74 | } |
| 75 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 76 | // NextPageURL is invoked when a paginated collection of networks has reached |
| 77 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 78 | // to do this, it needs to construct the next page's URL. |
| 79 | func (p NetworkPage) NextPageURL() (string, error) { |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 80 | type link struct { |
| 81 | Href string `mapstructure:"href"` |
| 82 | Rel string `mapstructure:"rel"` |
| 83 | } |
| 84 | type resp struct { |
| 85 | Links []link `mapstructure:"networks_links"` |
| 86 | } |
| 87 | |
| 88 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 89 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 90 | if err != nil { |
| 91 | return "", err |
| 92 | } |
| 93 | |
| 94 | var url string |
| 95 | for _, l := range r.Links { |
| 96 | if l.Rel == "next" { |
| 97 | url = l.Href |
| 98 | } |
| 99 | } |
| 100 | if url == "" { |
| 101 | return "", nil |
| 102 | } |
| 103 | |
| 104 | return url, nil |
| 105 | } |
| 106 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 107 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 108 | func (p NetworkPage) IsEmpty() (bool, error) { |
| 109 | is, err := ExtractNetworks(p) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 110 | if err != nil { |
| 111 | return true, nil |
| 112 | } |
| 113 | return len(is) == 0, nil |
| 114 | } |
| 115 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 116 | // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct, |
| 117 | // and extracts the elements into a slice of Network structs. In other words, |
| 118 | // a generic collection is mapped into a relevant slice. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 119 | func ExtractNetworks(page pagination.Page) ([]Network, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 120 | var resp struct { |
| 121 | Networks []Network `mapstructure:"networks" json:"networks"` |
| 122 | } |
| 123 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 124 | err := mapstructure.Decode(page.(NetworkPage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | |
| 129 | return resp.Networks, nil |
| 130 | } |