Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame^] | 1 | package ports |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
| 7 | |
| 8 | type IP struct { |
| 9 | SubnetID string `mapstructure:"subnet_id" json:"subnet_id"` |
| 10 | IPAddress string `mapstructure:"ip_address" json:"ip_address"` |
| 11 | } |
| 12 | |
| 13 | type Port struct { |
| 14 | Status string `mapstructure:"status" json:"status"` |
| 15 | Name string `mapstructure:"name" json:"name"` |
| 16 | AllowedAddressPairs []interface{} `mapstructure:"allowed" json:"allowed"` |
| 17 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 18 | NetworkID string `mapstructure:"network_id" json:"network_id"` |
| 19 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 20 | ExtraDHCPOpts interface{} `mapstructure:"extra_dhcp_opts" json:"extra_dhcp_opts"` |
| 21 | DeviceOwner string `mapstructure:"device_owner" json:"device_owner"` |
| 22 | MACAddress string `mapstructure:"mac_address" json:"mac_address"` |
| 23 | FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"` |
| 24 | ID string `mapstructure:"id" json:"id"` |
| 25 | SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"` |
| 26 | DeviceID string `mapstructure:"device_id" json:"device_id"` |
| 27 | BindingHostID string `mapstructure:"binding:host_id" json:"binding:host_id"` |
| 28 | BindingVIFDetails interface{} `mapstructure:"binding:vif_details" json:"binding:vif_details"` |
| 29 | BindingVIFType string `mapstructure:"binding:vif_type" json:"binding:vif_type"` |
| 30 | BindingProfile interface{} `mapstructure:"binding:profile" json:"binding:profile"` |
| 31 | BindingVNICType string `mapstructure:"binding:vnic_type" json:"binding:vnic_type"` |
| 32 | } |
| 33 | |
| 34 | type PortPage struct { |
| 35 | pagination.LinkedPageBase |
| 36 | } |
| 37 | |
| 38 | func (current PortPage) NextPageURL() (string, error) { |
| 39 | type resp struct { |
| 40 | Links []struct { |
| 41 | Href string `mapstructure:"href"` |
| 42 | Rel string `mapstructure:"rel"` |
| 43 | } `mapstructure:"ports_links"` |
| 44 | } |
| 45 | |
| 46 | var r resp |
| 47 | err := mapstructure.Decode(current.Body, &r) |
| 48 | if err != nil { |
| 49 | return "", err |
| 50 | } |
| 51 | |
| 52 | var url string |
| 53 | for _, l := range r.Links { |
| 54 | if l.Rel == "next" { |
| 55 | url = l.Href |
| 56 | } |
| 57 | } |
| 58 | if url == "" { |
| 59 | return "", nil |
| 60 | } |
| 61 | |
| 62 | return url, nil |
| 63 | } |
| 64 | |
| 65 | func (r PortPage) IsEmpty() (bool, error) { |
| 66 | is, err := ExtractPorts(r) |
| 67 | if err != nil { |
| 68 | return true, nil |
| 69 | } |
| 70 | return len(is) == 0, nil |
| 71 | } |
| 72 | |
| 73 | func ExtractPorts(page pagination.Page) ([]Port, error) { |
| 74 | var resp struct { |
| 75 | Ports []Port `mapstructure:"ports" json:"ports"` |
| 76 | } |
| 77 | |
| 78 | err := mapstructure.Decode(page.(PortPage).Body, &resp) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | |
| 83 | return resp.Ports, nil |
| 84 | } |