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 | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 8 | // NetworkProvider represents provider extension data |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 9 | type NetworkProvider struct { |
| 10 | ProviderSegmentationID int `json:"provider:segmentation_id"` |
Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 11 | ProviderPhysicalNetwork string `json:"provider:physical_network"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 12 | ProviderNetworkType string `json:"provider:network_type"` |
| 13 | } |
| 14 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 15 | // Network represents, well, a network. |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 16 | type Network struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 17 | // UUID for the network |
| 18 | ID string `mapstructure:"id" json:"id"` |
| 19 | // Human-readable name for the network. Might not be unique. |
| 20 | Name string `mapstructure:"name" json:"name"` |
| 21 | // The administrative state of network. If false (down), the network does not forward packets. |
| 22 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 23 | // Indicates whether network is currently operational. Possible values include |
| 24 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
| 25 | Status string `mapstructure:"status" json:"status"` |
| 26 | // Subnets associated with this network. |
| 27 | Subnets []string `mapstructure:"subnets" json:"subnets"` |
| 28 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 29 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 30 | // Specifies whether the network resource can be accessed by any tenant or not. |
| 31 | Shared bool `mapstructure:"shared" json:"shared"` |
| 32 | |
| 33 | ProviderSegmentationID int `mapstructure:"provider:segmentation_id" json:"provider:segmentation_id"` |
| 34 | ProviderPhysicalNetwork string `mapstructure:"provider:physical_network" json:"provider:physical_network"` |
| 35 | ProviderNetworkType string `mapstructure:"provider:network_type" json:"provider:network_type"` |
| 36 | RouterExternal bool `mapstructure:"router:external" json:"router:external"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 39 | // NetworkCreateResult represents what is returned by a create operation. |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 40 | type NetworkCreateResult struct { |
| 41 | Network |
| 42 | Segments []NetworkProvider `json:"segments"` |
| 43 | PortSecurityEnabled bool `json:"port_security_enabled"` |
Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 44 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 45 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 46 | // NetworkPage is the page returned by a pager when traversing over a |
| 47 | // collection of networks. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 48 | type NetworkPage struct { |
| 49 | pagination.LinkedPageBase |
| 50 | } |
| 51 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 52 | // NextPageURL is invoked when a paginated collection of networks has reached |
| 53 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 54 | // to do this, it needs to construct the next page's URL. |
| 55 | func (p NetworkPage) NextPageURL() (string, error) { |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 56 | type link struct { |
| 57 | Href string `mapstructure:"href"` |
| 58 | Rel string `mapstructure:"rel"` |
| 59 | } |
| 60 | type resp struct { |
| 61 | Links []link `mapstructure:"networks_links"` |
| 62 | } |
| 63 | |
| 64 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 65 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 66 | if err != nil { |
| 67 | return "", err |
| 68 | } |
| 69 | |
| 70 | var url string |
| 71 | for _, l := range r.Links { |
| 72 | if l.Rel == "next" { |
| 73 | url = l.Href |
| 74 | } |
| 75 | } |
| 76 | if url == "" { |
| 77 | return "", nil |
| 78 | } |
| 79 | |
| 80 | return url, nil |
| 81 | } |
| 82 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame^] | 83 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 84 | func (p NetworkPage) IsEmpty() (bool, error) { |
| 85 | is, err := ExtractNetworks(p) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 86 | if err != nil { |
| 87 | return true, nil |
| 88 | } |
| 89 | return len(is) == 0, nil |
| 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. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 95 | func ExtractNetworks(page pagination.Page) ([]Network, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 96 | var resp struct { |
| 97 | Networks []Network `mapstructure:"networks" json:"networks"` |
| 98 | } |
| 99 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 100 | err := mapstructure.Decode(page.(NetworkPage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | return resp.Networks, nil |
| 106 | } |