blob: 9f6f5c57449b3a8666972d063d8543b5c49b8764 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannafordd9036422014-09-23 17:50:24 +02005 "github.com/rackspace/gophercloud"
Jamie Hannaford548d3402014-09-18 15:50:08 +02006 "github.com/rackspace/gophercloud/pagination"
7)
8
Jamie Hannafordd9036422014-09-23 17:50:24 +02009type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040010 gophercloud.Result
Jamie Hannafordd9036422014-09-23 17:50:24 +020011}
12
Jamie Hannafordf3114832014-09-24 11:00:43 +020013// Extract is a function that accepts a result and extracts a port resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020014func (r commonResult) Extract() (*Port, error) {
15 if r.Err != nil {
16 return nil, r.Err
17 }
18
19 var res struct {
20 Port *Port `json:"port"`
21 }
22
Ash Wilsond3dc2542014-10-20 10:10:48 -040023 err := mapstructure.Decode(r.Body, &res)
Jamie Hannafordd9036422014-09-23 17:50:24 +020024
Jamie Hannafordc1f36492014-10-08 15:48:48 +020025 return res.Port, err
Jamie Hannafordd9036422014-09-23 17:50:24 +020026}
27
Jamie Hannafordf3114832014-09-24 11:00:43 +020028// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020029type CreateResult struct {
30 commonResult
31}
32
Jamie Hannafordf3114832014-09-24 11:00:43 +020033// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020034type GetResult struct {
35 commonResult
36}
37
Jamie Hannafordf3114832014-09-24 11:00:43 +020038// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020039type UpdateResult struct {
40 commonResult
41}
42
Jamie Hannafordf3114832014-09-24 11:00:43 +020043// DeleteResult represents the result of a delete operation.
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010044type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050045 gophercloud.ErrResult
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010046}
Jamie Hannafordd9036422014-09-23 17:50:24 +020047
Jamie Hannaford686c4962014-09-23 10:46:20 +020048// IP is a sub-struct that represents an individual IP.
Jamie Hannaford548d3402014-09-18 15:50:08 +020049type IP struct {
50 SubnetID string `mapstructure:"subnet_id" json:"subnet_id"`
Jamie Hannaford2a0492a2014-09-22 12:02:11 +020051 IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020052}
53
Travis Truman0447aca2015-09-15 16:09:24 -040054type AddressPair struct {
55 IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
Travis Trumanb02c75b2015-09-16 11:52:05 -040056 MACAddress string `mapstructure:"mac_address" json:"mac_address,omitempty"`
Travis Truman0447aca2015-09-15 16:09:24 -040057}
58
Jamie Hannaford686c4962014-09-23 10:46:20 +020059// Port represents a Neutron port. See package documentation for a top-level
60// description of what this is.
Jamie Hannaford548d3402014-09-18 15:50:08 +020061type Port struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020062 // UUID for the port.
63 ID string `mapstructure:"id" json:"id"`
64 // Network that this port is associated with.
65 NetworkID string `mapstructure:"network_id" json:"network_id"`
66 // Human-readable name for the port. Might not be unique.
67 Name string `mapstructure:"name" json:"name"`
68 // Administrative state of port. If false (down), port does not forward packets.
69 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
70 // Indicates whether network is currently operational. Possible values include
71 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
72 Status string `mapstructure:"status" json:"status"`
73 // Mac address to use on this port.
74 MACAddress string `mapstructure:"mac_address" json:"mac_address"`
75 // Specifies IP addresses for the port thus associating the port itself with
76 // the subnets where the IP addresses are picked from
77 FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"`
78 // Owner of network. Only admin users can specify a tenant_id other than its own.
79 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
80 // Identifies the entity (e.g.: dhcp agent) using this port.
81 DeviceOwner string `mapstructure:"device_owner" json:"device_owner"`
82 // Specifies the IDs of any security groups associated with a port.
83 SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
84 // Identifies the device (e.g., virtual server) using this port.
85 DeviceID string `mapstructure:"device_id" json:"device_id"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020086}
87
Jamie Hannaford686c4962014-09-23 10:46:20 +020088// PortPage is the page returned by a pager when traversing over a collection
89// of network ports.
Jamie Hannaford548d3402014-09-18 15:50:08 +020090type PortPage struct {
91 pagination.LinkedPageBase
92}
93
Jamie Hannaford686c4962014-09-23 10:46:20 +020094// NextPageURL is invoked when a paginated collection of ports has reached
95// the end of a page and the pager seeks to traverse over a new one. In order
96// to do this, it needs to construct the next page's URL.
97func (p PortPage) NextPageURL() (string, error) {
Jamie Hannaford548d3402014-09-18 15:50:08 +020098 type resp struct {
Jamie Hannafordc1f36492014-10-08 15:48:48 +020099 Links []gophercloud.Link `mapstructure:"ports_links"`
Jamie Hannaford548d3402014-09-18 15:50:08 +0200100 }
101
102 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +0200103 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200104 if err != nil {
105 return "", err
106 }
107
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200108 return gophercloud.ExtractNextURL(r.Links)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200109}
110
Jamie Hannaford686c4962014-09-23 10:46:20 +0200111// IsEmpty checks whether a PortPage struct is empty.
112func (p PortPage) IsEmpty() (bool, error) {
113 is, err := ExtractPorts(p)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200114 if err != nil {
115 return true, nil
116 }
117 return len(is) == 0, nil
118}
119
Jamie Hannaford686c4962014-09-23 10:46:20 +0200120// ExtractPorts accepts a Page struct, specifically a PortPage struct,
121// and extracts the elements into a slice of Port structs. In other words,
122// a generic collection is mapped into a relevant slice.
Jamie Hannaford548d3402014-09-18 15:50:08 +0200123func ExtractPorts(page pagination.Page) ([]Port, error) {
124 var resp struct {
125 Ports []Port `mapstructure:"ports" json:"ports"`
126 }
127
128 err := mapstructure.Decode(page.(PortPage).Body, &resp)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200129
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200130 return resp.Ports, err
Jamie Hannaford548d3402014-09-18 15:50:08 +0200131}