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