blob: 369aeb8ad620b63b4689110ece76000a0e4bea78 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jamie Hannafordd9036422014-09-23 17:50:24 +02004 "fmt"
5
Jamie Hannaford548d3402014-09-18 15:50:08 +02006 "github.com/mitchellh/mapstructure"
Jamie Hannafordd9036422014-09-23 17:50:24 +02007 "github.com/rackspace/gophercloud"
Jamie Hannaford548d3402014-09-18 15:50:08 +02008 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannafordd9036422014-09-23 17:50:24 +020011type commonResult struct {
12 gophercloud.CommonResult
13}
14
15func (r commonResult) Extract() (*Port, error) {
16 if r.Err != nil {
17 return nil, r.Err
18 }
19
20 var res struct {
21 Port *Port `json:"port"`
22 }
23
24 err := mapstructure.Decode(r.Resp, &res)
25 if err != nil {
26 return nil, fmt.Errorf("Error decoding Neutron port: %v", err)
27 }
28
29 return res.Port, nil
30}
31
32type CreateResult struct {
33 commonResult
34}
35
36type GetResult struct {
37 commonResult
38}
39
40type UpdateResult struct {
41 commonResult
42}
43
44type DeleteResult commonResult
45
Jamie Hannaford686c4962014-09-23 10:46:20 +020046// IP is a sub-struct that represents an individual IP.
Jamie Hannaford548d3402014-09-18 15:50:08 +020047type IP struct {
48 SubnetID string `mapstructure:"subnet_id" json:"subnet_id"`
Jamie Hannaford2a0492a2014-09-22 12:02:11 +020049 IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020050}
51
Jamie Hannaford686c4962014-09-23 10:46:20 +020052// Port represents a Neutron port. See package documentation for a top-level
53// description of what this is.
Jamie Hannaford548d3402014-09-18 15:50:08 +020054type Port struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020055 // UUID for the port.
56 ID string `mapstructure:"id" json:"id"`
57 // Network that this port is associated with.
58 NetworkID string `mapstructure:"network_id" json:"network_id"`
59 // Human-readable name for the port. Might not be unique.
60 Name string `mapstructure:"name" json:"name"`
61 // Administrative state of port. If false (down), port does not forward packets.
62 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
63 // Indicates whether network is currently operational. Possible values include
64 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
65 Status string `mapstructure:"status" json:"status"`
66 // Mac address to use on this port.
67 MACAddress string `mapstructure:"mac_address" json:"mac_address"`
68 // Specifies IP addresses for the port thus associating the port itself with
69 // the subnets where the IP addresses are picked from
70 FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"`
71 // Owner of network. Only admin users can specify a tenant_id other than its own.
72 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
73 // Identifies the entity (e.g.: dhcp agent) using this port.
74 DeviceOwner string `mapstructure:"device_owner" json:"device_owner"`
75 // Specifies the IDs of any security groups associated with a port.
76 SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
77 // Identifies the device (e.g., virtual server) using this port.
78 DeviceID string `mapstructure:"device_id" json:"device_id"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020079}
80
Jamie Hannaford686c4962014-09-23 10:46:20 +020081// PortPage is the page returned by a pager when traversing over a collection
82// of network ports.
Jamie Hannaford548d3402014-09-18 15:50:08 +020083type PortPage struct {
84 pagination.LinkedPageBase
85}
86
Jamie Hannaford686c4962014-09-23 10:46:20 +020087// NextPageURL is invoked when a paginated collection of ports has reached
88// the end of a page and the pager seeks to traverse over a new one. In order
89// to do this, it needs to construct the next page's URL.
90func (p PortPage) NextPageURL() (string, error) {
Jamie Hannaford548d3402014-09-18 15:50:08 +020091 type resp struct {
92 Links []struct {
93 Href string `mapstructure:"href"`
94 Rel string `mapstructure:"rel"`
95 } `mapstructure:"ports_links"`
96 }
97
98 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020099 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200100 if err != nil {
101 return "", err
102 }
103
104 var url string
105 for _, l := range r.Links {
106 if l.Rel == "next" {
107 url = l.Href
108 }
109 }
110 if url == "" {
111 return "", nil
112 }
113
114 return url, nil
115}
116
Jamie Hannaford686c4962014-09-23 10:46:20 +0200117// IsEmpty checks whether a PortPage struct is empty.
118func (p PortPage) IsEmpty() (bool, error) {
119 is, err := ExtractPorts(p)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200120 if err != nil {
121 return true, nil
122 }
123 return len(is) == 0, nil
124}
125
Jamie Hannaford686c4962014-09-23 10:46:20 +0200126// ExtractPorts accepts a Page struct, specifically a PortPage struct,
127// and extracts the elements into a slice of Port structs. In other words,
128// a generic collection is mapped into a relevant slice.
Jamie Hannaford548d3402014-09-18 15:50:08 +0200129func ExtractPorts(page pagination.Page) ([]Port, error) {
130 var resp struct {
131 Ports []Port `mapstructure:"ports" json:"ports"`
132 }
133
134 err := mapstructure.Decode(page.(PortPage).Body, &resp)
135 if err != nil {
136 return nil, err
137 }
138
139 return resp.Ports, nil
140}