blob: c190ad2b2173d7826a7f284bbbd4a3b70d452a7d [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"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020041}
42
Jamie Hannaford686c4962014-09-23 10:46:20 +020043// PortPage is the page returned by a pager when traversing over a collection
44// of network ports.
Jamie Hannaford548d3402014-09-18 15:50:08 +020045type PortPage struct {
46 pagination.LinkedPageBase
47}
48
Jamie Hannaford686c4962014-09-23 10:46:20 +020049// NextPageURL is invoked when a paginated collection of ports has reached
50// the end of a page and the pager seeks to traverse over a new one. In order
51// to do this, it needs to construct the next page's URL.
52func (p PortPage) NextPageURL() (string, error) {
Jamie Hannaford548d3402014-09-18 15:50:08 +020053 type resp struct {
54 Links []struct {
55 Href string `mapstructure:"href"`
56 Rel string `mapstructure:"rel"`
57 } `mapstructure:"ports_links"`
58 }
59
60 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020061 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford548d3402014-09-18 15:50:08 +020062 if err != nil {
63 return "", err
64 }
65
66 var url string
67 for _, l := range r.Links {
68 if l.Rel == "next" {
69 url = l.Href
70 }
71 }
72 if url == "" {
73 return "", nil
74 }
75
76 return url, nil
77}
78
Jamie Hannaford686c4962014-09-23 10:46:20 +020079// IsEmpty checks whether a PortPage struct is empty.
80func (p PortPage) IsEmpty() (bool, error) {
81 is, err := ExtractPorts(p)
Jamie Hannaford548d3402014-09-18 15:50:08 +020082 if err != nil {
83 return true, nil
84 }
85 return len(is) == 0, nil
86}
87
Jamie Hannaford686c4962014-09-23 10:46:20 +020088// ExtractPorts accepts a Page struct, specifically a PortPage struct,
89// and extracts the elements into a slice of Port structs. In other words,
90// a generic collection is mapped into a relevant slice.
Jamie Hannaford548d3402014-09-18 15:50:08 +020091func ExtractPorts(page pagination.Page) ([]Port, error) {
92 var resp struct {
93 Ports []Port `mapstructure:"ports" json:"ports"`
94 }
95
96 err := mapstructure.Decode(page.(PortPage).Body, &resp)
97 if err != nil {
98 return nil, err
99 }
100
101 return resp.Ports, nil
102}