blob: cddb58d7724f5c4cf6a585d7868e79b0310b6be8 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud/pagination"
6)
7
Jamie Hannaford686c4962014-09-23 10:46:20 +02008// IP is a sub-struct that represents an individual IP.
Jamie Hannaford548d3402014-09-18 15:50:08 +02009type IP struct {
10 SubnetID string `mapstructure:"subnet_id" json:"subnet_id"`
Jamie Hannaford2a0492a2014-09-22 12:02:11 +020011 IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020012}
13
Jamie Hannaford686c4962014-09-23 10:46:20 +020014// Port represents a Neutron port. See package documentation for a top-level
15// description of what this is.
Jamie Hannaford548d3402014-09-18 15:50:08 +020016type Port struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020017 // UUID for the port.
18 ID string `mapstructure:"id" json:"id"`
19 // Network that this port is associated with.
20 NetworkID string `mapstructure:"network_id" json:"network_id"`
21 // Human-readable name for the port. Might not be unique.
22 Name string `mapstructure:"name" json:"name"`
23 // Administrative state of port. If false (down), port does not forward packets.
24 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
25 // Indicates whether network is currently operational. Possible values include
26 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
27 Status string `mapstructure:"status" json:"status"`
28 // Mac address to use on this port.
29 MACAddress string `mapstructure:"mac_address" json:"mac_address"`
30 // Specifies IP addresses for the port thus associating the port itself with
31 // the subnets where the IP addresses are picked from
32 FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"`
33 // Owner of network. Only admin users can specify a tenant_id other than its own.
34 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
35 // Identifies the entity (e.g.: dhcp agent) using this port.
36 DeviceOwner string `mapstructure:"device_owner" json:"device_owner"`
37 // Specifies the IDs of any security groups associated with a port.
38 SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
39 // Identifies the device (e.g., virtual server) using this port.
40 DeviceID string `mapstructure:"device_id" json:"device_id"`
41
Jamie Hannaford548d3402014-09-18 15:50:08 +020042 AllowedAddressPairs []interface{} `mapstructure:"allowed" json:"allowed"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020043 ExtraDHCPOpts interface{} `mapstructure:"extra_dhcp_opts" json:"extra_dhcp_opts"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020044 BindingHostID string `mapstructure:"binding:host_id" json:"binding:host_id"`
45 BindingVIFDetails interface{} `mapstructure:"binding:vif_details" json:"binding:vif_details"`
46 BindingVIFType string `mapstructure:"binding:vif_type" json:"binding:vif_type"`
47 BindingProfile interface{} `mapstructure:"binding:profile" json:"binding:profile"`
48 BindingVNICType string `mapstructure:"binding:vnic_type" json:"binding:vnic_type"`
49}
50
Jamie Hannaford686c4962014-09-23 10:46:20 +020051// PortPage is the page returned by a pager when traversing over a collection
52// of network ports.
Jamie Hannaford548d3402014-09-18 15:50:08 +020053type PortPage struct {
54 pagination.LinkedPageBase
55}
56
Jamie Hannaford686c4962014-09-23 10:46:20 +020057// NextPageURL is invoked when a paginated collection of ports has reached
58// the end of a page and the pager seeks to traverse over a new one. In order
59// to do this, it needs to construct the next page's URL.
60func (p PortPage) NextPageURL() (string, error) {
Jamie Hannaford548d3402014-09-18 15:50:08 +020061 type resp struct {
62 Links []struct {
63 Href string `mapstructure:"href"`
64 Rel string `mapstructure:"rel"`
65 } `mapstructure:"ports_links"`
66 }
67
68 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020069 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford548d3402014-09-18 15:50:08 +020070 if err != nil {
71 return "", err
72 }
73
74 var url string
75 for _, l := range r.Links {
76 if l.Rel == "next" {
77 url = l.Href
78 }
79 }
80 if url == "" {
81 return "", nil
82 }
83
84 return url, nil
85}
86
Jamie Hannaford686c4962014-09-23 10:46:20 +020087// IsEmpty checks whether a PortPage struct is empty.
88func (p PortPage) IsEmpty() (bool, error) {
89 is, err := ExtractPorts(p)
Jamie Hannaford548d3402014-09-18 15:50:08 +020090 if err != nil {
91 return true, nil
92 }
93 return len(is) == 0, nil
94}
95
Jamie Hannaford686c4962014-09-23 10:46:20 +020096// ExtractPorts accepts a Page struct, specifically a PortPage struct,
97// and extracts the elements into a slice of Port structs. In other words,
98// a generic collection is mapped into a relevant slice.
Jamie Hannaford548d3402014-09-18 15:50:08 +020099func ExtractPorts(page pagination.Page) ([]Port, error) {
100 var resp struct {
101 Ports []Port `mapstructure:"ports" json:"ports"`
102 }
103
104 err := mapstructure.Decode(page.(PortPage).Body, &resp)
105 if err != nil {
106 return nil, err
107 }
108
109 return resp.Ports, nil
110}