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 | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 15 | Status string `mapstructure:"status" json:"status"` |
| 16 | Subnets []interface{} `mapstructure:"subnets" json:"subnets"` |
| 17 | Name string `mapstructure:"name" json:"name"` |
| 18 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 19 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 20 | Shared bool `mapstructure:"shared" json:"shared"` |
| 21 | ID string `mapstructure:"id" json:"id"` |
| 22 | ProviderSegmentationID int `mapstructure:"provider:segmentation_id" json:"provider:segmentation_id"` |
| 23 | ProviderPhysicalNetwork string `mapstructure:"provider:physical_network" json:"provider:physical_network"` |
| 24 | ProviderNetworkType string `mapstructure:"provider:network_type" json:"provider:network_type"` |
| 25 | RouterExternal bool `mapstructure:"router:external" json:"router:external"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type NetworkCreateResult struct { |
| 29 | Network |
| 30 | Segments []NetworkProvider `json:"segments"` |
| 31 | PortSecurityEnabled bool `json:"port_security_enabled"` |
Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 32 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 33 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 34 | type NetworkPage struct { |
| 35 | pagination.LinkedPageBase |
| 36 | } |
| 37 | |
| 38 | func (current NetworkPage) NextPageURL() (string, error) { |
| 39 | type link struct { |
| 40 | Href string `mapstructure:"href"` |
| 41 | Rel string `mapstructure:"rel"` |
| 42 | } |
| 43 | type resp struct { |
| 44 | Links []link `mapstructure:"networks_links"` |
| 45 | } |
| 46 | |
| 47 | var r resp |
| 48 | err := mapstructure.Decode(current.Body, &r) |
| 49 | if err != nil { |
| 50 | return "", err |
| 51 | } |
| 52 | |
| 53 | var url string |
| 54 | for _, l := range r.Links { |
| 55 | if l.Rel == "next" { |
| 56 | url = l.Href |
| 57 | } |
| 58 | } |
| 59 | if url == "" { |
| 60 | return "", nil |
| 61 | } |
| 62 | |
| 63 | return url, nil |
| 64 | } |
| 65 | |
| 66 | func (r NetworkPage) IsEmpty() (bool, error) { |
| 67 | is, err := ExtractNetworks(r) |
| 68 | if err != nil { |
| 69 | return true, nil |
| 70 | } |
| 71 | return len(is) == 0, nil |
| 72 | } |
| 73 | |
| 74 | func ExtractNetworks(page pagination.Page) ([]Network, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 75 | var resp struct { |
| 76 | Networks []Network `mapstructure:"networks" json:"networks"` |
| 77 | } |
| 78 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 79 | err := mapstructure.Decode(page.(NetworkPage).Body, &resp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | |
| 84 | return resp.Networks, nil |
| 85 | } |